一、feign调用方testA-service
1.调用方接口
public interface TestAService {
@RequestMapping(value = "test/handle", method = RequestMethod.POST)
public String handle(@RequestBody TestDTO test);
}
2.调用方feign接口
@FeignClient(name="testB-service",fallback = TestFeignFallback.class)
public interface TestAFeign extends TestAService{
}
3.调用方回调用 接口
@Component
public class TestAFeignFallback implements TestAFeign{
@Override
public String handle(Test testDTO) {
log.info("调用testB-service-TestBService-handle 服务失败,降级处理 {}", testDTO);
return "调用失败-熔断";
}
}
4.测试类
public class Test{
private static final Logger logger = LoggerFactory.getLogger(Test.class);
@Autowired
private TestAFeign testAFeign;
@Test
public String test() {
logger.info("请求开始");
String rest = testAFeign.handle(new Test());
logger.info("请求结束");
logger.info("请求结果:{}", rest);
}
}
二、被调用服务testB-service
1、补调用方服务接口
public interface TestBService {
@RequestMapping(value = "/handle",method = RequestMethod.POST)
public ResponseEntity handle(@RequestBody Test testDTO);
}
2.被调用方接口实现类
@RestController
@RequestMapping("/test")
public class TestBServiceImpl implements TestBService{
private static final Logger logger = LoggerFactory.getLogger(TestBServiceImpl .class);
@Override
public String handle(@RequestBody Test testDTO) {
logger.info("业务处理请求-参数{}。", testDTO);
try {
Thread.sleep(5000);
} catch (Exception e) {
logger.error("异常业务处理请求-异常。", e);
}
return "业务处理成功";
}
}
三、设置熔断时间
在调用方:testA服务的配置文件中添加以下配置
hystrix.command.TestAFeign#handle(Test).execution.isolation.thread.timeoutInMilliseconds=6000
TestAFeign:feign接口名
handle: 设置熔断时间的方法名
四、注意事项
hystrix.command.TestAFeign#handle(Test).execution.isolation.thread.timeoutInMilliseconds设置的时间必须小于ribben设置的时间,因为如果大于ribben设置的时间会被ribben先熔断。