1. 降低配置
@HystrixCommand
8001先从自身找问题:
设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,作服务降级fallback
1.1 8001fallback
@HystrixCommand报异常后如何处理
一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法

上图故意制造异常
接收3秒钟 ,运行5秒钟,超时异常
当前服务不可用了(超时异常,运行异常),做服务降级,默认的兜底方案都是paymentInfo_TimeOutHandler
主启动类激活
添加新注解@EnableCircuitBreaker
测试
http://localhost:8001/payment/hystrix/timeout/1
1.2 80fallback
80订单微服务,也可以更好的保护自己,自己也依样画葫芦进行客户端降级保护。
- yml
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
- 主启动
Spring Cloud(十):Hystrix服务降级 - 业务类
- OrderHystrixController
package com.lele.springcloud.controller;
import com.lele.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author: lele
* @date: 2021/3/20 17:45
* @description:
*/
@RestController
@Slf4j
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "paymentInfoFallbackMethod", commandProperties = {
@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1500")
})
public String paymentInfo_TimeOut(Integer id) {
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentInfoFallbackMethod(Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错,请检查自己,(╥╯^╰╥)";
}
}
目前问题
每个业务方法对应一个兜底的方法,代码膨胀
统一和自定义的分开
解决问题
除了个别的核心业务有专属,其他普通的都可以通过@DefaultProperties(defaultFallback = “”)统一跳转到统一处理界面。
- controller
package com.lele.springcloud.controller;
import com.lele.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* @author: lele
* @date: 2021/3/20 17:45
* @description:
*/
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
// @HystrixCommand(fallbackMethod = "paymentInfoFallbackMethod", commandProperties = {
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1500")
// })
@HystrixCommand
public String paymentInfo_TimeOut(Integer id) {
int age = 10/0;
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String paymentInfoFallbackMethod(Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错,请检查自己,(╥╯^╰╥)";
}
// 下面是全局 fallback 方法
public String payment_Global_FallbackMethod() {
return "Global异常处理信息,请稍候再试~~";
}
}
服务降级,客户端去调用服务端,碰上服务端宕机或关闭.
本次案例服务降级处理是在客户端80实现完成的,与服务端8001没有关系,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦。
未来我们要面对的异常:
- 运行
- 超时
- 宕机
修改cloud-consumer-feign-hystrix-order80
根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里面的方法进行异常处理
- PaymentFallbackService 类实现PaymentHystrixService接口
package com.lele.springcloud.service;
import org.springframework.stereotype.Component;
/**
* @author: lele
* @date: 2021/3/21 8:44
* @description:
*/
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfo_OK(Integer id) {
return "------PaymentFallbackService fall back-paymentInfo_OK------";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "------PaymentFallbackService fall back-paymentInfo_TimeOut------";
}
}
- PaymentHystrixService
Spring Cloud(十):Hystrix服务降级
测试
单个eureka先启动7001
PaymentHystrixMain8001启动
正常访问测试
故意关闭微服务8001
客户端自己调用提示
此时服务端provider已经down了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器