SpringCloud微服務(wù)熔斷器使用詳解
一、簡介
當(dāng)微服務(wù)中的某個子服務(wù),發(fā)生異常服務(wù)器宕機,其他服務(wù)在進(jìn)行時不能正常訪問而一直占用資源導(dǎo)致正常的服務(wù)也發(fā)生資源不能釋放而崩潰,這時為了不造成整個微服務(wù)群癱瘓,進(jìn)行的保護(hù)機制 就叫做熔斷,是一種降級策略
熔斷的目的:保護(hù)微服務(wù)集群
二、作用
- 對第三方訪問的延遲和故障進(jìn)行保護(hù)和控制
- 防止復(fù)雜分布式系統(tǒng)中的雪崩效應(yīng)(級聯(lián)故障)
- 快速失敗,快速恢復(fù)
- 回退,盡可能優(yōu)雅的降級
- 啟用近實時監(jiān)控、報警和操作控制
三、核心概念
3.1 熔斷目的
應(yīng)對雪崩效應(yīng),快速失敗,快速恢復(fù)
3.2 降級目的
保證整體系統(tǒng)的高可用性
四、實例
4.1 基于Hystrix
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
application
@EnableHystrix // 開啟熔斷
@SpringBootApplication
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
}4.1.1 熔斷觸發(fā)降級
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired
private RestTemplate restTemplate;
/**
* @param num 參數(shù)
* @return 字符串
*/
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "5"), // 默認(rèn)20, 最小產(chǎn)生5次請求
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // 熔斷時間, 該時間內(nèi)快速失敗
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 10s內(nèi)失敗率達(dá)到50%觸發(fā)熔斷
}, // 10s 內(nèi)最少 5 個請求, 如果失敗率大于 50% 則觸發(fā)熔斷
fallbackMethod = "fallback"
) // 服務(wù)調(diào)用失敗時,觸發(fā)熔斷進(jìn)行降級
@GetMapping("/test")
public String test(@RequestParam Integer num) {
if (num % 2 == 0) {
return "訪問正常";
}
List<?> list = restTemplate.getForObject("http://localhost:7070/product/list", List.class);
assert list != null;
return list.toString();
}
/**
* 熔斷時觸發(fā)降級
*
* @param num 參數(shù)
* @return 字符串
*/
private String fallback(Integer num) {
// 降級操作
return "系統(tǒng)繁忙";
}
}4.1.2 超時觸發(fā)降級
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired
private RestTemplate restTemplate;
@HystrixCommand(
commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "500")
},
fallbackMethod = "timeout"
)
@GetMapping("/timeout")
public String timeoutTest(){
return restTemplate.getForObject("http://localhost:7070/product/list", String.class);
}
/**
* 超時時觸發(fā)降級
*
* @return 字符串
*/
private String timeout() {
// 降級操作
return "請求超時";
}
}4.1.3 資源隔離觸發(fā)降級
平臺隔離、業(yè)務(wù)隔離、部署隔離
線程池隔離、信號量隔離
4.2 基于OpenFeign pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application
@SpringBootApplication
@ComponentScan(basePackages = {
"com.study.provider.service", // feign 包目錄
"com.study.hystrix" // 當(dāng)前模塊啟動目錄
})
@EnableFeignClients(basePackages = "com.study.provider.service") // feign 目錄
public class HystrixApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixApplication.class, args);
}
}HystrixFeignController
@RestController
@RequestMapping("/hystrixFeign")
public class HystrixFeignController {
@Qualifier("com.study.provider.service.ProductionService")
@Autowired
ProductionService productService; // feign Client
@GetMapping("test")
public String test(){
return productService.getProduction(1).toString(); // 遠(yuǎn)程調(diào)用
}
}feign Client
@FeignClient(value = "provider", fallback = ProductionFallback.class) // fallback 用于熔斷
public interface ProductionService {
@RequestMapping("/product/getProduction")
Object getProduction(@RequestParam Integer id);
}
ProductionFallback
@Component
public class ProductionFallback implements ProductionService {
@Override
public Object getProduction(Integer id) {
return "請求失敗";
}
@Override
public Map createProduction(Object production) {
return new HashMap<>();
}
@Override
public Object selectByProduct(Object product) {
return "請求失敗";
}
}到此這篇關(guān)于SpringCloud微服務(wù)熔斷器使用詳解的文章就介紹到這了,更多相關(guān)SpringCloud熔斷器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud微服務(wù)續(xù)約實現(xiàn)源碼分析詳解
- SpringCloud微服務(wù)剔除下線功能實現(xiàn)原理分析
- SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)
- SpringCloud微服務(wù)開發(fā)基于RocketMQ實現(xiàn)分布式事務(wù)管理詳解
- SpringCloud微服務(wù)熔斷器Hystrix使用詳解
- SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Gateway
- SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul
- SpringCloud微服務(wù)中跨域配置的方法詳解
相關(guān)文章
關(guān)于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案
這篇文章主要介紹了關(guān)于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Springboot定時任務(wù)Scheduled重復(fù)執(zhí)行操作
這篇文章主要介紹了Springboot定時任務(wù)Scheduled重復(fù)執(zhí)行操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
解決spring.thymeleaf.cache=false不起作用的問題
這篇文章主要介紹了解決spring.thymeleaf.cache=false不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
springboot配置文件中使用${}注入值的兩種方式小結(jié)
這篇文章主要介紹了springboot配置文件中使用${}注入值的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java任務(wù)定時執(zhí)行器案例的實現(xiàn)
定時器會執(zhí)行指定的任務(wù),本文主要介紹了Java任務(wù)定時執(zhí)行器案例的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
MyBatis Mapper XML中比較操作符轉(zhuǎn)義問題解決
在使用MyBatis編寫Mapper XML時,有時會遇到比較操作符需要進(jìn)行轉(zhuǎn)義的情況,本文主要介紹了MyBatis Mapper XML中比較操作符轉(zhuǎn)義問題解決,具有一定的參考價值,感興趣的可以了解一下2024-01-01

