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

