SpingCloud 2020微服务教程【28】全局服务降级、通配服务降级

视频链接: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的降级配置生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值