關(guān)于Hystrix的監(jiān)控及可視化面板
Hystrix監(jiān)控
Hystrix除了實(shí)現(xiàn)容錯之外,還提供了近乎實(shí)時(shí)的監(jiān)控。Hystrix Command和HystrixObservableCommand在執(zhí)行時(shí),會會生成執(zhí)行結(jié)果和運(yùn)行指標(biāo),比如每秒的請求數(shù)和成功數(shù)等,這些監(jiān)控?cái)?shù)據(jù)對于分析系統(tǒng)請求的調(diào)用情況很有用。
我們以之前項(xiàng)目介紹過的micro-service-consumer-ribbon-hystrix為例,因?yàn)橹暗捻?xiàng)目中已經(jīng)包含了spring-boot-starter-actuator和spring-cloud-starter-hystrix,因此可以直接通過http://localhost:7908/hystrix.stream來訪問,但是頁面展示的并不直觀。
那么為什么之前介紹的micro-service-consumer-movie-feign-hystrix-fallback-factory打開http://localhost:8028/hystrix.stream卻是404呢?因?yàn)樵谝雈eign的時(shí)候帶了hystrix,因此只需要再單獨(dú)引入hystrix即可,同時(shí)還需要在啟動類上加@EnableCircuitBreaker。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
使用Hystrix Dashboard 數(shù)據(jù)可視化監(jiān)控面板
創(chuàng)建micro-service-hystrix-dashboard項(xiàng)目,首先來看一下這個項(xiàng)目的pom文件,我們引入了spring-cloud-starter-hystrix-dashboard的依賴:
<?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> <artifactId>micro-service-hystrix-dashboard</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.fanfan.cloud</groupId> <artifactId>micro-service-spring-cloud-study</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>micro-service-hystrix-dashboard</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
接下來是編寫啟動類,在啟動類上添加@EnableHystrixDashboard注解:
@SpringBootApplication @EnableHystrixDashboard public class HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(HystrixDashboardApplication.class, args); } }
application.yml的配置文件為:
server: port: 8030
由配置可知,我們并沒有把HystrixDashboard注冊到Eureka上。
訪問http://localhost:8030/hystrix,可看到如下的畫面,輸入http://localhost:8028/hystrix.stream地址查看監(jiān)控?cái)?shù)據(jù)。
我們也可以將dashboard注冊到Eureka:
server: port: 8030 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} spring: application: name: hystrix-dashboard
使用Turbine聚合監(jiān)控?cái)?shù)據(jù)
上面我們使用/hystrix.stream端點(diǎn)監(jiān)控單個微服務(wù)實(shí)例,然而微服務(wù)架構(gòu)的應(yīng)用系統(tǒng)一般會有若干個微服務(wù),如果每次只能查看單個實(shí)例的監(jiān)控?cái)?shù)據(jù),就要不停地切換Hystrix Dashboard的監(jiān)控地址,非常不便,因此我們引入Hystrix監(jiān)控?cái)?shù)據(jù)聚合工具Turbine,它可以將所有相關(guān)的/hystrix.stream端點(diǎn)的數(shù)據(jù)聚合到一個組合的/turbine.stream中,從而使得集群監(jiān)控更加方便。
創(chuàng)建一個項(xiàng)目micro-service-hystrix-turbine,需要引入spring-cloud-starter-turbine的依賴,如下所示:
<?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"> <!-- 引入spring boot的依賴 --> <modelVersion>4.0.0</modelVersion> <artifactId>micro-service-hystrix-turbine</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.fanfan.cloud</groupId> <artifactId>micro-service-spring-cloud-study</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>micro-service-hystrix-turbine</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
在啟動類上添加@EnableTurbine注解,如下所示:
@SpringBootApplication @EnableTurbine public class TurbineApplication { public static void main(String[] args) { SpringApplication.run(TurbineApplication.class, args); } }
接下來是application.yml配置文件:
server: port: 8031 spring: application: name: hystrix-turbine eureka: client: serviceUrl: defaultZone: http://discovery:8761/eureka/ instance: prefer-ip-address: true # 這里的名字是spring.application.name turbine: appConfig: cunsumer-ribbon-custom-hystrix,cunsumer-movie-feign-hystrix-fallback clusterNameExpression: "'default'"
接下來我們分別啟動micro-service-discovery-eureka、micro-service-provider-user、micro-service-consumer-ribbon-hystrix、micro-service-consumer-movie-feign-hystrix-fallback、micro-service-hystrix-dashboard、micro-service-hystrix-turbine,然后訪問http://localhost:8030/hystrix, 輸入http://localhost:8031/turbine.stream查看監(jiān)控?cái)?shù)據(jù):
使用消息中間件收集數(shù)據(jù)
在微服務(wù)和Turbine網(wǎng)絡(luò)不通的情況下,可借助消息中間件進(jìn)行數(shù)據(jù)收集。
各個微服務(wù)將Hystrix Command的監(jiān)控?cái)?shù)據(jù)發(fā)送至消息中間件,Turbine去消費(fèi)消息中間件的數(shù)據(jù)。
作者書上使用的消息隊(duì)列使用RabbitMQ,需要安裝并啟動,先安裝Erlang OTP。
安裝完成后,在sbin目錄下(或者將sbin目錄配置到環(huán)境變量,然后執(zhí)行 rabbitmq-plugins enable rabbitmq_management 啟動服務(wù)
配置完成后,先執(zhí)行stop再執(zhí)行start。
啟動后打開如下的頁面:localhost:15672,用戶名和密碼都是guest。
項(xiàng)目中使用的是rabbitmq,要注意rabbitmq和activemq不可以同時(shí)開啟。否則回報(bào)錯。
com.rabbitmq.client.MalformedFrameException: AMQP protocol version mismatch; we are version 0-9-1, server sent signature 3,1,0,0
創(chuàng)建消息隊(duì)列相關(guān)的項(xiàng)目
復(fù)制micro-service-consumer-ribbon-hystrix,修改為micro-service-consumer-ribbon-hystrix-turbine-mq項(xiàng)目,pom文件中引入spring-cloud-netflix-hystrix-stream和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> <artifactId>micro-service-consumer-ribbon-hystrix-turbine-mq</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.fanfan.cloud</groupId> <artifactId>micro-service-spring-cloud-study</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-hystrix-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies> <!-- 引入spring cloud的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>micro-service-consumer-ribbon-hystrix-turbine-mq</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置文件application.yml文件:
server: port: 7910 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}} spring: application: name: cunsumer-ribbon-custom-hystrix rabbitmq: host: localhost port: 5672 username: guest password: guest
以上監(jiān)控?cái)?shù)據(jù)發(fā)送到消息隊(duì)列就修改完了。
然后我們創(chuàng)建項(xiàng)目micro-service-hystrix-turbine-mq,需要引入spring-cloud-starter-turbine-stream和spring-cloud-starter-stream-rabbit的依賴,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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>micro-service-hystrix-turbine-mq</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.fanfan.cloud</groupId> <artifactId>micro-service-spring-cloud-study</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies> <build> <finalName>micro-service-hystrix-turbine-mq</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build> </project>
將@EnableTurbine修改為@EnableTurbineStream,修改配置文件application.yml文件:
server: port: 8032 spring: application: name: robot-hystrix-turbine-mq rabbitmq: host: localhost port: 5672 username: guest password: guest eureka: client: serviceUrl: defaultZone: http://discovery:8761/eureka/ instance: prefer-ip-address: true
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot @Autowired和@Resource的區(qū)別解析
@Resource是JDK 提供的注解,只是Spring 在實(shí)現(xiàn)上提供了這個注解的功能支持,本文給大家介紹Springboot @Autowired和@Resource的區(qū)別,感興趣的朋友一起看看吧2025-04-04Java面試題 從源碼角度分析HashSet實(shí)現(xiàn)原理
這篇文章主要介紹了Java面試題 從源碼角度分析HashSet實(shí)現(xiàn)原理?,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)
這篇文章主要介紹了JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Java timezone設(shè)置和mybatis連接數(shù)據(jù)庫時(shí)區(qū)設(shè)置方式
這篇文章主要介紹了Java timezone設(shè)置和mybatis連接數(shù)據(jù)庫時(shí)區(qū)設(shè)置方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09SpringBoot整合SpringSecurity和JWT的示例
這篇文章主要介紹了SpringBoot整合SpringSecurity和JWT的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06解決spring結(jié)合mybatis時(shí)一級緩存失效的問題
這篇文章主要介紹了解決spring結(jié)合mybatis時(shí)一級緩存失效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11