SpringCloud集成Hystrix熔斷過程分步分解
版本:
SpringBoot 2.6.1
SpringCloud 2021.0.0
依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.1.6.RELEASE</version> </dependency>
貼@EnableHystrix注解
@EnableHystrix @SpringBootApplication public class ConsumerOneApplication { public static void main(String[] args) { SpringApplication.run(ConsumerOneApplication.class, args); } }
在需要熔斷的接口上貼@HystrixCommand注解
@RequestMapping("/consumerOne") @RestController public class ControllerOne { @Autowired private RestTemplate restTemplate; private String providerOneName = "provider-one"; @HystrixCommand(fallbackMethod = "fallbackMethodOne") @RequestMapping("/serviceOne/{msg}") public String serviceOne(@PathVariable("msg") String msg) { return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class); } /** 熔斷回調方法 */ private String fallbackMethodOne(String msg) { return "熔斷一默認返回:" + msg; } }
異常熔斷測試
其中一個服務方拋異常,另一服務方正常返回
@RequestMapping("/providerOne") @RestController public class ControllerOne { @RequestMapping("/serviceOne/{msg}") public String serviceOne(@PathVariable("msg") String msg) { throw new RuntimeException(); // return "8702:" + msg; } }
@RequestMapping("/providerOne") @RestController public class ControllerOne { @RequestMapping("/serviceOne/{msg}") public String serviceOne(@PathVariable("msg") String msg) { return "8701:" + msg; } }
訪問結果:
超時熔斷測試
全局
先配置默認超時時間為3秒(default為全局)
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
在這里插入代碼片
其中一個接口睡眠4秒
@RequestMapping("/providerOne") @RestController public class ControllerOne { @RequestMapping("/serviceOne/{msg}") public String serviceOne(@PathVariable("msg") String msg) { try { Thread.sleep(4 * 1000); return "8702:" + msg; } catch (InterruptedException e) { e.printStackTrace(); } return "8702:" + msg; } }
訪問結果:
當服務方為延時返回的那個時,訪問等待了3秒返回熔斷默認對象
另一個正常
單個接口
除了全局外,另外給serviceTwo接口配置獨立的超時時間,其余不變
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
serviceTwo:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
增加一個路徑為serviceTwo的接口
@RequestMapping("/consumerOne") @RestController public class ControllerOne { @Autowired private RestTemplate restTemplate; private String providerOneName = "provider-one"; @HystrixCommand(fallbackMethod = "fallbackMethodOne") @RequestMapping("/serviceOne/{msg}") public String serviceOne(@PathVariable("msg") String msg) { return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class); } @HystrixCommand(fallbackMethod = "fallbackMethodOne") @RequestMapping("/serviceTwo/{msg}") public String serviceTwo(@PathVariable("msg") String msg) { return restTemplate.getForObject("http://" + providerOneName + "/providerOne/serviceOne/" + msg, String.class); } private String fallbackMethodOne(String msg) { return "熔斷一默認返回:" + msg; } }
測試結果:
一個為等待5秒返回結果
一個正常返回
到此這篇關于SpringCloud集成Hystrix熔斷過程分步分解的文章就介紹到這了,更多相關SpringCloud Hystrix熔斷內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
idea在用Mybatis時xml文件sql不提示解決辦法(提示后背景顏色去除)
mybatis的xml文件配置的時候,有時候會沒有提示,這讓我們很頭疼,下面這篇文章主要給大家介紹了關于idea在用Mybatis時xml文件sql不提示的解決辦法,提示后背景顏色去除的相關資料,需要的朋友可以參考下2023-03-03IDEA創(chuàng)建Maven項目一直顯示正在加載的問題及解決
這篇文章主要介紹了IDEA創(chuàng)建Maven項目一直顯示正在加載的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12