{{announcement.body}}
{{announcement.title}}

Spring Cloud and Turbine

DZone 's Guide to

Spring Cloud and Turbine

In this article, we'll introduce you to Spring Cloud Netflix Turbine. It aggregates multiple Hystrix Metrics Streams into a single dashboard view.

· Cloud Zone ·
Free Resource

In this article, we'll introduce you to Spring Cloud Netflix Turbine. It aggregates multiple Hystrix Metrics Streams into one, so that it could be displayed into a single dashboard view.

To give a small introduction to Hystrix. In a microservice architecture, we have many small applications that talk to each other to complete a request.

There is always a possibility that one of these downstream services won't respond correctly or simply fails completely. To prevent any cascading failures we set up a Hystrix fallback mechanism for our microservices.

Each microservice that implements Hystrix can choose to expose the Hystrix Metrics Streams (via the actuator endpoint /hystrix.stream) that can be viewed via the Hystrix Dashboard.

Turbine is an open-source tool from Netflix for aggregating multiple streams into a single stream. Spring provided a nice wrapper around it to be easily used in the Spring ecosystem.

Setup

The setup is similar to the Spring Cloud: Hystrix setup. Here's what our back-end service looks like:

  • Eureka Server: Acts as a service registry and running on port 8761.
  • Order Service: A simple REST service that has a single endpoint of /order-details and running on port 8070.
  • User Service: A simple REST service that has a single endpoint of  /customer/{customerId}  and running on port 8060.
  • Hystrix Turbine: A Hystrix dashboard service to display Hystrix streams running on port 9090.

Eureka Dashboard - http://localhost:8761/ 

Both the  customer-service  and  order-service  have implemented the Hystrix fallback mechanism and have the /hystrix.stream endpoint exposed via Actuator:

  • Hystrix endpoint for user-service: http://localhost:8060/actuator/hystrix.stream
  • Hystrix endpoint for recommnedation-service: http://localhost:8070/actuator/hystrix.stream

We can check both this individually in our Hystrix Dashboard by typing the URL in the box and clicking "Monitor Stream":

Hystrix Dashboard - http://localhost:9090/hystrix 

We'll see a metric like this:  Note: If you don't see any stream, then you probably have to hit the endpoints of the service whose stream you want to see. Ex: for customer-service we have to hit  http://localhost:8070/order-details/1   to generate the stream. 

Installing Turbine

You might have realized that looking at the individual stream is not very productive, especially when we have many microservices.

Turbine can aggregate all these individual hystrix.streams to a single turbine.stream, which can be viewed on the Hystrix Dashboard.

It uses the DiscoveryClient interface to find out relevant services that produce /hystrix.streams.

To add Turbine to your Hystrix Dashboard, add the following dependency:

XML
 




xxxxxxxxxx
1


 
1
<dependency>
2
    <groupId>org.springframework.cloud</groupId>
3
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
4
</dependency>



Note: This is a starter dependency of Turbine, which by default uses Spring Cloud Eureka as the discovery server. If you are using Spring Cloud Consul, use the following dependencies: 

XML
 




xxxxxxxxxx
1


1
<dependency>
2
    <groupId>org.springframework.cloud</groupId>
3
    <artifactId>spring-cloud-netflix-turbine</artifactId>
4
</dependency>
5
<dependency>
6
    <groupId>org.springframework.cloud</groupId>
7
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
8
</dependency>



In this article, we'll be using the starter dependency, i.e spring-cloud-starter-netflix-turbine.

To enable Turbine, we simply annotate our main class with @EnableTurbine:

Java
 




xxxxxxxxxx
1
18


 
1
import org.springframework.boot.SpringApplication;
2
import org.springframework.boot.autoconfigure.SpringBootApplication;
3
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
4
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
5
import org.springframework.cloud.netflix.turbine.EnableTurbine;
6
 
          
7
 
          
8
@EnableTurbine
9
@EnableDiscoveryClient
10
@EnableHystrixDashboard
11
@SpringBootApplication
12
public class TurbineServiceApplication {
13
 
          
14
    public static void main(String[] args) {
15
        SpringApplication.run(TurbineServiceApplication.class, args);
16
    }
17
 
          
18
}



application.properties

Properties files
 




xxxxxxxxxx
1
11


 
1
server.port= 9090
2
 
          
3
spring.application.name= hystirx-turbine
4
 
          
5
eureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/
6
 
          
7
turbine.appConfig=customer-service,order-service
8
 
          
9
#turbine.aggregator.clusterConfig= CUSTOMER-SERVICE,ORDER-SERVICE
10
#or
11
turbine.clusterNameExpression= new String("default")



For Turbine to work as expected we have to add few details to our application.propeties:

Properties files
 




xxxxxxxxxx
1


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



Here, we are telling Turbine the Eureka server location, applications whose /hystrix.stream it needs to fetch and turbine.clusterNameExpression as new String("default"), which gives the cluster name of "default".

We can open up http://localhost:9090/turbine.stream?cluster=default to see the aggregate stream of both user-service and recommendation-service:

Recommendation service output

Again, if you are not viewing anything, just hit the user-service and recommendation-service endpoints to generate the streams.

We can also use this URL on our Hystrix dashboard to generate a nice aggregated view:

Hybrid dashboard

order-service

Order.java

Java
 







OrderController.java

Java
 







pom.xml

XML
 







Topics:
dashboard ,data visualization ,hystrix ,spring-cloud ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}