Spring cloud Part 5: fuse monitoring

Posted by madhu on Mon, 29 Jun 2020 06:47:29 +0200

Hystrix Dashboard is a real-time monitoring tool for hystrix. Through the Hystrix Dashboard, we can directly see the request response time, request success rate and other data of each Hystrix Command. But if you only use the Hystrix Dashboard, you can only see the service information in a single application, which is obviously not enough. We need a tool that allows us to aggregate the data of multiple services in the system and display it on the Hystrix Dashboard. This tool is Turbine.

1. Hystrix Dashboard

Create a new project, hystrix dashboard, using the eureka and producer projects mentioned in the previous article.

1. hystrix-dashboard pom.xml Dependency package management

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</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>

I will not talk about the packages mentioned above. I will talk about some packages that I have not seen before

  • Activator: this package is used for service monitoring. Many monitoring related functions will use this package. I will not talk about the specific content here, and there will be a special article about this later.
  • Hystrix dashboard: This is the protagonist today. Hystrix dashboard helps us encapsulate the monitoring panel of hystrix.

2. Starting hystrix DashboardApplication.java

package com.springcloud.hystrixdashboard;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class HystrixDashboardApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }

}

Start class add enable hytrix dashboard and fuse

Note: you should be aware that I have registered the hystrixmmetricstreamservlet here. Under the springboot1.x version, there is no need to register here. After the 2.x version, you need to register the hystrixmmetricstreamservlet here, and the access path is shown.

3. Configuration file

server:
  port: 8081
spring:
  application:
    name: spring-cloud-hystrix-dashboard
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
feign:
  hystrix:
    enabled: true

4. Test

It's time to test again. First, we put yesterday's eureka and producer CV s into today's working directory, start the service in sequence, and finally start the hystrix dashboard. After the startup, we visit: http://localhost:8081/hystrix, you will see the following interface:

[

](https://springcloud-oss.oss-c...

Here are some tips:

Cluster via Turbine (default cluster): http://turbine-hostname:port/turbine.stream

Cluster via Turbine (custom cluster): http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

Single Hystrix App: http://hystrix-app:port/hystrix.stream

In general, if the first url is used for the default cluster, the second url is used for the specified cluster, and the last url is used for the monitoring of a single application. For the moment, we only demonstrate the application of a single application. Therefore, in the input box, enter: http://localhost:8081/hystrix.stream , after input, click monitor to enter the page.

If there is no request, Loading will be displayed first , visit http://localhost :8081/hystrix.stream ping will also be displayed continuously.

Let's ask for a link we used yesterday: http://localhost:8081/hello/spring.

You'll see the statistics in a minute.

You can visit it again http://localhost :8081/hystrix.stream , as shown below:

ping: 

data: {"type":...}

data: {"type":...}

Indicates that the monitoring results have been returned

The monitoring page will be displayed as shown in the following figure:

[

](https://springcloud-oss.oss-c...

In fact http://localhost :8081/hystrix.stream The returned results are displayed graphically. The meaning of each indicator in the diagram is explained in detail on the Hystrix Dashboard Wiki, as shown in the following figure:

[

](https://springcloud-oss.oss-c...

The fuse monitoring for this single application has been completed.

2. Turbine

In a complex distributed system, hundreds or even thousands of nodes with the same service often need to be deployed. Many times, the operation and maintenance personnel hope to show the node status of the same service in the form of an overall cluster, so as to better grasp the status of the whole system. To this end, Netflix provides an open source project (Turbine) to provide multiple hystrix.stream The content of is aggregated into a data source for the Dashboard to display.

1. Turbine 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>turbine</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</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>

2. Configuration file

server:
  port: 8888
spring:
  application:
    name: hystrix-dashboard-turbine
turbine:
  app-config: node01,node02
  aggregator:
    cluster-config: default
  cluster-name-expression: new String("default")
  combine-host-port: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  • turbine.appConfig : configure the serviceId list in Eureka to indicate which services are monitored
  • turbine.aggregator.clusterConfig : specify which clusters to aggregate, multiple use "," split, default. Availablehttp:// ... / turbine.stream?cluster= One of {clusterconfig} access
  • turbine.clusterNameExpression : 1. clusterNameExpression specifies the cluster name, the default expression is appName; at this time: turbine.aggregator.clusterConfig 2. When clusterNameExpression: default, turbine.aggregator.clusterConfig 3. When the cluster name expression: When metadata ['cluster'], assume that the application you want to monitor is configured eureka.instance.metadata-map.cluster: ABC, you need to configure the turbine.aggregator.clusterConfig: ABC

3. Startup class

Start class add @ EnableTurbine, activate support for Turbine

package com.springcloud.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class TurbineApplication {

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

}

4. Create a consumer cluster

Copy 2 copies of yesterday's consumer copy to today's file path, change the name to consumers-node01, consumers-node02

Increased package dependency

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

Add hystrixmetrics streamservlet registration to the startup class

package com.springcloud.consumers;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumersApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

Note: the access path of hystrixmetrics streamservlet registered here is "/ actor"/ hystrix.stream ", which is because Turbine accesses this path by default.

You can modify the startup port number in the two configuration files, spring.application.name Modify to node01 and node02 respectively

5. Testing

Now launch the registration center, two consumers and Turbine in turn, visit two consumers in turn, and visit http://localhost:8888/turbine.stream Will return the same ping information as above.

And it will refresh continuously to obtain real-time monitoring data. The description is similar to a single monitoring, and the information of monitoring items is returned.

For graphical monitoring and viewing, input: http://localhost:8888/hystrix, return to the cool bear interface, enter: http://localhost:8888/turbine.stream , and then click Monitor Stream, you can see that there are two monitoring lists (this is the theoretical situation)

Note that the local environment cannot be tested successfully. Only one service can be displayed in the local environment. An error will be reported in the Turbine log

The software in your host has terminated an established connection.

The reason is not clear at present. Issues have been mentioned on github.

Topics: Java Spring Maven Apache