视频链接:2020最新版SpringCloud框架开发教程-周阳
文章源码:https://github.com/geyiwei-suzhou/cloud2020/
问题:
- 每个业务方法都写一个fallback方法,代码冗余
- 和业务逻辑混合在一起,比较混乱
解决:
在接口上使用统一服务降级配置@DefaultProperties
注解,除了个别重要核心业务专属,其他普通的可以通过@DefaultProperties(defaultFallback = “”)跳转到统一处理结果页面
cloud-consumer-feign-hystrix-order80模块
全局服务降级
修改类:com.antherd.springcloud.controller.OrderHystrixController
package com.antherd.springcloud.controller;
import com.antherd.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 javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallback") // 添加DefaultProperties
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@Value("${server.port}")
private String serverPort;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfoOk(@PathVariable("id") Integer id) {
String result = paymentHystrixService.paymentInfoOk(id);
log.info("*****result***** : " + result);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
// @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandle", commandProperties = {
// @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
// })
@HystrixCommand // 需要服务降级的方法上仅添加@HystrixCommand注解
public String paymentInfoTimeout(@PathVariable("id") Integer id) {
int age = 10/0; // 添加异常代码,测试全局服务降级
return paymentHystrixService.paymentInfoTimeout(id);
}
public String paymentInfoTimeoutHandle(@PathVariable("id") Integer id) {
return "我是消费者80,对方支付系统繁忙请10秒后再试或者自己运行出错请检查自己,┭┮﹏┭┮";
}
public String paymentGlobalFallback() {
return "Global异常处理信息,请稍后重试,o(╥﹏╥)o";
}
}
启动:cloud-eureka-server7001,cloud-provider-hystrix-payment8001,cloud-consumer-feign-hystrix-order80三个模块
访问:http://localhost/consumer/payment/hystrix/timeout/1 发现服务降级到paymentGlobalFallback方法
通配服务降级
新建通配降级服务类:com.antherd.springcloud.service.PaymentFallbackService
package com.antherd.springcloud.service;
import org.springframework.stereotype.Component;
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String paymentInfoOk(Integer id) {
return "----- PaymentFallbackService fall back-----paymentInfoOk, o(╥﹏╥)o";
}
@Override
public String paymentInfoTimeout(Integer id) {
return "----- PaymentFallbackService fall back-----paymentInfoTimeout, o(╥﹏╥)o";
}
}
在com.antherd.springcloud.service.PaymentHystrixService的@FeignClient注解中添加fallback类
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
修改pom文件添加如下配置
feign:
hystrix:
enabled: true
启动:cloud-eureka-server7001,cloud-provider-hystrix-payment8001,cloud-consumer-feign-hystrix-order80三个模块
访问:http://localhost/consumer/payment/hystrix/ok/1可以正常访问。
现在,停止cloud-provider-hystrix-payment8001模块
再访问:http://localhost/consumer/payment/hystrix/ok/1,发现在paymentInfoOk方法上没有添加降级配置,PaymentFallbackService的降级配置生效