在消费者的rpc中添加
package com.pp.rpc;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import feign.hystrix.FallbackFactory;
@FeignClient(name = "APP-BANANA", fallbackFactory = MyFactory.class)
public interface DemoService {
@RequestMapping("/pp/Student/selectAll")
@ResponseBody
public Map<String, Object> listStudent();
}
@Component
class MyFactory implements FallbackFactory<DemoService> {
@Override
public DemoService create(Throwable arg0) {
return new DemoService() {
@Override
public Map<String, Object> listStudent() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("FALLBACK", "熔断器生效/业务繁忙 稍后请重试");
return map;
}
};
}
}
试验:当把生产者的名字故意改错时,消费者调用时
可以看到,熔断器生效了
application.properties配置熔断时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000(5秒)