第七章中在 ProductController 和 OrderController 中都使用了局部服务降级,但同时也导致两个问题, 通过观察两个局部降级的案例,可以发现:
- 每个业务方法都对应一个降级方法,会导致代码膨胀
- 业务逻辑方法和处理服务异常降级方法混在一起。
一、全局降级
- 每个业务方法都对应一个降级方法,会导致代码膨胀
为解决第一个问题即每个业务方法都对应一个降级方法,会导致代码膨胀,可以使用@DefaultProperties(defaultFallback = "") 进行全局服务降级。
使用 @DefaultProperties(defaultFallback = "") 将其它普通的统一跳转到统一处理结果页面,个别重要核心业务有专属,通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量。
以 订单服务 OrderController 为案例,讲解解决代码膨胀膨胀问题。将 OrderController 中所有的兜底方法都使用统一的一个全局兜底方法进行降级。对个别的方法例如 buy( ) 方法使用专属。过程如下:
1、定义全局降级方法
2、使用 @HystrixCommand 注解 指定专属
在专属方法上使用此注解指定此方法超时异常需要有降级兜底方法。其他方法使用注解@HystrixCommand 则使用全局的降级方法。
3、使用 @DefaultProperties 注解 指定全局降级
在订单服务的 OrderController 类上使用注解 @DefaultProperties 指定全局降级。
具体代码均在第七章基础上进行修改,代码如下:
import com.hwadee.springcloud.entity.Product;
import com.hwadee.springcloud.service.IOrderFeignService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "globalHystrixHandler")
public class OrderController {
@Autowired
IOrderFeignService orderFeignService;
@RequestMapping("/buy/{id}")
// 指定专属降级服务
@HystrixCommand(fallbackMethod = "buyTimeout", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
})
public Product buy(@PathVariable Long id) {
System.out.println("进入OrderController的buy方法, orderFeignService 准备调用远端接口 findById");
Product product = orderFeignService.findOrderById(id);
return product;
}
@RequestMapping(value = "/delete/{id}")
@HystrixCommand // 未指定专属则使用全局降级服务
public Product deleteOrderById(@PathVariable Long id) {
System.out.println("进入OrderController的deleteOrderById方法, orderFeignService 准备调用远端接口deleteOrderById");
int i =10/0;
Product product = orderFeignService.deleteOrderById(id);
return product;
}
// 专属降级服务
public Product buyTimeout(Long id) {
Product product = new Product();
product.setId(id);
product.setName("当前订单服务访问/order/buy/1 超时 进行专属服务降级:" + id);
return product;
}
// 全局降级服务
public Product globalHystrixHandler() {
Product product = new Product();
product.setName("当前订单服务访问/order/delete/1 10/0发生异常 进行全局服务降级:");
return product;
}
}
二、进行测试
分别访问:http://localhost:9000/order/buy/1 和 http://localhost:9000/order/delete/1 全局降级和专属降级。结果如下: