feign+hystrix 实现服务降级

参考上一篇  feign + eureka 负载均衡

1:   消费端依赖    

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

2:  application 配置feign+hystrix

server:
  port: 8081
spring:
  application:
    name: cloud-order-service

eureka:
  instance:
    hostname: localhost
    port: 7001
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    #是否从eurekaServer抓取自己的注册信息,默认是true。
    #单节点无所谓,集群必须设置为true擦能配个ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka


feign:
  hystrix:
    enabled: true

ribbon:
  #指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ReadTimeout: 5000
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ConnectTimeout: 5000

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000 # 设置hystrix的超时时间为6000ms

logging:
  level:
    # feign日志以什么级别监控哪个接口
    com.atguigu.springcloud.service.FeignPaymentService: debug

3: main配置 hystrix

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
//@RibbonClient(name = "CLOUD-PAYMENT-SERVICE",configuration = MySelfRule.class)
public class FeignHistrixOrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(FeignHistrixOrderMain80.class,args);
    }
}

/**************************************************************    降级处理1           **********************************************************************************/

1)在消费端,controller类里面的方法头上添加降级处理

当指定类paymentTimeOutFallbackMethod时,会有限走到这个方法内去

    @Autowired
    PaymentHystrixSerice paymentHystrixSerice;

    @GetMapping("consumer/payment/histrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="10000")
    public String paymentInfoTimeOut(@PathVariable("id") Integer id){

//        int a=10/0;
        return paymentHystrixSerice.paymentInfoTimeOut(id);
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
    {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

/**************************************************************    降级处理2           **********************************************************************************/

1)在消费端,controller类头上添加降级处理  ,@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod"),并实现payment_Global_FallbackMethod方法

@RestController
@CrossOrigin
@RequestMapping("order")
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Autowired
    PaymentHystrixSerice paymentHystrixSerice;

    @HystrixCommand
    public String paymentInfoTimeOut(@PathVariable("id") Integer id){

//        int a=10/0;
        return paymentHystrixSerice.paymentInfoTimeOut(id);
    }
 
    // 下面是全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
    }

}

/**************************************************************    降级处理3           **********************************************************************************/

1): 写fallback   (超时,代码错误,宕机)

@Component
public class PaymentFallbackService  implements PaymentHystrixSerice{
    @Override
    public String paymentInfoOK(Integer id) {
        return "服务降级-----------paymentInfoOK";
    }

    @Override
    public String paymentInfoTimeOut(Integer id) {
        return "服务降级-----------paymentInfoTimeOut";
    }
}

2):  给接口添加fallback

@Service
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixSerice {


    @GetMapping("payment/histrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id);


    @GetMapping("payment/histrix/timeout/{id}")
    public String paymentInfoTimeOut(@PathVariable("id") Integer id);
}

以上3种降级处理优先级

降级处理1>降级处理2>降级处理3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值