Tracing and Circuit-Breaker with Microservices

Introduction

Up to now I have discussed about using Spring Boot for developing microservices [1]. Netflix stack has provided utility solutions for microservices deployments. And I have shown some of those solutions can be used with Spring Boot microservices [2]. So this article is going to be a continuation from the last article on microservices. In here I am going to discuss on using Zipkin tracing, Hystrix Circuit-Breaker and Hystrix Dashboard with Turbine.

The use-case so far

The use-case we have taken is 2 microservices; payment-service and customerinfo-service communicating each other and fronted by a gateway service (based on Zuul). Eureka-service sits as a registry service and helps to figure out the correct service. In this article we are going to implement tracing functionality and circuit-breaker functionality to payment-service.

Implementing zipkin-service

Zipkin is the solution provided in Netflix OSS for distributed tracing of requests. To add it to your microservice setup, you need to implement the zipkin-service first and then configure other services to send tracing events to that service.

First add following two dependencies to each service’s pom file



org.springframework.cloud
spring-cloud-starter-sleuth



org.springframework.cloud
spring-cloud-sleuth-zipkin


Then you have to create the zipkin-service with following artifacts, similar to other microservices.

pom.xml file, refer : https://github.com/Buddhima/project-mss/blob/step-2/zipkin-service/pom.xml

Application.java file with @EnableZipkinServer annotation

package com.buddhima.example.service.zipkin;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import zipkin.server.EnableZipkinServer;

@SpringBootApplication
@EnableZipkinServer
public class Application {

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

application.yml file

server:
  port: 9411

spring:
  application:
    name: zipkin-service

Following image depicts zipkin tracing of a request through microservices:

traces

Implementing Hystrix Circuit-Breaker

In short, a circuit-breaker is a design pattern which helps clients to be fault-tolerant with frequently failing endpoints. You will get a more detailed explanation on that concept through references [3], [4].

In this case I have added circuit-breaker functionality to the client-implementation of customerinfo-service at payment-service. In last article I have discussed how feign is used as the client. In addition to those attributes mentioned, we need to add hystrix dependencies in the pom file, implement fallback methods and point to those at feign client definition.

Following dependencies are required for hystrix and hystrix-dashboard

<!-- dependency for hystrix -->

org.springframework.cloud
spring-cloud-starter-hystrix


<!-- dependencies for hystrix-dashboard -->

org.springframework.boot
spring-boot-starter-actuator


org.springframework.cloud
spring-cloud-starter-hystrix-dashboard

Together with Hystrix, I have enabled Hystrix-Dashboard to view the status of each endpoint. This can be done via @EnableCircuitBreaker, @EnableHystrixDashboard annotations.

Then the client interface has been modified, specifying the fallback class as below:


package com.buddhima.example.service.payment.clients;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "customerinfo-service", path = "/customerinfo", fallback = CustomerInfoClientFallback.class)
public interface CustomerInfoClient {

@RequestMapping(method = RequestMethod.GET, value = "/name")
public String getName();

@RequestMapping(method = RequestMethod.GET, value = "/age")
public int getAge();
}

You can refer the implementation of CustomerInfoClientFallback at here: https://github.com/Buddhima/project-mss/blob/step-2/payment-service/src/main/java/com/buddhima/example/service/payment/clients/CustomerInfoClientFallback.java

By-default endpoint dashboard is per microservice, which is not usable when you have many microservices. Therefore Netflix has introduced a solution called Turbine which can aggregate the endpoint statuses of each microservice and construct an aggregated stream to be displayed at hystrix-dashboard.

You may refer source code of turbine-service at here: https://github.com/Buddhima/project-mss/tree/step-2/turbine-service

In the configuration file (application.yml), I have specified the names of microservices which need to be monitored


server:
port: 9060

spring:
application:
name: turbine-service

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
preferIpAddress: true

turbine:
clusterNameExpression: new String("default")
appConfig: PAYMENT-SERVICE,CUSTOMERINFO-SERVICE,EDGE-SERVICE
combineHostPort: true

(Here I have configured hystrix in the payment-service only. You may add hystrix configs to other services as well)

hystrix-dashboard-e1517331780499.png
Hystrix Dashboard

You can check the git repository containing the complete source code at here [5].

References

[1] Getting start with Spring Boot : https://buddhimawijeweera.wordpress.com/2017/05/04/microservices-with-spring-boot/
[2] Netflix OSS with Spring Boot : https://buddhimawijeweera.wordpress.com/2017/12/29/microservices-with-netflix-stack-and-spring-boot/
[3] How Hystrix works : https://github.com/Netflix/Hystrix/wiki/How-it-Works
[4] Spring sample for Circuit-Breaker : https://spring.io/guides/gs/circuit-breaker/
[5] Source code : https://github.com/Buddhima/project-mss/tree/step-2

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s