这里写自定义目录标题
消费端和服务端同时加服务降级fallback,出现多次请求
在练习中遇到的问题:使用Hystrix对消费侧,服务侧同时加入fallback降级。
1.对消费侧降级要求是响应时长超过5s,则降级到另一个接口
2.对服务侧降级要求是响应时长超过5s,则降级到另一个接口
3.服务侧被降级接口被调用时方法内等待3s,之后返回信息。
4.通过请求消费者(被降级)接口,消费者接口通过OpenFeign服务调用服务方(被降级)接口。
5.按照逻辑5s内消费者会返回服务方被降级接口的返回,但实操情况是,日志中先打印服务方降级后接口的返回,之后打印两次服务方被降级接口返回。
1.消费侧代码
Controller
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000") //3秒钟以内就是正常的业务逻辑
})
public String paymentInfo_Timeout(@PathVariable("id") Integer id){
log.info("-----------------------");
String result = feignService.paymentInfo_TimeOut(id);
log.info("************result: "+result);
return result;
}
public String paymentInfo_TimeoutHandler(@PathVariable("id") Integer id){
log.info("--------------------------------------------------");
String result = feignService.paymentInfo_TimeoutHandler(id);
return result ;
}
Service
@GetMapping("/payment/hystrix/timeout/{id}")
String paymentInfo_TimeOut(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeouthandler1/{id}")
String paymentInfo_TimeoutHandler(@PathVariable("id") Integer id);
2.服务方代码
Controller
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_Timeout(@PathVariable("id") Integer id){
log.info("8001-----------------------------------"+id);
String result = paymentService.paymentInfo_Timeout(id);
log.info("************result: "+result);
return result;
}
@GetMapping("/payment/hystrix/timeouthandler1/{id}")
public String paymentInfo_TimeoutHandler1(@PathVariable("id") Integer id){
String result = paymentService.paymentInfo_TimeoutHandler1(id);
log.info("************result: "+result);
return result;
}
Service
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler1" ,commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "5000") //3秒钟以内就是正常的业务逻辑
})
public String paymentInfo_Timeout(Integer id){
System.out.println("---------------------------------------------8001");
long time = 4000;
// int age = 10/0;
try{
TimeUnit.MILLISECONDS.sleep(time);
// Thread.sleep(time);
}catch(InterruptedException e){
e.printStackTrace();
}
return "线程池: " + Thread.currentThread().getName() + "paymentInfo_TimeOut:耗时"+time+"ms,id: O(∩_∩)O" + id;
}
public String paymentInfo_TimeoutHandler1(Integer id){
return "线程池: " + Thread.currentThread().getName() + "系统繁忙, ┭┮﹏┭┮" ;
}
3.浏览器调用消费者接口
4.日志输出
2020-07-01 21:00:52.205 INFO 4076 --- [io-8001-exec-10] c.f.s.controller.PaymentController : 8001-----------------------------------2
---------------------------------------------8001
2020-07-01 21:00:53.211 INFO 4076 --- [nio-8001-exec-8] c.f.s.controller.PaymentController : 8001-----------------------------------2
2020-07-01 21:00:53.212 INFO 4076 --- [nio-8001-exec-3] c.f.s.controller.PaymentController : ************result: 线程池: http-nio-8001-exec-3系统繁忙, ┭┮﹏┭┮
---------------------------------------------8001
2020-07-01 21:00:56.210 INFO 4076 --- [io-8001-exec-10] c.f.s.controller.PaymentController : ************result: 线程池: hystrix-PaymentService-10paymentInfo_TimeOut:耗时4000ms,id: O(∩_∩)O2
2020-07-01 21:00:57.215 INFO 4076 --- [nio-8001-exec-8] c.f.s.controller.PaymentController : ************result: 线程池: hystrix-PaymentService-9paymentInfo_TimeOut:耗时4000ms,id: O(∩_∩)O2
2020-07-01 21:04:06.529 INFO 4076 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2020-07-01 21:09:06.530 INFO 4076 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration