Integrating microservice components with Spring Cloud Sleuth

When we call the add service on the service consumer, it will invoke Microservice A through API Gateway. To be able to track the service call across different components, we would need something unique assigned to the request flow across components.

Spring Cloud Sleuth provides options to track a service call across different components using a concept called span. Each span has a unique 64-bit ID. The unique ID can be used to trace the call across components.

The following snippet shows the dependency for spring-cloud-starter-sleuth:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

We need to add the preceding dependency on Spring Cloud Sleuth to the following three projects listed:

  • Microservice A
  • Service consumer
  • Zuul API Gateway Server

We will start with tracing all the service requests across microservices. To be able to trace all the requests, we will need to configure an AlwaysSampler bean, as shown in the following snippet:

    @Bean
public AlwaysSampler defaultSampler() {
return new AlwaysSampler();
}

The AlwaysSampler bean needs to be configured in the following microservice application classes:

  • MicroserviceAApplication
  • ServiceConsumerApplication
  • ZuulApiGatewayServerApplication

When we invoke the add service at http://localhost:8765/service-consumer/add, we will see the typical response:

    1748

However, you will start to see a few more details in the log entries. A simple entry from the service consumer microservice log is shown here:

2017-03-28 20:53:45.582 INFO [service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true] 89416 --- [l-api-gateway-5] c.netflix.loadbalancer.BaseLoadBalancer : Client:zuul-api-gateway instantiated a LoadBalancer:DynamicServerListLoadBalancer:{NFLoadBalancer:name=zuul-api-gateway,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null

[service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true]: The first value service-consumer is the application name. The key part is the second value--d8866b38c3a4d69c. This is the value that can be used to trace this request across other microservice components.

The following are some other entries from the service consumer log:

2017-03-28 20:53:45.593 INFO [service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true] 89416 --- [l-api-gateway-5] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2017-03-28 20:53:45.597 INFO [service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true] 89416 --- [l-api-gateway-5] c.netflix.config.ChainedDynamicProperty : Flipping property: zuul-api-gateway.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-03-28 20:53:45.599 INFO [service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true] 89416 --- [l-api-gateway-5] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client zuul-api-gateway initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=zuul-api-gateway,current list of Servers=[192.168.1.5:8765],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
[service-consumer,d8866b38c3a4d69c,d8866b38c3a4d69c,true] 89416 --- [nio-8100-exec-1] c.m.s.c.service.NumberAdderController : Returning 1748

The following is an extract from the Microservice A log:

[microservice-a,d8866b38c3a4d69c,89d03889ebb02bee,true] 89404 --- [nio-8080-exec-8] c.m.s.c.c.RandomNumberController : Returning [425, 55, 51, 751, 466]

The following is an extract from the Zuul API Gateway log:

[zuul-api-gateway,d8866b38c3a4d69c,89d03889ebb02bee,true] 89397 --- [nio-8765-exec-8] c.m.s.z.filters.pre.SimpleLoggingFilter : Request Method : GET
URL: http://192.168.1.5:8765/microservice-a/random

As you can see in the preceding log extracts, we can use the second value in the log--called span ID--to trace the service call across microservice components. In this example, the span ID is d8866b38c3a4d69c.

However, this requires searching through logs of all the microservice components. One option is to implement a centralized log using something like an ELK (Elasticsearch, Logstash, and Kibana) stack. We will take the simpler option of creating a Zipkin Distributed Tracing service in the next step.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset