SpringCloud調研系列3.2:斷路器-Feign集成Hystrix

接上一篇的文章,若是這個時候把2個Computer服務都關閉,調用的結果以下:java

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中已經依賴了Hystrixweb

使用@FeignClient註解中的fallback屬性指定回調類spring

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類app

補上ide

建立回調類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;
	}
}

再重試下,結果是什麼?this

相關文章
相關標籤/搜索