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