SpringCloud——服务降级三种实现

一、fallback

  1. yml

    server:
      port: 80
    
    spring:
      application:
        name: cloud-provider-hystrix-order
    
    eureka:
      client:
        register-with-eureka: true    #示表不向注册中心注册自己
        fetch-registry: true   #表示自己就是注册中心,职责是维护服务实例,并不需要去检索服务
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/
    
    ####################以下配置需要新增(ribbon相关配置和Feign超时相关)##################
    #全局配置
    # 请求连接的超时时间 默认的时间为 1 秒
    ribbon:
      ConnectTimeout: 5000
      # 请求处理的超时时间
      ReadTimeout: 5000
    
    # feign 配置
    feign:
      hystrix:
        enabled: true
    
    # hystrix 配置
    hystrix:
      command:
        default:
          execution:
            isolation:
              thread:
                timeoutInMilliseconds: 3000
    
  2. service

    @Component
    @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentHystixServiceFallBackImpl.class)
    public interface PaymentHystrixService {
        @GetMapping("/payment/hystrix/ok/{id}")
        String paymentInfo_OK(@PathVariable("id") Integer id);
    
        @GetMapping("/payment/hystrix/timeout/{id}")
        String paymentInfo_TimeOut(@PathVariable("id") Integer id);
    
    }
    
  3. fallbackImpl

    @Component
    public class PaymentHystixServiceFallBackImpl implements PaymentHystrixService {
    
        @Override
        public String paymentInfo_OK(Integer id) {
            return "服务降级";
        }
    
        @Override
        public String paymentInfo_TimeOut(Integer id) {
            return "服务降级";
        }
    
    }
    

二、fallbackFactory

  1. yml同上

  2. service

    @Component
    @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallbackFactory = PaymentHytrixServiceFallBackFactory.class)
    public interface PaymentHystrixService {
        @GetMapping("/payment/hystrix/ok/{id}")
        String paymentInfo_OK(@PathVariable("id") Integer id);
    
        @GetMapping("/payment/hystrix/timeout/{id}")
        String paymentInfo_TimeOut(@PathVariable("id") Integer id);
    
    }
    
  3. fallbackFactory

    @Component
    public class PaymentHytrixServiceFallBackFactory implements FallbackFactory<PaymentHystrixService> {
    
        @Override
        public PaymentHystrixService create(Throwable throwable) {
            return new PaymentHystrixService() {
                @Override
                public String paymentInfo_OK(Integer id) {
                    return "服务降级";
                }
    
                @Override
                public String paymentInfo_TimeOut(Integer id) {
                    return "服务降级";
                }
            };
        }
    }
    

三、两者结合

  1. fallbackImpl同一

  2. 修改fallbackFactory

    @Component
    public class PaymentHytrixServiceFallBackFactory implements FallbackFactory<PaymentHystrixService> {
    
        @Override
        public PaymentHystrixService create(Throwable throwable) {
            PaymentHystixServiceFallBackImpl paymentHystixServiceFallBack = new PaymentHystixServiceFallBackImpl();
            return paymentHystixServiceFallBack;
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值