一.pom依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>//将所有的feign调用的接口单独写个模块
<groupId>org.example</groupId>
<artifactId>feign-client-intf</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
二.启动类
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class HystrixFallbackApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(HystrixFallbackApplication.class)
.web(WebApplicationType.SERVLET)
.run(args);
}
}
1.降级类
①.继承调用的接口,并编写降级类,等到降级时调用
@FeignClient(name = "feign-client",fallback = Fallback.class)
public interface Myservice extends IService{
}
②. 降级类
@Slf4j
@Component
public class Fallback implements Myservice {
@Override
public String sayHi() {
return null;
}
@Override
public Friend sayHiPost(Friend friend) {
return null;
}
@Override
public String retry(@RequestParam(name = "timeout") int timeout) {
return "you are late!";
}
@Override
public String error() {
log.info("Fallback");
return "Fallback: sb";
}
}
三.配置
获取调远程方法的信息
System.out.println(Feign.configKey(Myservice.class,Myservice.class.getMethod("retry", int.class)));
--------------------------------
Myservice#retry(int)
# 开启Feign下面的Hystrix功能
feign.hystrix.enabled=true
# 是否开启服务降级
hystrix.command.default.fallback.enabled=true
# 全局超时
hystrix.command.default.execution.timeout.enabled=true
# 超时时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=1000
# 超时以后终止线程
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 取消的时候终止线程
hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=true
#具体方法
hystrix.command.MyService#retry(int).execution.isolation.thread.timeoutInMilliseconds=3000
四.缓存功能
Cleanup :用完之后自动关闭,可用于流的操作
@GetMapping("/cache")
public Friend cache(String name) {
@Cleanup HystrixRequestContext context = HystrixRequestContext.initializeContext();
Friend friend = requestCacheService.requestCache(name);
friend = requestCacheService.requestCache(name);
return friend;
}
@Slf4j
@Service
public class RequestCacheService {
@Autowired
private Myservice service;
@CacheResult
@HystrixCommand(commandKey = "cacheKey")
public Friend requestCache(@CacheKey String name) {
log.info("request cache" + name);
Friend friend = new Friend();
friend.setName(name);
friend = service.sayHiPost(friend);
log.info("after requesting cache" + name);
return friend;
}
}
五.多重降级规则
@Override
@HystrixCommand(fallbackMethod = "fallback2")
public String error() {
log.info("Fallback");
throw new RuntimeException("first fallback");
}
@HystrixCommand(fallbackMethod = "fallback3")
public String fallback2() {
log.info("fallback again");
throw new RuntimeException("fallback again");
}
public String fallback3() {
log.info("fallback again and again");
return "success";
}
六.Turbine聚合信息