接上一篇的文章,如果这个时候把2个Computer服务都关闭,调用的结果如下:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Dec 02 10:44:06 CST 2016
There was an unexpected error (type=Internal Server Error, status=500).
add timed-out and no fallback available.
我们不需要在Feigh工程中引入Hystix,Feign中已经依赖了Hystrix
使用@FeignClient注解中的fallback属性指定回调类
package i.a.c;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "compute-service", fallback = ComputeClientHystrix.class)
public interface ComputeClient {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b);
}
报错是因为目前还没有ComputeClientHystrix.class类
补上
创建回调类ComputeClientHystrix,实现@FeignClient的接口,此时实现的方法就是对应@FeignClient接口中映射的fallback函数。
package i.a.c;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;
@Component
public class ComputeClientHystrix implements ComputeClient {
@Override
public Integer add(@RequestParam(value = "a") Integer a, @RequestParam(value = "b") Integer b) {
return -9999;
}
}
再重试下,结果是什么?