環境:
<java.version>1.8</java.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
<lcn.last.version>4.2.1</lcn.last.version>
一.首先說在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儀表盤的微服務都需要添加依賴
<!--熔斷器 健康檢查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--熔斷器 Dashboard--> <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>
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; } }
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文件依賴有這些
<!--熔斷器 健康檢查--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--熔斷器 Dashboard--> <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-netflix-turbine</artifactId> </dependency>
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/
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); } }
4.啟動Turbine服務,並且分別啟動 注冊中心,ms-member服務,ms-integral服務,ms-goods服務,並訪問調用服務的接口,真正的feign調用 調通一次。
5.訪問Turbine的圖形化訪問界面
http://sxd:10000/hystrix
如上圖,輸入查看各個服務的圖形化展示地址:
http://sxd:10000/turbine.stream
進入之后既可以看到集中展示的 圖形化儀表盤監控頁面