SpringCloud微服務(wù)熔斷器使用詳解
一、簡介
當(dāng)微服務(wù)中的某個子服務(wù),發(fā)生異常服務(wù)器宕機(jī),其他服務(wù)在進(jìn)行時不能正常訪問而一直占用資源導(dǎo)致正常的服務(wù)也發(fā)生資源不能釋放而崩潰,這時為了不造成整個微服務(wù)群癱瘓,進(jìn)行的保護(hù)機(jī)制 就叫做熔斷,是一種降級策略
熔斷的目的:保護(hù)微服務(wù)集群
二、作用
- 對第三方訪問的延遲和故障進(jìn)行保護(hù)和控制
- 防止復(fù)雜分布式系統(tǒng)中的雪崩效應(yīng)(級聯(lián)故障)
- 快速失敗,快速恢復(fù)
- 回退,盡可能優(yōu)雅的降級
- 啟用近實(shí)時監(jiān)控、報警和操作控制
三、核心概念
3.1 熔斷目的
應(yīng)對雪崩效應(yīng),快速失敗,快速恢復(fù)
3.2 降級目的
保證整體系統(tǒng)的高可用性
四、實(shí)例
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ù)約實(shí)現(xiàn)源碼分析詳解
- SpringCloud微服務(wù)剔除下線功能實(shí)現(xiàn)原理分析
- SpringCloud超詳細(xì)講解微服務(wù)網(wǎng)關(guān)Zuul基礎(chǔ)
- SpringCloud微服務(wù)開發(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)文章
Java筆記之從IO模型到Netty框架學(xué)習(xí)初識篇
Netty作為一個已經(jīng)發(fā)展了十多年的框架,已然非常成熟了,其中有大量的細(xì)節(jié)是普通使用者不知道或者不關(guān)心的,本文帶你查缺補(bǔ)漏掌握Netty的使用2022-03-03
Spring中@Configuration注解和@Component注解的區(qū)別詳解
這篇文章主要介紹了Spring中@Configuration注解和@Component注解的區(qū)別詳解,@Configuration 和 @Component 到底有何區(qū)別呢?我先通過如下一個案例,在不分析源碼的情況下,小伙伴們先來直觀感受一下這兩個之間的區(qū)別,需要的朋友可以參考下2023-09-09
Springboot如何基于assembly服務(wù)化實(shí)現(xiàn)打包
這篇文章主要介紹了Springboot如何基于assembly服務(wù)化實(shí)現(xiàn)打包,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
spring-boot中使用spring-boot-devtools的實(shí)現(xiàn)代碼
這篇文章主要介紹了spring-boot中使用spring-boot-devtools的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句詳解
這篇文章主要為大家詳細(xì)介紹了Java語法基礎(chǔ)之循環(huán)結(jié)構(gòu)語句,感興趣的小伙伴們可以參考一下2016-09-09
Java中BeanUtils.copyProperties基本用法與小坑
本文主要介紹了Java中BeanUtils.copyProperties基本用法與小坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Java中ArrayList和LinkedList有什么區(qū)別舉例詳解
這篇文章主要介紹了Java中ArrayList和LinkedList區(qū)別的相關(guān)資料,包括數(shù)據(jù)結(jié)構(gòu)特性、核心操作性能、內(nèi)存與GC影響、擴(kuò)容機(jī)制、線程安全與并發(fā)方案,以及工程實(shí)踐場景,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-02-02

