基础配置
开发工具 :IDEA
MAVEN :3.6
JDK : 1.8
Spring boot :1.5.9.RELEASE
Spring cloud : Edgware.RELEASE
pox配置信息
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
在上的篇基础上给cloud_client2添加熔断回掉
- 给cloud_client2的pox中添加上述配置信息
- application.properties,中添加:
feign.hystrix.enabled=true
- 接口CloudClient1Feign的@FeignClient中添加fallback = “接口的实现”
package com.cloud.client2.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@FeignClient(name = "cloud-client1",fallback = CloudClient1Hystrix.class)
public interface CloudClient1Feign {
@RequestMapping(value = "/show/hello",method = RequestMethod.GET)
String showHello();
@RequestMapping(value = "/show/noHello",method = RequestMethod.GET)
String showNoHello();
}
- 接口CloudClient1Feign的实现方法,返回自己设置的参数
package com.cloud.client2.feign;
import org.springframework.stereotype.Component;
@Component
public class CloudClient1Hystrix implements CloudClient1Feign {
@Override
public String showHello() {
return "get show Hello,回掉了";
}
@Override
public String showNoHello() {
return "get no show hello,回掉了";
}
}
分别启动cloud_eureka,cloud_client1,cloud_client2,按上篇操作进行,Feign的调用没问题后,关闭cloud_client1
再次访问http://localhost:8082/to/getShow
得到的接口实现中的内容