Spring Cloud学习笔记(八)熔断器数据聚合监控Turbine

上一篇内容学习了Hystrix Dashboard查看熔断信息,通常情况下,查看单个的Hystrix熔断信息意义不大,需要把信息聚合到一起,这个时候就需要用到Turbine了,本篇学习内容,基于上一篇学习代码,我们在添加一个Service-Consumer项目,具体内容可以下载工程代码,分别命名为Service-Consumer-A和Service-Consumer-B。在建一个Service-Turbine工程,主要用来显示聚合监控数据。

首先在新建模块中引入POM依赖:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.dothwinds</groupId>
		<artifactId>spring-cloud-study</artifactId>
		<version>1.0.0</version>
	</parent>
	<groupId>org.dothwinds</groupId>
	<artifactId>spring-cloud-study-service-turbine</artifactId>
	<version>1.0.0</version>
	<name>spring-cloud-study-service-turbine</name>

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

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

</project>

 然后在项目启动类上,加入注解@EnableTurbine

package org.dothwinds.serviceconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableTurbine
public class SpringCloudStudyServiceConsumerApplication {

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

}

然后我们来修改配置文件application.yml文件,加入turbine的配置内容

server:
  port: 11000
spring:
  application:
    name: service-turbine #应用名称,会显示在eureka server中

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka/ #eureka server地址

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

#turbine配置信息
turbine:
  #需要聚合监控的应用名称,多个以逗号隔开
  app-config: service-cosumer-a,service-cosumer-b
  aggregator:
    #聚合哪些集群,默认为 default
    clusterConfig: default
  #集群名称为 default
  clusterNameExpression: new String("default")

分别启动Eureka和两个Service-Consumer服务,还有Service-Turbine的服务:

接下来进入Turbine的监控界面,http://localhost:11000/turbine.stream返回如下信息:

分别访问Service-Consumer的地址:http://localhost:10001/testLoadBalancehttp://localhost:10002/testLoadBalance,这两个地址多刷新几次,然后打开http://localhost:11000/hystrix,输入http://localhost:11000/turbine.stream,点击Monitor Stream按钮,成功返回:

扫描二维码关注公众号,回复: 10240992 查看本文章

遇到的问题:

启动Service-Turbine服务之后,访问/actuator/hystrix.stream报了404错误,返回类似的如下错误信息

[{"timestamp":"2020-03-26T13:02:10.935+0000","status":404,"error":"Not Found","message":"No message available","path":"/actuator/hystrix.stream"}]

 这个时候我们需要把监控的服务都在启动类中加上下面的代码,否则默认是找不到的(原因在此:https://blog.csdn.net/ddxd0406/article/details/79643059

@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;
}

参考资料:https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/single/spring-cloud.html

代码:https://gitee.com/dothwinds/Spring-Cloud-Study/tree/master/spring-cloud-study-turbine

发布了18 篇原创文章 · 获赞 33 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Dothwinds/article/details/105112152