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ù)停下,別的客戶端再來(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í)間后客戶端再次調(diào)用成功后,一定時(shí)間內(nèi)成功率上去,系統(tǒng)的熔斷機(jī)制會(huì)慢慢的關(guān)閉,恢復(fù)到正常請(qǐng)求的狀態(tài)。
本篇接上一章直接改動(dòng)。
1.主啟動(dòng)類加上新的注解。
@EnableCircuitBreaker
2.service寫入新的熔斷控制方法
@Service
public class PaymentHystrixService {
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //是否開啟斷路器
@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
為了能夠更加直觀的看見服務(wù)訪問(wèn)的一些情況,配置下可視化的網(wǎng)頁(yè)觀察熔斷。
新建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>
主啟動(dòng)類
@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)類加上如下配置。
/**
* 此配置是為了服務(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設(shè)計(jì)模式中橋接模式應(yīng)用詳解
橋接,顧名思義,就是用來(lái)連接兩個(gè)部分,使得兩個(gè)部分可以互相通訊。橋接模式將系統(tǒng)的抽象部分與實(shí)現(xiàn)部分分離解耦,使他們可以獨(dú)立的變化。本文通過(guò)示例詳細(xì)介紹了橋接模式的原理與使用,需要的可以參考一下2022-11-11
SpringBoot單點(diǎn)登錄實(shí)現(xiàn)過(guò)程詳細(xì)分析
這篇文章主要介紹了SpringBoot單點(diǎn)登錄實(shí)現(xiàn)過(guò)程,單點(diǎn)登錄英文全稱Single?Sign?On,簡(jiǎn)稱就是SSO。它的解釋是:在多個(gè)應(yīng)用系統(tǒng)中,只需要登錄一次,就可以訪問(wèn)其他相互信任的應(yīng)用系統(tǒng)2022-12-12
教你如何在IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問(wèn)題)
這篇文章主要介紹了如何在?IDEA?中添加?Maven?項(xiàng)目的?Archetype(解決添加不起作用的問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08

