Skip to content

Commit

Permalink
Merge branch 'part-8'
Browse files Browse the repository at this point in the history
# Conflicts:
#	api-gateway/src/main/resources/application.properties
#	discovery-server/src/main/resources/application.properties
#	inventory-service/src/main/java/com/programmingtechie/inventoryservice/service/InventoryService.java
#	inventory-service/src/main/resources/application.properties
#	order-service/pom.xml
#	order-service/src/main/java/com/programmingtechie/orderservice/service/OrderService.java
#	order-service/src/main/resources/application.properties
#	product-service/pom.xml
#	product-service/src/main/resources/application.properties
  • Loading branch information
SaiUpadhyayula committed Sep 17, 2022
2 parents ae0a373 + 62ab267 commit b310baf
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api-gateway/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ spring.cloud.gateway.routes[3].predicates[0]=Path=/eureka/**
spring.security.oauth2.resourceserver.jwt.issuer-uri= http://localhost:8181/realms/spring-boot-microservices-realm

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability= 1.0
2 changes: 1 addition & 1 deletion discovery-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ server.port=8761
app.eureka.username=eureka
app.eureka.password=password
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability= 1.0
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
version: '3'
services:
zookeeper:
image: confluentinc/cp-zookeeper:7.0.1
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000

broker:
image: confluentinc/cp-kafka:7.0.1
container_name: broker
ports:
- "9092:9092"
depends_on:
- zookeeper
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class InventoryService {
@Transactional(readOnly = true)
@SneakyThrows
public List<InventoryResponse> isInStock(List<String> skuCode) {
log.info("Checking Inventory");
log.info("Wait Started");
// Thread.sleep(10000);
// log.info("Wait Ended");
return inventoryRepository.findBySkuCodeIn(skuCode).stream()
.map(inventory ->
InventoryResponse.builder()
Expand Down
3 changes: 2 additions & 1 deletion inventory-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ spring.jpa.hibernate.ddl-auto=create-drop
server.port=0
eureka.client.serviceUrl.defaultZone=http://eureka:password@localhost:8761/eureka
spring.application.name=inventory-service

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability= 1.0
76 changes: 76 additions & 0 deletions notification-service/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>microservices-new</artifactId>
<groupId>com.programming.techie</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>notification-service</artifactId>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.programming.techie;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;

@SpringBootApplication
@Slf4j
public class NotificationServiceApplication {

public static void main(String[] args) {
SpringApplication.run(NotificationServiceApplication.class, args);
}

@KafkaListener(topics = "notificationTopic")
public void handleNotification(OrderPlacedEvent orderPlacedEvent) {
// send out an email notification
log.info("Received Notification for Order - {}", orderPlacedEvent.getOrderNumber());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.programming.techie;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
}
15 changes: 15 additions & 0 deletions notification-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
eureka.client.serviceUrl.defaultZone=http://eureka:password@localhost:8761/eureka
spring.application.name=notification-service
server.port=0

spring.sleuth.integration.enabled=true
spring.zipkin.base-url=http://localhost:9411/
spring.sleuth.sampler.probability=1

# Kafka Properties
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=notificationTopic
spring.kafka.consumer.group-id= notificationId
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.type.mapping=event:com.programming.techie.OrderPlacedEvent
4 changes: 4 additions & 0 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,9 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.programmingtechie.orderservice.event;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderPlacedEvent {
private String orderNumber;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.programmingtechie.orderservice.service;

import brave.Span;
import brave.Tracer;
import com.programmingtechie.orderservice.dto.InventoryResponse;
import com.programmingtechie.orderservice.dto.OrderLineItemsDto;
import com.programmingtechie.orderservice.dto.OrderRequest;
import com.programmingtechie.orderservice.event.OrderPlacedEvent;
import com.programmingtechie.orderservice.model.Order;
import com.programmingtechie.orderservice.model.OrderLineItems;
import com.programmingtechie.orderservice.repository.OrderRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.reactive.function.client.WebClient;
Expand All @@ -21,12 +22,12 @@
@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class OrderService {

private final OrderRepository orderRepository;
private final WebClient.Builder webClientBuilder;
private final Tracer tracer;
private final KafkaTemplate<String, OrderPlacedEvent> kafkaTemplate;

public String placeOrder(OrderRequest orderRequest) {
Order order = new Order();
Expand All @@ -43,11 +44,11 @@ public String placeOrder(OrderRequest orderRequest) {
.map(OrderLineItems::getSkuCode)
.toList();

log.info("Calling inventory service");

Span inventoryServiceLookup = tracer.nextSpan().name("InventoryServiceLookup");

try(Tracer.SpanInScope spanInScope = tracer.withSpan(inventoryServiceLookup.start())){
try (Tracer.SpanInScope isLookup = tracer.withSpanInScope(inventoryServiceLookup.start())) {

inventoryServiceLookup.tag("call", "inventory-service");
// Call Inventory Service, and place order if product is in
// stock
InventoryResponse[] inventoryResponsArray = webClientBuilder.build().get()
Expand All @@ -60,14 +61,15 @@ public String placeOrder(OrderRequest orderRequest) {
boolean allProductsInStock = Arrays.stream(inventoryResponsArray)
.allMatch(InventoryResponse::isInStock);

if(allProductsInStock){
if (allProductsInStock) {
orderRepository.save(order);
kafkaTemplate.send("notificationTopic", new OrderPlacedEvent(order.getOrderNumber()));
return "Order Placed Successfully";
} else {
throw new IllegalArgumentException("Product is not in stock, please try again later");
}
} finally {
inventoryServiceLookup.end();
inventoryServiceLookup.flush();
}
}

Expand Down
10 changes: 9 additions & 1 deletion order-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ resilience4j.retry.instances.inventory.max-attempts=3
resilience4j.retry.instances.inventory.wait-duration=5s

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability= 1.0

# Kafka Properties
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.template.default-topic=notificationTopic
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
spring.kafka.producer.properties.spring.json.type.mapping=event:com.programmingtechie.orderservice.event.OrderPlacedEvent

1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>inventory-service</module>
<module>discovery-server</module>
<module>api-gateway</module>
<module>notification-service</module>
</modules>

<properties>
Expand Down
1 change: 1 addition & 0 deletions product-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>

</dependencies>

</project>
3 changes: 2 additions & 1 deletion product-service/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ spring.data.mongodb.uri=mongodb://localhost:27017/product-service
eureka.client.serviceUrl.defaultZone=http://eureka:password@localhost:8761/eureka
spring.application.name=product-service
server.port=0

spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0
spring.sleuth.sampler.probability= 1.0

0 comments on commit b310baf

Please sign in to comment.