1. Introduction
In the previous article, we have implemented monitoring of a single service instance. Of course, in actual applications, the monitoring data of a single instance is not very valuable. What we need more is the monitoring information of a cluster system. At this time, we need to introduce Turbine. Turbine can collect monitoring information and provide the aggregated information to Hystrix Dashboard for centralized display and monitoring.
2. Build monitoring bureaus and services
2.1 Overall structure and preparation
The engineering implementation of this article is based on the project of the previous article. By introducing Turbine, it aggregates the monitoring information of the service-ribbon service and outputs it to the Hystrix Dashboard for display. Because we need a Dashboard for multiple services, we will build a service-ribbon cluster, and we need to create a new service-turbine project to aggregate information and display it. The final overall structure is as follows:
2.2 Create a service-turbine
Create a new Spring Boot project, named service-turbine, and import the necessary dependencies in pom.xml:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dan</groupId> <artifactId>service-turbine</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-turbine</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-turbine</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Add the @EnableTurbine annotation to the project startup class and enable Turbine. The annotation already contains the @EnableDiscoveryClient annotation, that is, the registration service has been enabled:
@SpringBootApplication @EnableTurbine public class ServiceTurbineApplication { public static void main(String[] args) { SpringApplication.run(ServiceTurbineApplication.class, args); } } Add Eureka and Turbine related configurations in application.properties, as follows:
spring.application.name=service-turbine server.port=8766 security.basic.enabled=false eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka turbine.aggregator.cluster-config=default turbine.app-config=service-ribbon turbine.cluster-name-expression="default" turbine.combine-host-port=true
In the above configuration, the turbine.app-config parameter configures the serviceId list in Eureka to indicate which services are monitored; the turbine.cluster-name-expression parameter specifies the cluster name as default. When there are a large number of services, multiple Turbine services can be started to build different aggregation clusters. This parameter can be used to distinguish these different aggregation clusters. At the same time, the parameter value can be used to locate different aggregation clusters in the Hystrix dashboard. Just specify the cluster parameter in the URL of the Hystrix Stream; the turbine.combine-host-port parameter is set to true, so that services on the same host can be distinguished by the combination of host name and port number. By default, different services will be distinguished by host, which will make the different services on the local machine aggregate into one service for statistics during debugging.
3. Cluster monitoring
Start the eureka-server project with the port number 8761;
Start the eureka-client project with port numbers 8762 and 8763;
Start the service-ribbon project with port numbers 8764 and 8765;
Start the service-turbine project with the port number 8766.
Visit http://localhost:8766/turbine.stream to see:
Visit http://localhost:8765/hystrix, enter the monitoring stream http://localhost:8766/turbine.stream to see:
As you can see in the figure, although we have started two service-ribbons, the monitoring page is still only showing a monitoring diagram. However, we can find that the Hosts attribute in the cluster reporting area in the figure is different from when trying stand-alone monitoring before, but because these two instances are the same service, and for the cluster, we are concerned about the high availability of the service cluster, so Turbine will view the same service as a whole and summarize it into a monitoring diagram.
Of course, if you really want to see two monitoring diagrams, you can set the spring.application.name of one of the instances of service-ribbon to another name, so that two different monitoring diagrams will appear after running.
4. Combined with message broker
When Spring Cloud encapsulates Turbine, it also encapsulates a collection implementation based on message broker. Therefore, we can output all the monitoring information that needs to be collected into the message broker, and then the Turbine service will obtain these monitoring information asynchronously from the message broker, and finally aggregate and output them to the Hystrix Dashboard. By introducing a message broker, the monitoring architecture implemented by our Turbine and Hystrix Dashboard can be changed to the structure shown in the figure below:
First, create a new Spring Boot project, named service-turbine-amqp, and introduce spring-cloud-starter-turbine-amqp and spring-boot-starter-actuator dependencies. Seeing this is just introducing these two dependencies, because spring-cloud-starter-turbine-amqp actually wraps spring-cloud-starter-turbine-stream and spring-cloud-starter-stream-rabbit.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dan</groupId> <artifactId>service-turbine-amqp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-turbine-amqp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <scope>test</scope> </dependency> </dependency> </dependency> <dependencyManagement> <dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependency> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Use the @EnableTurbineStream annotation in the main class to enable the configuration of Turbine Stream:
@SpringBootApplication @EnableTurbineStream @EnableDiscoveryClient public class ServiceTurbineAmqpApplication { public static void main(String[] args) { SpringApplication.run(ServiceTurbineAmqpApplication.class, args); } }Then configure the application.properties file:
spring.application.name=service-turbine-amqp server.port=8766 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
The configuration of Turbine has been completed. The following needs to make some modifications to the service-ribbon so that its monitoring information can be output to RabbitMQ. This modification is also very simple. Just add dependency on spring-cloud-netflix-hystrix-amqp in pom.xml, as follows:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-amqp</artifactId> </dependency>
After completing the above configuration, start all required projects, visit http://localhost:8765/hystrix, enter the monitoring stream http://localhost:8766/turbine.stream, and we can see the same result as before, except that the monitoring information collection here is implemented asynchronously through the message broker.
Source code download: https://github.com/lingd3/SpringCloudLearning/tree/master/chapter8
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.