前幾章,我們講解了Eureka注冊中心、Ribbon和OpenFeign服務調(diào)用框架;今天開始講一個分布式微服務項目中很重要的內(nèi)容Hytrix服務降級框架。
盡管Hytrix官網(wǎng)停止更新了,但是Hytrix的設計理念和思想非常優(yōu)秀,其他服務降級框架的設計都是借鑒于它,可以說它是所有分布式微服務項目中服務降級使用的基石。
1、Hystrix熔斷器概述
2、HyStrix重要概念
- 服務降級fallback
比如當某個服務繁忙,不能讓客戶端的請求一直等待,應該立刻返回給客戶端一個備選方案(兜底的服務)
服務器忙,請稍后再試,不讓客戶端等待并立刻返回一個友好提示,fallback
哪些情況會發(fā)出降級:
- 程序運行異常
- 超時
- 服務熔斷觸發(fā)服務降級
- 線程池/信號量也會導致服務降級
- 服務熔斷break
當某個服務出現(xiàn)問題,卡死了,不能讓用戶一直等待,需要關(guān)閉所有對此服務的訪問,然后調(diào)用服務降級
類比保險絲達到最大服務訪問后,直接拒絕訪問,拉閘限電,然后調(diào)用服務降級的方法并返回友好提示
就是保險絲:服務的降級->進而熔斷->恢復調(diào)用鏈路
- 服務限流flowlimit
限流,比如秒殺場景,不能訪問用戶瞬間都訪問服務器,限制一次只可以有多少請求
秒殺高并發(fā)等操作,嚴禁一窩蜂的過來擁擠,大家排隊,一秒鐘N個,有序進行。
3、hystrix案例
3.1 新建模塊 Cloud-provider-hystrix-payment8001
- 建模塊 Cloud-provider-hystrix-payment8001
- 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">
<parent>
<artifactId>springcloud2023</artifactId>
<groupId>com.tigerhhzz.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Cloud-provider-hystrix-payment8001</artifactId>
<dependencies>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tigerhhzz.springcloud-tigerhhzz</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- yml文件
server:
port: 8001
spring:
application:
name: cloud-provider-hystrix-service
eureka:
client:
service-url:
# defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
defaultZone: http://eureka7001.com:7001/eureka/
- 主啟動類
package com.tigerhhzz.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author tigerhhzz
* @date 2023/4/12 9:41
*/
@SpringBootApplication
@EnableEurekaClient
@Slf4j
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
log.info("PaymentHystrixMain8001啟動成功~~~~~~~~~~~~~~~~~~~");
}
}
- 業(yè)務類
寫service:
package com.tigerhhzz.springcloud.service;
/**
* @author tigerhhzz
* @date 2022/6/15 9:16
*/
public interface PaymentService {
String getPaymentInfo_OK(Integer id);
String getPaymentInfo_Error(Integer id);
}
service實現(xiàn)類:
package com.tigerhhzz.springcloud.service.impl;
import com.tigerhhzz.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author tigerhhzz
* @date 2022/6/15 9:18
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
public String getPaymentInfo_OK(Integer id) {
return "當前線程池名稱: "+Thread.currentThread().getName()+"----getPaymentInfo_OK----,id: "+id;
}
@Override
public String getPaymentInfo_Error(Integer id) {
//System.out.println(Thread.currentThread().getName()+"-----------error----------"+id);
int timeout = 3;
//int age = 10/0;
try {
TimeUnit.SECONDS.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "當前線程池名稱: "+Thread.currentThread().getName()+"-----getPaymentInfo_Error----ERROR----"+id;
}
}
controller:
package com.tigerhhzz.springcloud.controller;
import com.tigerhhzz.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author tigerhhzz
* @date 2022/6/15 9:19
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/payment/hystrix/ok/{id}")
public String getPaymentInfo_OK(@PathVariable("id") Integer id){
String res = paymentService.getPaymentInfo_OK(id);
return res;
}
@GetMapping("/payment/hystrix/error/{id}")
public String getPaymentInfo_Error(@PathVariable("id") Integer id){
String res = paymentService.getPaymentInfo_Error(id);
return res;
}
}
啟動7001和8001模塊,測試正常
以上述為根基平臺,從正確–錯誤–降級熔斷–恢復。
通過jmeter壓力測試 20000個并發(fā)請求http://localhost:8001/payment/hystrix/error/1
- jmeter壓力測試設置
再次壓測2萬并發(fā),發(fā)現(xiàn)http://localhost:8001/payment/hystrix/ok/1訪問也變慢了,開始轉(zhuǎn)圈圈了。
此時使用壓測工具,并發(fā)20000個請求,請求會延遲的那個方法,
壓測中,發(fā)現(xiàn),另外一個方法并沒有被壓測,但是我們訪問它時,卻需要等待
這就是因為被壓測的方法它占用了服務器大部分資源,導致其他請求也變慢了
讓問題和錯誤更嚴重,建一個80消費模塊去訪問8001,80模塊感覺很崩潰~~~~~~~~
3.2 創(chuàng)建帶降級的order模塊 Cloud-comsumer-feign-hystrix-order80
-
建模塊
-
修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">
<parent>
<artifactId>springcloud2023</artifactId>
<groupId>com.tigerhhzz.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Cloud-comsumer-feign-hystrix-order80</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tigerhhzz.springcloud-tigerhhzz</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 改yml
server:
port: 80
spring:
application:
name: cloud-comsumer-feign-hystrix-order80
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
- 主啟動類
package com.tigerhhzz.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author tigerhhzz
* @date 2022/6/15 9:56
*/
@SpringBootApplication
@EnableFeignClients
@Slf4j
public class OrderFeignHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignHystrixMain80.class,args);
log.info("OrderFeignHystrixMain80啟動成功~~~~~~~~~~~~~~~~~~~");
}
}
- 遠程調(diào)用8001模塊的接口
package com.tigerhhzz.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author tigerhhzz
* @date 2022/6/15 9:59
*
* 微服務接口 + @feign注解
*/
@Component
@FeignClient(value = "cloud-provider-hystrix-service")
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String getPaymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/error/{id}")
public String getPaymentInfo_Error(@PathVariable("id") Integer id);
}
- controller
package com.tigerhhzz.springcloud.controller;
import com.tigerhhzz.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author tigerhhzz
* @date 2022/6/15 9:19
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String getPaymentInfo_OK(@PathVariable("id") Integer id){
String res = paymentService.getPaymentInfo_OK(id);
return res;
}
@GetMapping("/consumer/payment/hystrix/error/{id}")
public String getPaymentInfo_Error(@PathVariable("id") Integer id){
String res = paymentService.getPaymentInfo_Error(id);
return res;
}
}
- 測試
啟動80模塊,訪問8001,正常。
再次壓測2萬并發(fā),發(fā)現(xiàn)80訪問也變慢了
此時使用壓測工具,并發(fā)20000個請求,請求會延遲的那個方法,
壓測中,發(fā)現(xiàn),另外一個方法并沒有被壓測,但是我們訪問它時,卻需要等待
這就是因為被壓測的方法它占用了服務器大部分資源,導致其他請求也變慢了
3.3 配置服務降級:
3.3.1 服務降級 Cloud-provider-hystrix-payment8001模塊
8001先從自身找問題,設置自身調(diào)用超時時間的峰值,峰值內(nèi)可以正常運行,超過了需要有兜底的方法處理,做服務降級fallback。
具體配置步驟如下:
- 為service的指定方法(會延遲的方法)添加@HystrixCommand注解
一旦調(diào)用服務方法失敗并拋出錯誤信息后,會自動調(diào)用 @HystrixCommand標注好的fallbackMethod調(diào)用類中的指定方法
超過3秒,fallback服務降級。
package com.tigerhhzz.springcloud.service.impl;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tigerhhzz.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author tigerhhzz
* @date 2022/6/15 9:18
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
public String getPaymentInfo_OK(Integer id) {
return "當前線程池名稱: "+Thread.currentThread().getName()+"----getPaymentInfo_OK----,id: "+id;
}
@Override
@HystrixCommand(fallbackMethod = "getPaymentInfo_ErrorHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String getPaymentInfo_Error(Integer id) {
//System.out.println(Thread.currentThread().getName()+"-----------error----------"+id);
int timeout = 5;
//int age = 10/0;
try {
TimeUnit.SECONDS.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "當前線程池名稱: "+Thread.currentThread().getName()+"-----getPaymentInfo_Error----ERROR----"+id;
}
/*fallback服務降級后的兜底方法*/
public String getPaymentInfo_ErrorHandler(Integer id) {
return Thread.currentThread().getName()+"-----8001 allback服務降級后的兜底方法----超時或異常------getPaymentInfo_ErrorHandler----ERROR----"+id;
}
}
getPaymentInfo_ErrorHandler服務降級后的兜底方法
- 主啟動類上,添加激活hystrix的注解
package com.tigerhhzz.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author tigerhhzz
* @date 2023/4/12 9:41
*/
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@Slf4j
public class PaymentHystrixMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentHystrixMain8001.class,args);
log.info("PaymentHystrixMain8001啟動成功~~~~~~~~~~~~~~~~~~~");
}
}
先測試8001的自身加固,有沒有效果?
訪問:http://localhost:8001/payment/hystrix/error/1
結(jié)論:
上面故意制造兩個異常:
1,int timeout = 5;
2,我們能接受3秒鐘,它運行5秒鐘,超時異常
當前服務不可用了,做服務降級,兜底的方案都是方法 getPaymentInfo_ErrorHandler。
這是8001對自己做的保護,遇到異常做服務降級,進入兜底方法。
3.3.2 服務降級Cloud-comsumer-feign-hystrix-order80模塊
首先80模塊也要有個服務自我保護的機制,也需要服務降級保護
然后,80通過feign調(diào)用8001時
- 修改80的yml配置
添加在feign中開啟hystrix的配置
server:
port: 80
spring:
application:
name: cloud-comsumer-feign-hystrix-order80
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
feign:
hystrix:
enabled: true #在feign中開啟hystrix
- 改80啟動類
在啟動類中加上注解@EnableHystrix
package com.tigerhhzz.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author tigerhhzz
* @date 2022/6/15 9:56
*/
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@Slf4j
public class OrderFeignHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderFeignHystrixMain80.class,args);
log.info("OrderFeignHystrixMain80啟動成功~~~~~~~~~~~~~~~~~~~");
}
}
- 業(yè)務類
主要意思是:80模塊中的paymentInfo_ERROR接口也自身做個hystrix服務降級兜底方法。并設置超時時間峰值是1500毫秒。
@GetMapping("/consumer/payment/hystrix/error/{id}")
@HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
public String paymentInfo_ERROR(@PathVariable("id") Integer id){
//int age= 10/0;
return paymentHystrixService.getPaymentInfo_Error(id);
}
public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id){
return "80____paymentInfo_ERROR_Handler___異常返回,請檢查自己";
}
測試,訪問地址:http://localhost/consumer/payment/hystrix/error/1
上面有三個時間,需要注意一下。
第一個時間是:8001模塊getPaymentInfo_Error接口的峰值時間5000毫秒(8001自我保護的服務降級峰值時間)
第二個時間是:8001模塊getPaymentInfo_Error中的假設業(yè)務超時時間3000毫秒,允許3秒內(nèi)不進行服務降級
第三個時間是:80模塊的自我保護服務降級峰值時間1500毫秒。
3.3.3 Hystrix全局服務降級DefaultProperties
上述案例中,我們發(fā)現(xiàn)每個方法都需要服務降級的兜底方法,這樣代碼顯得很膨脹,業(yè)務重復的問題。
統(tǒng)一與自定義的分開。
案例是在80模塊的controller中設置如下:
package com.tigerhhzz.springcloud.controller;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tigerhhzz.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author tigerhhzz
* @date 2022/6/15 10:03
*/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "Payment_Global_Fallback_Handler")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String getPaymentInfo_OK(@PathVariable("id") Integer id) {
return paymentHystrixService.getPaymentInfo_OK(id);
}
@GetMapping("/consumer/payment/hystrix/error/{id}")
@HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
public String paymentInfo_ERROR(@PathVariable("id") Integer id){
//int age= 10/0;
return paymentHystrixService.getPaymentInfo_Error(id);
}
public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id){
return "80____paymentInfo_ERROR_Handler___異常返回,請檢查自己";
}
/*全局fallback方法 注意 這個全局兜底方法不能攜帶參數(shù) 這是我遇到的坑*/
public String Payment_Global_Fallback_Handler(){
return "80____Payment_Global_Fallback_Handler___異常返回";
}
}
這里我新加了一個接口方法:
@GetMapping("/consumer/payment/hystrix/error1/{id}")
@HystrixCommand
public String paymentInfo_ERROR1(@PathVariable("id") Integer id){
int age= 10/0;
return paymentHystrixService.getPaymentInfo_Error(id);
}
測試訪問地址:http://localhost/consumer/payment/hystrix/error1/1
進入全局服務降級的兜底方法。
3.3.3 Hystrix通配服務降級FeignFallback
微服務中,只要通過feign調(diào)用的接口上都有@FeignClient注解,換句話說:標有@FeignClient注解的接口中的所有方法做個統(tǒng)一的服務降級類,就可以實現(xiàn)接口服務的統(tǒng)一服務降級。
具體步驟:
- 新建一個類PaymentFallbackService實現(xiàn) 標注有@FeignClient注解的接口
統(tǒng)一為接口里面的方法進行異常處理。
package com.tigerhhzz.springcloud.service;
import org.springframework.stereotype.Component;
/**
* @author tigerhhzz
* @date 2022/6/15 19:16
*/
@Component
public class PaymentFallbackService implements PaymentHystrixService{
@Override
public String getPaymentInfo_OK(Integer id) {
return "80---PaymentFallbackService----payment/hystrix/ok";
}
@Override
public String getPaymentInfo_Error(Integer id) {
return "80----PaymentFallbackService---payment/hystrix/error";
}
}
- 修改80中的feign接口PaymentHystrixService
添加@FeignClient(value = “cloud-provider-hystrix-service”,fallback = PaymentFallbackService.class)
package com.tigerhhzz.springcloud.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author tigerhhzz
* @date 2022/6/15 9:59
*
* 微服務接口 + @feign注解
*/
@Component
@FeignClient(value = "cloud-provider-hystrix-service",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String getPaymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/error/{id}")
public String getPaymentInfo_Error(@PathVariable("id") Integer id);
}
- 測試訪問:
首先訪問http://localhost/consumer/payment/hystrix/ok/1
然后,關(guān)閉8001服務
再次訪問上述地址
此時服務端已經(jīng)宕機了,但是我們做了服務降級處理,讓客戶端在服務端不可用時也會獲得提示信息而不會掛起耗死服務器。
3.4 服務熔斷CircuitBreaker
熔斷機制是應對雪崩效應的一種微服務鏈路保護機制。
保險絲
在springcloud框架中,熔斷機制通過Hystrix實現(xiàn),Hystrix會健康微服務間調(diào)用的狀況
當失敗的調(diào)用到一定閾值,缺省是5秒內(nèi)20次調(diào)用的失敗,就會啟動熔斷機制。熔斷機制的注解是@HystrixCommand。
4.4.1 服務熔斷理論
三個狀態(tài) closed、open、halfopen(開、關(guān)、半開)
4.4.2 服務熔斷案例
修改Cloud-provider-hystrix-payment8001模塊
- PaymentServiceImpl:
//---服務的熔斷
@Override
@HystrixCommand(
fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否開啟斷路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //請求次數(shù)
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //時間窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//失敗率達到多少后跳閘
}
)
public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
if (id<0) {
throw new RuntimeException("******id不能為負數(shù)");
}
String simpleUUID = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\t" + "成功調(diào)用,流水號是:" + simpleUUID;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
return "id不能為負數(shù),請稍后再試............"+id;
}
- PaymentController
//服務熔斷
@GetMapping("info/circuit/{id}")
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
String res = paymentService.paymentCircuitBreaker(id);
log.info("-----res:" +res);
return res;
}
- 測試
自測Cloud-provider-hystrix-payment8001 啟動
正確 http://localhost:8001/info/circuit/1
錯誤 http://localhost:8001/info/circuit/-1
一次正確一次錯誤,遠遠沒有達到訪問10次失敗率6次以上的設置
此時,連續(xù)訪問-1 20多次,然后再訪問1時,發(fā)現(xiàn)剛開始調(diào)用鏈路還沒有恢復,等幾秒鐘后才訪問正常。
多次錯誤,然后慢慢正確,發(fā)現(xiàn)剛開始不滿足條件,隨著錯誤率下降后,才恢復正常。
服務的降級------進而熔斷----恢復調(diào)用鏈路
4.4.3 服務熔斷總結(jié)
當斷路器開啟后:
熔斷的整體流程:文章來源:http://www.zghlxwxcb.cn/news/detail-419688.html
請求進來,首先查詢緩存,如果緩存有,直接返回
如果緩存沒有,--->2
查看斷路器是否開啟,如果開啟的,Hystrix直接將請求轉(zhuǎn)發(fā)到降級返回,然后返回
如果斷路器是關(guān)閉的,
判斷線程池等資源是否已經(jīng)滿了,如果已經(jīng)滿了
也會走降級方法
如果資源沒有滿,判斷我們使用的什么類型的Hystrix,決定調(diào)用構(gòu)造方法還是run方法
然后處理請求
然后Hystrix將本次請求的結(jié)果信息匯報給斷路器,因為斷路器此時可能是開啟的
(因為斷路器開啟也是可以接收請求的)
斷路器收到信息,判斷是否符合開啟或關(guān)閉斷路器的條件,
如果本次請求處理失敗,又會進入降級方法
如果處理成功,判斷處理是否超時,如果超時了,也進入降級方法
最后,沒有超時,則本次請求處理成功,將結(jié)果返回給controller
其他參數(shù)文章來源地址http://www.zghlxwxcb.cn/news/detail-419688.html
到了這里,關(guān)于微服務+springcloud+springcloud alibaba學習筆記【Hystrix(豪豬哥)的使用】(6/9)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!