Skip to content

Commit 2769ac7

Browse files
author
Абрамов Александр
committed
Add trace logging sleuth zipkin
1 parent 6653806 commit 2769ac7

File tree

12 files changed

+55
-18
lines changed

12 files changed

+55
-18
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ Based on [Spring Boot Microservices Full Course](https://www.youtube.com/playlis
1515
start keycloak in docker
1616
```
1717
docker run -p 8181:8080 -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin quay.io/keycloak/keycloak:18.0.0 start-dev
18+
```
19+
start zipkin in docker
20+
```
21+
docker run -d -p 9411:9411 openzipkin/zipkin
1822
```

api-gateway/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ repositories {
1212
dependencies {
1313
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
1414
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
15+
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
16+
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
1517
implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
1618
implementation 'org.springframework.boot:spring-boot-starter-security'
1719
}

api-gateway/src/main/resources/application.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ spring.cloud.gateway.routes[3].uri=http://localhost:8761
2424
spring.cloud.gateway.routes[3].predicates[0]=Path=/eureka/**
2525

2626
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8181/realms/spring-boot-microservices-realm
27+
28+
spring.zipkin.base-url=http://localhost:9411
29+
sping.sleuth.sampler.probability=1.0

discovery-server/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ repositories {
1212
dependencies {
1313
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
1414
implementation 'org.springframework.boot:spring-boot-starter-security'
15+
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
16+
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
1517
}

discovery-server/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ eureka.client.register-with-eureka=false
55
eureka.client.fetch-registry=false
66

77
eureka.username=${EUREKA_USERNAME:eureka}
8-
eureka.password=${EUREKA_PASSWORD:password}
8+
eureka.password=${EUREKA_PASSWORD:password}
9+
10+
spring.zipkin.base-url=http://localhost:9411
11+
sping.sleuth.sampler.probability=1.0

inventory-service/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ repositories {
1111

1212
dependencies {
1313
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
14+
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
15+
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
1416
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
1517
implementation 'org.springframework.boot:spring-boot-starter-web'
1618
compileOnly 'org.projectlombok:lombok'

inventory-service/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ spring.datasource.password=postgres
88

99
spring.jpa.hibernate.ddl-auto=create-drop
1010

11-
eureka.client.service-url.defaultZone=http://eureka:password@localhost:8761/eureka
11+
eureka.client.service-url.defaultZone=http://eureka:password@localhost:8761/eureka
12+
13+
spring.zipkin.base-url=http://localhost:9411
14+
sping.sleuth.sampler.probability=1.0

order-service/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ repositories {
1919
dependencies {
2020
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
2121
implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
22+
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
23+
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
2224
implementation 'org.springframework.boot:spring-boot-starter-actuator'
2325
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
2426
implementation 'org.springframework.boot:spring-boot-starter-web'

order-service/src/main/java/com/alllexe/orderservice/service/OrderService.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.alllexe.orderservice.model.OrderLineItems;
88
import com.alllexe.orderservice.repository.OrderRepository;
99
import lombok.RequiredArgsConstructor;
10+
import org.springframework.cloud.sleuth.Span;
11+
import org.springframework.cloud.sleuth.Tracer;
1012
import org.springframework.stereotype.Service;
1113
import org.springframework.transaction.annotation.Transactional;
1214
import org.springframework.web.reactive.function.client.WebClient;
@@ -21,6 +23,7 @@ public class OrderService {
2123

2224
private final OrderRepository orderRepository;
2325
private final WebClient.Builder webClientBuilder;
26+
private final Tracer tracer;
2427

2528
@Transactional
2629
public String placeOrder(OrderRequest orderRequest) {
@@ -37,22 +40,27 @@ public String placeOrder(OrderRequest orderRequest) {
3740
.map(OrderLineItems::getSkuCode)
3841
.toList();
3942

40-
// Call Inventory Service, and place order if product is in stock
41-
InventoryResponse[] inventoryResponseArray = webClientBuilder.build().get()
42-
.uri("http://inventory-service/api/inventory",
43-
uriBuilder -> uriBuilder.queryParam("skuCodes", skuCodes).build())
44-
.retrieve()
45-
.bodyToMono(InventoryResponse[].class)
46-
.block();
43+
Span inventoryServiceLookup = tracer.nextSpan().name("InventoryServiceLookup");
44+
try (Tracer.SpanInScope tracerInScope = tracer.withSpan(inventoryServiceLookup.start())) {
45+
// Call Inventory Service, and place order if product is in stock
46+
InventoryResponse[] inventoryResponseArray = webClientBuilder.build().get()
47+
.uri("http://inventory-service/api/inventory",
48+
uriBuilder -> uriBuilder.queryParam("skuCodes", skuCodes).build())
49+
.retrieve()
50+
.bodyToMono(InventoryResponse[].class)
51+
.block();
4752

48-
boolean allProductsInStock = Arrays.stream(inventoryResponseArray)
49-
.allMatch(InventoryResponse::isInStock);
53+
boolean allProductsInStock = Arrays.stream(inventoryResponseArray)
54+
.allMatch(InventoryResponse::isInStock);
5055

51-
if (allProductsInStock) {
52-
orderRepository.save(order);
53-
return "Order placed successfully";
54-
} else {
55-
throw new IllegalArgumentException("Product is not in stock");
56+
if (allProductsInStock) {
57+
orderRepository.save(order);
58+
return "Order placed successfully";
59+
} else {
60+
throw new IllegalArgumentException("Product is not in stock");
61+
}
62+
} finally {
63+
inventoryServiceLookup.end();
5664
}
5765
}
5866

order-service/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ resilience4j.retry.instances.inventory.wait-duration=5s
3434
#Spring Cloud Stream Kafka Properties
3535
spring.cloud.stream.output-bindings=notificationEventSupplier
3636
spring.cloud.stream.bindings.notificationEventSupplier-out-0.destination=notification-events
37-
spring.sleuth.integration.enabled=true
37+
spring.sleuth.integration.enabled=true
38+
39+
spring.zipkin.base-url=http://localhost:9411
40+
sping.sleuth.sampler.probability=1.0

0 commit comments

Comments
 (0)