电子商务平台源码请加企鹅求求:三五三六二四七二五九。继以前项目继续整合hystrix框架,hystrix框架为Netflix的模块,是一个容错框架。当用户访问服务调用者的时候,若是服务提供者出现异常致使没法正常返回出现请求超时的状况,而服务调用者并不知情,还在继续请求,这样会致使服务的崩溃。java
传统的解决办法:添加超时机制、人肉运维,而hystrix正是为解决这一问题,它在服务调用者与服务提供者之间增长了容错机制,当服务提供者没法提供服务的时候,服务调用者会根据设置的超时时间来阻断请求,并调用回退逻辑。spring
一、添加hystrix依赖bash
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
复制代码
二、在application.java类中加入@EnableCircuitBreaker断路器注解。 三、在controller中加入类设置@DefaultProperties超时时间和最大线程值,咱们为/hello接口增长一个3秒的线程阻塞,把hystrix的超时时间设置为2秒,让该方法走回退逻辑。接口方法上的@HystrixCommand中的fallbackMethod参数就是回退逻辑的方法名。helloFallback()方法为回退方法。app
@RestController
@DefaultProperties(groupKey = "hello-groupKey",
commandProperties = {
// 超时时间(毫秒)
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
},
threadPoolProperties = {
// 最大线程数
@HystrixProperty(name = "coreSize", value = "2")
})
public class MyRestController {
@Autowired
private IService iService;
@GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@HystrixCommand(fallbackMethod = "helloFallback", commandKey = "hello-commandKey")
public String hello() throws InterruptedException {
Thread.sleep(3000);
String hello = iService.hello();
return "hello: " + hello;
}
public String helloFallback() {
return "helloFallback";
}
}
复制代码