版本:
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熔断内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!