Detailed explanation of the integration of microservices and spring cloud turbine

Time:2020-1-9

1. overview

In this article, I’ll introduce you to spring cloud Netflix turbine. It aggregates multiple hystrix metrics streams into one for display in a dashboard view.

A brief introduction to hystrix. In the microservice architecture, we have many small applications that communicate with each other to complete requests. These downstream services may not respond correctly or fail completely. In order to prevent cascading failures, we set up the hystrix fallback mechanism for microservices.

Each microservice implementing hystrix can choose to expose the hystrix metrics streams (via the actor endpoint / hystrix. Stream) for viewing through the hystrix dashboard.

If you want to learn more, I’ve covered this in detail in spring cloud: hystrix.

Turbine is an open source tool for Netflix to aggregate multiple streams into one stream. Spring provides a good wrapper for easy use in the spring ecosystem.

2. build

Similar to the setting of spring cloud: hystrix, the back-end services are as follows:

  • Eureka server: runs as a service registry and on port 8761.
  • Recommendation service: a simple rest service with only one endpoint: recommendations, running on port 8070.
  • User service: a simple rest service, with a single endpoint of: / personalized / {ID}, and running on port 8060.
  • Hystrix turbine: the hystrix dashboard service, which displays the hystrix stream and runs on port ‘9090’.

Here is the list of services we see on Eureka server:

Both user service and recommendation service implement the hystrix fallback mechanism, and expose the / hystrix.stream endpoint through the activator:

  • Hystrix endpoint of user service: http://localhost: 8060/actor/hystrix.stream
  • Hystrix endpoint for recommended services: http://localhost: 8070/actor/hystrix.stream

We can view these separately in the hystrix dashboard by typing the URL in the box and clicking monitor stream:

You will see the following metrics:

Note: if you do not see any streams, you may have to click on the service endpoint of that stream. For example, for user service, we can click http: / / localhost: 8060 / personalized / 1 to generate the flow.

3. Install turbine

You may have realized that viewing a single stream is inefficient, especially when there are many microservices.

Turbine can aggregate all the individual hystrix. Streams into a single turbine. Stream for viewing on the hystrix dashboard.

It uses the discoveryclient interface to find out about the services that produce / hystrix. Stream.

To add turbine to the hystrix dashboard, add the following dependencies:


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

Note: This is turbine’s starter dependency. Spring cloud Eureka is used as the service discovery by default. If you are using spring cloud consult, use the following dependencies:


<dependency> 
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency> 
<dependency> 
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency> 

In this article, we’ll use the starter dependency, spring cloud starter Netflix turbine.

To enable turbine, simply annotate the main class with @ enableturbine:


@SpringBootApplication
@EnableTurbine
@EnableDiscoveryClient
@EnableHystrixDashboard
public class HystrixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication.class, args);
}
}

In order for turbine to work as expected, we must add some details to application.properties:


server.port= 9090 
spring.application.name= hystirx-turbine 
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/ 
turbine.appConfig= user-service,recommendation-service 
turbine.clusterNameExpression= new String("default") 

Here, we tell you the location of the turbine Eureka server and the application it needs to get / hystrix. Stream. And set turbine.clusternameexpression to new string (“default”), that is, the default cluster name is “default”.

We can open http: / / localhost: 9090 / turbine. Stream? Cluster = default to view the aggregate flow of user service and recommendation service:

Similarly, if you don’t see anything, just click the user service and recommendation service endpoints to generate the flow.

We can also use this URL on the hystrix dashboard to generate a good aggregation view:

Sometimes, you may want to use Eureka’s serviceid as the dashboard’s cluster name. This can be done by setting turbine.aggregator.clusterconfig:

server.port = 9090
spring.application.name = hystirx-turbine
eureka.client.serviceUrl.defaultZone = http:// localhost:8761 / eureka /
turbine.aggregator.clusterConfig = USER-SERVICE,RECOMMENDATION-SERVICE
Turbine.appconfig = user service, recommended service

You can also check the currently configured clusters in the turbine application by clicking on the / clusters endpoint.

You can disable this endpoint by setting turbine.endpoints.clusters.enabled to false.

So now we can treat turbine.stream as Eureka ID, for example: http: / / localhost: 9090 / turbine. Stream? Cluster = user-service

If more than one instance of a particular service is running, turbine sorts by cluster and displays it in the results.

4. summary

In this article, we’ve shown you how to set up turbine based on the hystrix stream to get an aggregate view. We first saw the classic way for turbine to get the hystrix stream from all services.

As always, the sample code used in this article can be found on GitHub.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.