环境:

1.82.0.4.RELEASEFinchley.SR14.2.1

 

 

一.首先说在spring cloud的每个微服务中配置Hystrix Dashboard

>Dashboard代表仪表盘,作用是用于展示微服务之间调用时的监控。

>这里说明的是服务间进行feign调用时,微服务配置Dashboard的步骤

>这里说的是,在每一个业务服务上添加Dashboard的步骤,不是单独抽离出来一个Dashboard服务

>配置Dashboard之前,微服务直接按已经完成了feign的调用,并且已经在feignClient上设置了熔断器

 

【ms-member服务(port:9000) 调用 ms-integral服务(port:9002)】

【ms-member服务是服务调用方,通过feign调用ms-integral服务,ms-integral是服务提供方】

 

1.首先每一个需要配置Dashboard仪表盘的微服务都需要添加依赖

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.cloud

spring-cloud-starter-netflix-hystrix-dashboard

 

2.启动类上添加注解

@EnableCircuitBreaker

@EnableHystrixDashboard//展示熔断器仪表盘

 

3.spring boot2.0以后,不提供 hystrix.stream节点,需要自己添加【可以不加@Service放在启动类,也可以加上@Service或者@Component放在一个单独的文件中,只要能注入spring中为Bean即可】

【解决:Hystrix仪表盘Unable to connect to Command Metric Stream的问题,就是这一步骤以上的这些都配置了即可解决这个问题】

/**

* SpringBoot2.0以后,不提供 hystrix.stream节点,需要自己增加

*/

@Service

public class HystrixStreamServlet {

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

}

}

View Code

 

 

 

4.配置完成,即可分别启动ms-member和ms-integral服务, 启动后:

ms-member服务显示如下:

 

此时访问地址:

http://localhost:9000/hystrix

即可访问ms-member这个服务的熔断器仪表盘

 

在输入框输入:

http://localhost:9000/hystrix.stream

点击按钮

 

 

 

【解决:Hystrix仪表盘Loading...的问题】

跳转进来发现Hystrix仪表盘Loading...

原因:是因为并没有进行feign调用ms-intergral,所以暂时没有记录

直接访问http://localhost:9000/hystrix.stream 也可以发现一直在ping:

 

此时,可以访问一下ms-member中调用ms-integral服务的一个接口:

http://localhost:9000/member/save

本接口即保存会员,并且 调用ms-intergral 保存会员的原始积分记录。

 

调用feign的接口访问后,就可以看到

 

 

同理,按上面的步骤访问ms-integral服务的Hystrix Dashboard,因为ms-integral服务并没有调用别的服务的feign,所以它的依旧是loading...并且ping:一直没有消息反馈。

 

最后附一张说明

 

 

 

二.来说一说搭建Turbine 

 在使用Hystrix Dashboard组件监控服务的熔断器状况时,每个服务都有一个Hystrix Dashboard主页,服务数量过多时,监控非常不方便。Netflix开源了另一个组件Turbine,用于聚合多个Hystrix Dashboard,将数据显示在一个页面上,集中监控。

 新建一个Turbine服务,用于集中展示各个服务的feign调用的情况,也就是上面的各个服务的Hystrix Dashboard仪表盘。

 

【ms-member服务在一个接口中  分别调用ms-integral服务和ms-goods服务】

【ms-member port:9000】

【ms-integral port:9002】

【ms-goods port:9001】

 

1.pom.xml文件依赖有这些

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-netflix-hystrix

org.springframework.cloud

spring-cloud-starter-netflix-hystrix-dashboard

org.springframework.cloud

spring-cloud-starter-netflix-turbine

 

2.application.properties配置文件【注意,配置文件中turbine.instanceUrlSuffix=hystrix.stream 中配置的地址,是上面一中为每一个服务添加的spring boot2.0后需要自己添加的HystrixStreamServlet】

spring.application.name=springcloud-ms-hystrix-turbine

server.port=10000

eureka.client.service-url.defaultZone=http://127.0.0.1:8000/eureka/

#turbine特定配置

#配置eureka中的服务列表,标明监控哪些服务

turbine.appConfig=springcloud-ms-integral,springcloud-ms-member,springcloud-ms-goods

#指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://.../turbine.stream?cluster={clusterConfig之一}访问

turbine.aggregator.clusterConfig= default

turbine.cluster-name-expression="default"

# 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称

# 2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default

# 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC

#此处和每一个被监控服务中配置的HystrixStreamServlet自动加载Bean中配置的一样

turbine.instanceUrlSuffix=hystrix.stream

#因为parent的pom.xml中 添加了连接数据库的依赖,所以 需要配置数据库连接相关配置

spring.datasource.continue-on-error=false

spring.datasource.url=jdbc:mysql://localhost:3306/springcloudtest?useSSL=false&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#txmanager地址

tm.manager.url=http://127.0.0.1:7000/tx/manager/

View Code

 

3.启动类添加注解

package com.swapping.springcloud.ms.hystrix.turbine;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.netflix.turbine.EnableTurbine;

import org.springframework.cloud.openfeign.EnableFeignClients;

/**

*

* IP:turbine服务所在服务器IP localhost

* port:turbine服务所配置的服务端口 10000

* 监控项目访问: http://IP:port/turbine.stream

* 展示信息:

* ping:

* {.....}

*

*

*

* 图形化监控页面:http://IP:port/hystrix

*

* 图形化监控页面使用说明:

* 1.在进入豪猪主页后,在输入框输入http://localhost:10000/turbine.stream,点击Monitor Stream按钮

* 2.展示所有配置了Hystrix Dashboard仪表盘展示的 各个服务之间的feign调用情况

*/

@EnableTurbine//开启turbine

@EnableHystrixDashboard//开启仪表盘

@EnableDiscoveryClient

@SpringBootApplication

public class SpringcloudMsHystrixTurbineApplication {

public static void main(String[] args) {

SpringApplication.run(SpringcloudMsHystrixTurbineApplication.class, args);

}

}

View Code

 

4.启动Turbine服务,并且分别启动 注册中心,ms-member服务,ms-integral服务,ms-goods服务,并访问调用服务的接口,真正的feign调用 调通一次。

 

 5.访问Turbine的图形化访问界面

http://sxd:10000/hystrix

 

如上图,输入查看各个服务的图形化展示地址:

http://sxd:10000/turbine.stream

进入之后既可以看到集中展示的 图形化仪表盘监控页面

 

查看原文