服务降级
降级配置
@HystrixCommand
8001先从自身找问题
设置自身调用超时时间的峰值,峰值内可以正常运行,超过了需要有兜底的方法处理,做服务降级
8001fallback
业务类启用
/**
* 超时访问,演示降级
* @param id
* @return
*/
@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})
public String paymentInfo_TimeOut(Integer id){
//int age = 10/0;
int timeNumber = 5;
try {
TimeUnit.SECONDS.sleep(timeNumber);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "线程池: " + Thread.currentThread().getName() + " paymentInfo_TimeOut" + id + "\t" + "haha~~" + "耗时(秒)" + timeNumber + "降级";
}
public String paymentInfo_TimeOutHandler(Integer id){
return "线程池: " + Thread.currentThread().getName() + " 系统繁忙或者运行报错,请稍后再试id: " + id + "\t" + "my god";
}
@HystrixCommand报异常后如何处理
一旦调用服务方法失败并抛出错误信息后,会自动调用@HystrixCommand
标注好的fallbackMethod 调用类中的指定方法
主动类激活
添加@EnableCircuitBreaker
注解
80fallback
80订单微服务,可更好的保护自己,进行客户端降级
题外话,我们自己配置过的热部署方式对java代码改动明显,但对@HystrixCommand
内属性的修改建议重启微服务
YML
server:
port: 80
eureka:
client:
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka
#服务降级
feign:
hystrix:
enabled: true
主启动
添加@EnableHystrix
业务类
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
@HystrixCommand(fallbackMethod = "PaymentTimeOutFallbackMethod",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
})
//@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
//int age = 10/0;
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String PaymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
目前问题
- 每个业务方法对应一个兜底方法,代码膨胀
- 统一和自定义分开
@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 = "PaymentTimeOutFallbackMethod",commandProperties = {
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
// })
@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
//int age = 10/0;
String result = paymentHystrixService.paymentInfo_TimeOut(id);
return result;
}
public String PaymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
//下面是全局fallback方法
public String payment_Global_FallbackMethod(){
return "Global异常处理信息,请稍后再试~~";
}
}
服务降级,客户端去调用服务端,碰上服务端宕机或关闭
本次案例是在客户端80实现完成的,与服务端8001没有关系,只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦。
未来面对的异常
- 运行
- 超时
- 宕机
根据cloud-consumer-feign-hystrix-order80已有PaymentHystrixService接口,重新新建一个类(PaymentFallbackService)实现该接口,统一为接口里的方法进行异常处理
PaymentFallbackService
@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
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TimeOut(@PathVariable("id") Integer id);