Springcloud hystrix服務(wù)熔斷和dashboard如何實現(xiàn)
服務(wù)在經(jīng)過一定負(fù)荷之后,如果達(dá)到一定上限之后會中斷進(jìn)行報錯,而服務(wù)調(diào)用的方法也會報錯等等,一旦整體服務(wù)停下,別的客戶端再來訪問就會無法調(diào)用。對此需要進(jìn)行另外一種服務(wù)熔斷模式。
不同于現(xiàn)實中的熔斷保險絲,服務(wù)熔斷是在系統(tǒng)服務(wù)達(dá)到一定錯誤之后,自動熔斷降級,采取備用方法,但是在一定時間后客戶端再次調(diào)用成功后,一定時間內(nèi)成功率上去,系統(tǒng)的熔斷機制會慢慢的關(guān)閉,恢復(fù)到正常請求的狀態(tài)。
本篇接上一章直接改動。
1.主啟動類加上新的注解。
@EnableCircuitBreaker
2.service寫入新的熔斷控制方法
@Service public class PaymentHystrixService { @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = { @HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否開啟斷路器 @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //請求數(shù)達(dá)到后才計算 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠時間窗 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"), //錯誤率達(dá)到多少跳閘 }) public String paymentCirtuitBreaker(@PathVariable("id")Integer id){ if(id<0){ throw new RuntimeException("****id不能為負(fù)數(shù)"); } String randomNum= IdUtil.simpleUUID(); return Thread.currentThread().getName()+"\t"+"調(diào)用成功,編號"+randomNum; } public String paymentCircuitBreaker_fallback(@PathVariable("id")Integer id){ return "id不能為負(fù)數(shù),請稍后重試,o(╥﹏╥)o+"+id; }
此處hystrixCommand注解即是對熔斷的一些限制,一般是在10秒內(nèi)進(jìn)行10次有60%的訪問錯誤率就會進(jìn)行熔斷,自動啟動備用的方法,默認(rèn)5秒后有 正確的執(zhí)行結(jié)果就會慢慢恢復(fù)正常狀態(tài),關(guān)閉斷路器。
3.dashboard
為了能夠更加直觀的看見服務(wù)訪問的一些情況,配置下可視化的網(wǎng)頁觀察熔斷。
新建dashboard工程。
pom文件依賴
<dependencies> <dependency> <groupId>com.bai</groupId> <artifactId>cloud-api-common</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!--監(jiān)控--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!--eureka client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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>
主啟動類
@SpringBootApplication @EnableHystrixDashboard public class HystrixDashboard9001 { public static void main(String[] args) { SpringApplication.run(HystrixDashboard9001.class,args); } }
yml配置下端口即可。
訪問地址
http://localhost:9001/hystrix/
對于被監(jiān)控的服務(wù)需要額外的配置。新版本會有報錯需要在啟動類加上如下配置。
/** * 此配置是為了服務(wù)監(jiān)控而配置,與服務(wù)容錯本身無關(guān),springcloud升級后的坑 * ServletRegistrationBean因為SpringBoot的默認(rèn)路徑不是 “/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; }
本篇所有代碼均在GitHub:
https://github.com/MaTsukun/springcloud2020
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA裝飾者模式(從現(xiàn)實生活角度理解代碼原理)
裝飾者模式可以動態(tài)地給一個對象添加一些額外的職責(zé)。就增加功能來說,Decorator模式相比生成子類更為靈活。這篇文章主要介紹了JAVA裝飾者模式的相關(guān)資料,需要的朋友可以參考下2016-12-12Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析
這篇文章主要介紹了Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03SpringBoot3.x打包Docker容器的實現(xiàn)
這篇文章主要介紹了SpringBoot3.x打包Docker容器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11解決springboot jpa @Column columnDefinition等屬性失效問題
這篇文章主要介紹了解決springboot jpa @Column columnDefinition等屬性失效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10詳解Http請求中Content-Type講解以及在Spring MVC中的應(yīng)用
這篇文章主要介紹了Http請求中Content-Type講解以及在Spring MVC中的應(yīng)用的相關(guān)資料,需要的朋友可以參考下2017-02-02