SpringCloud HystrixDashboard服務監(jiān)控詳解
hystrixDashboard服務監(jiān)控
除了隔離依賴服務的調(diào)用以外,Hystrix還提供了準實時的調(diào)用監(jiān)控(Hystrix Dashboard),Hystrix會持續(xù)地記錄所有通過Hystrix發(fā)起的請求的執(zhí)行信息,并以統(tǒng)計報表和圖形的形式展示給用戶,包括每秒執(zhí)行多少請求多少成功,多少失敗等。Netflix通過hystrix-metrics-event-stream項目實現(xiàn)了對以上指標的監(jiān)控。Spring Cloud也提供了Hystrix Dashboard的整合,對監(jiān)控內(nèi)容轉化成可視化界面。
1、新建cloud-consumer-hystrix-dashboard9001
儀表盤監(jiān)控模塊
2、修改pom.xml文件引入儀表盤依賴
核心依賴:spring-cloud-starter-netflix-hystrix-dashboard
注意:所有的圖形化展示,都需要引入spring-boot-starter-actuator
依賴,在8001、8002上都需要引入
<?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"> <parent> <artifactId>springcloud2022</artifactId> <groupId>com.zcl.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumer-hystrix-dashboard9001</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <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.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
3、添加YAML配置文件
server:
port: 9001
4、建立啟動類
必須要加上@EnableHystrixDashboard
注解激活
package com.zcl.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /** * 描述:儀表盤啟動類 */ @SpringBootApplication @EnableHystrixDashboard public class HystrixDashboardMain9001 { public static void main(String[] args) { SpringApplication.run(HystrixDashboardMain9001.class, args); } }
5、啟動項目
啟動項目測試:http://localhost:901/hystrix
使用方法:在下面頁面中輸入需要進行監(jiān)控的地址即可
斷路器演示監(jiān)控
監(jiān)控8001注意事項
1、必須要有如下的兩個依賴
<!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、對啟動類的修改
注意:新版本Hystrix需要在主啟動類MainAppHystrix8001中指定監(jiān)控路徑,否則會出現(xiàn)報錯
package com.zcl.springcloud; 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.context.annotation.Bean; /** * 描述:熔斷限流啟動類 */ @SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker public class PaymentHystrixMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentHystrixMain8001.class, args); } /** *此配置是為了服務監(jiān)控而配置,與服務容錯本身無關,springcloud升級后的坑 *ServletRegistrationBean因為springboot的默認路徑不是"/hystrix.stream", *只要在自己的項目里配置上下面的servlet就可以了 */ @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; } }
監(jiān)控測試
啟動7001Eureka服務中心
觀察監(jiān)控窗口
9001監(jiān)控8001
8001地址測試
先訪問正確地址,再訪問錯誤地址,再正確地址,會發(fā)現(xiàn)圖示斷路器都是慢慢放開的。
http://localhost:8001/payment/circuit/31:正常的訪問
http://localhost:8001/payment/circuit/-31:異常訪問
監(jiān)控狀態(tài)
到此這篇關于SpringCloud HystrixDashboard服務監(jiān)控詳解的文章就介紹到這了,更多相關SpringCloud HystrixDashboard內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java中使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別
這篇文章主要介紹了使用Thread類和Runnable接口實現(xiàn)多線程的區(qū)別,本文給大家介紹了兩種實現(xiàn)方式的步驟,除了以上兩種多線程實現(xiàn)方式,還可以使用 Callable 接口實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07Spring AI 使用本地 Ollama Embeddings的操作方法
使用 OpenAI 的 Embeddings 接口是有費用的,如果想對大量文檔進行測試,使用本地部署的 Embeddings 就能省去大量的費用,所以我們嘗試使用本地的 Ollama Embeddings,這篇文章主要介紹了Spring AI 使用本地 Ollama Embeddings,需要的朋友可以參考下2024-05-05