一、添加依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
2.2.5.RELEASE
二、配置文件开启feign对hystrix的支持:
feign:
hystrix:
enabled: true
三、启动类添加注解@EnableHystrix
四、编写@FeignClient接口
package cn.edu.tju.service;
import cn.edu.tju.component.FeignDemoServiceFallback;
import cn.edu.tju.config.MyConfig2;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "EUREKA-SERVICE-PROVIDER" /*, configuration = MyConfig2.class*/ , fallback = FeignDemoServiceFallback.class)
public interface FeignDemoService {
@RequestMapping("/hello")
String helloWorld();
}
五、编写接口对应的降级实现类:
package cn.edu.tju.component;
import cn.edu.tju.service.FeignDemoService;
import org.springframework.stereotype.Component;
@Component
public class FeignDemoServiceFallback implements FeignDemoService {
@Override
public String helloWorld() {
return "my fallback......";
}
}