SpringCloud微服務熔斷器使用詳解
一、簡介
當微服務中的某個子服務,發(fā)生異常服務器宕機,其他服務在進行時不能正常訪問而一直占用資源導致正常的服務也發(fā)生資源不能釋放而崩潰,這時為了不造成整個微服務群癱瘓,進行的保護機制 就叫做熔斷,是一種降級策略
熔斷的目的:保護微服務集群
二、作用
- 對第三方訪問的延遲和故障進行保護和控制
- 防止復雜分布式系統(tǒng)中的雪崩效應(級聯(lián)故障)
- 快速失敗,快速恢復
- 回退,盡可能優(yōu)雅的降級
- 啟用近實時監(jiān)控、報警和操作控制
三、核心概念
3.1 熔斷目的
應對雪崩效應,快速失敗,快速恢復
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"), // 默認20, 最小產(chǎn)生5次請求 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000"), // 熔斷時間, 該時間內(nèi)快速失敗 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50"), // 10s內(nèi)失敗率達到50%觸發(fā)熔斷 }, // 10s 內(nèi)最少 5 個請求, 如果失敗率大于 50% 則觸發(fā)熔斷 fallbackMethod = "fallback" ) // 服務調(diào)用失敗時,觸發(fā)熔斷進行降級 @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è)務隔離、部署隔離
線程池隔離、信號量隔離
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" // 當前模塊啟動目錄 }) @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(); // 遠程調(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 "請求失敗"; } }
到此這篇關于SpringCloud微服務熔斷器使用詳解的文章就介紹到這了,更多相關SpringCloud熔斷器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案
這篇文章主要介紹了關于Spring多數(shù)據(jù)源TransactionManager沖突的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Springboot定時任務Scheduled重復執(zhí)行操作
這篇文章主要介紹了Springboot定時任務Scheduled重復執(zhí)行操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09解決spring.thymeleaf.cache=false不起作用的問題
這篇文章主要介紹了解決spring.thymeleaf.cache=false不起作用的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06springboot配置文件中使用${}注入值的兩種方式小結(jié)
這篇文章主要介紹了springboot配置文件中使用${}注入值的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03MyBatis Mapper XML中比較操作符轉(zhuǎn)義問題解決
在使用MyBatis編寫Mapper XML時,有時會遇到比較操作符需要進行轉(zhuǎn)義的情況,本文主要介紹了MyBatis Mapper XML中比較操作符轉(zhuǎn)義問題解決,具有一定的參考價值,感興趣的可以了解一下2024-01-01