通过hystrix可以解决雪崩效应问题,它提供了资源隔离、降级机制、融断、缓存等功能。接下来通过本文给大家分享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熔断内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
本文标题为:SpringCloud集成Hystrix熔断过程分步分解
基础教程推荐
- Java文件管理操作的知识点整理 2023-05-19
- java基础知识之FileInputStream流的使用 2023-08-11
- Java实现线程插队的示例代码 2022-09-03
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- java实现多人聊天系统 2023-05-19
- Java并发编程进阶之线程控制篇 2023-03-07
- Java实现查找文件和替换文件内容 2023-04-06
- Java数据结构之对象比较详解 2023-03-07
- springboot自定义starter方法及注解实例 2023-03-31
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02