系列文章目录
1、父工程创建
2、支付模块构建和热部署
3、消费者订单模块
4、服务注册中心-Eureka
5、zookeeper没学习
6、服务注册中心-Consul
7、Eureka、Consul异同
8、服务调用-Ribbon
9、服务调用-OpenFeign
10、服务降级-Hystrix
11、服务降级-Hystrix(二)
12、服务熔断-Hystrix
13、服务网关-Gateway
14-17 在git上做配置中心,没有学习
17、请求链路跟踪-Sleuth
18、Spring Cloud Alibaba-Nacos注册中心与配置中心
19、Spring Cloud Alibaba-Nacos集群和持久化配置
20、Sentinel流控
21、Sentinel熔断降级、热点key限流
22、SentinelResource配置
23、Sentinel 服务熔断与持久化
1. 存在的问题
上一篇文章中存在的问题:
- 每个方法都需要一个服务降级的方法,代码膨胀
- 业务逻辑方法与处理服务降级的方法混在一起,
2. 解决方法
有一个类似全局的处理方法,针对特殊要求编写特殊的处理。通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量
2.1 代码膨胀问题解决
使用@DefaultProperties(defaultFallback = “”)注解
2.1.1 注解使用方法
- @DefaultProperties(defaultFallback = “方法名”),标注在Controller类上,设置一个默认的处理方法。
- 在需要处理的controller方法上使用@hystrixCommand注解
@hystrixCommand注解:如果没有特别指明处理方法就用默认的处理方法。
2.1.2 注意点
默认的处理方法不能有参数,否则会报错
@RestController
@RequestMapping("consumer")
@Slf4j
@DefaultProperties(defaultFallback = "paymentInfo_TimeoutHandler")//配置默认处理方法
public class OrderController {
@Resource
private PaymentFeignService feignService;
@GetMapping("/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
return feignService.paymentInfo_OK(id);
}
@GetMapping("/hystrix/timeout/{id}")
@HystrixCommand//没有特别标注,使用默认处理方法
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
return feignService.paymentInfo_TimeOut(id);
}
//善后方法,这是默认的处理方法,不能够有参数
public String paymentInfo_TimeoutHandler(){
return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
}
}
2.2 代码耦合问题解决
代码耦合问题,本次降级在客户端cloud-consumer-feign-hyxtrix-order80完成
只需要为feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦。
修改cloud-consumer-feign-hyxtrix-order80
- 已经存在PaymentFeignService接口(就是上一步讲的feign客户端定义的接口)
- 重新建一个类(PaymentFallBackService)实现该接口,统一为接口中的方法进行异常处理直接在实现类的实现的方法中写降级逻辑
@Component
public class PaymentFallBackService implements PaymentFeignService{
@Override
public String paymentInfo_OK(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_OK ,o(╥﹏╥)o";
}
@Override
public String paymentInfo_TimeOut(Integer id) {
return "-----PaymentFallbackService fall back-paymentInfo_TimeOut ,o(╥﹏╥)o";
}
}
- 然后在PaymentHystrixService接口类上的@FeignClient注解属性fallback添加实现类
@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",//告诉应该去找哪一个微服务
fallback = PaymentFallBackService.class)//fallback的实现类
public interface PaymentFeignService {
@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);
}
3. 测试
3.1 服务降级的三种实现方式:
- @HystrixCommand注解,直接写在Controller方法上
@HystrixCommand(fallbackMethod = "paymentInfo_TimeoutHandler",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="4000")})
- @HystrixCommand+@DefaultProperties
@DefaultProperties(defaultFallback = "paymentInfo_TimeoutHandler")
public class OrderController {
......
@GetMapping("/hystrix/timeout/{id}")
@HystrixCommand
public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
return feignService.paymentInfo_TimeOut(id);
}
......
}
- @FeignClient方式,写接口实现类,然后再接口的注解中添加的方式
3.2 优先级
经过测试,优先级是:
1 > 3
2 > 3
因为1是特定的返回,2是默认的返回所以1和2没什么可比较的