SpringCloud调研系列3.2:断路器-Feign集成Hystrix

我怕爱的太早我们不能终老 提交于 2020-03-01 06:56:14

接上一篇的文章,如果这个时候把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;
	}
}

再重试下,结果是什么?

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!