zoukankan      html  css  js  c++  java
  • spingcloud(6)hystrix服务降级

    众多的服务串在一起,虽然避免了耦合性,但却形成了闭合链,只要其中一环失效,就会造成整个服务死锁,这个时候hystrix就可以发挥作用了,将服务降级,

    采用替代的方案来顶替出现错误的方法,避免整个链路中断。

    fallback是对于整个应用来说的,服务提供者和消费者端都可以进行配置,但一般都会配置在客户端,对于用户进行反馈。

    1.服务提供者。

    yml文件都是常规配置

    server:
      port: 8001
    spring:
      application:
        name: cloud-provider-hystrix-payment
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/

    启动类加上断路器注解

    @EnableCircuitBreaker

    service测试类

    @Service
    public class PaymentHystrixService {
        public String paymentinfo_ok(Integer id){
            return "线程池:"+Thread.currentThread().getName()+"paymentinfo_ok,id:"+id+"	"+"成功!!!!";
        }
        @HystrixCommand(fallbackMethod = "paymentinfo_Timeouthander",commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
        })
       public String paymentinfo_timeout(Integer id){
            int timenum=5;
           try {
               TimeUnit.SECONDS.sleep(timenum);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           return "线程池:"+Thread.currentThread().getName()+"paymentinfo_timeout,id:"+id+"	"+"失败!!!!耗时(秒):"+timenum;
       }
       public String paymentinfo_Timeouthander(@PathVariable("id") Integer id){
           return "线程池:"+Thread.currentThread().getName()+"系统繁忙,请稍后重试,id:"+id+"	"+"失败!!!!o(╥﹏╥)o";
       }
    }
    View Code

    controller采用feign配合调用接口,服务提供者端也可以采用hystrix进行服务降级。

    @RestController
    @Slf4j
    public class PaymentHystrixController {
        @Resource
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping(value = "/payment/hystrix/ok/{id}")
        public String payment_ok(@PathVariable("id")Integer id){
            String result = paymentHystrixService.paymentinfo_ok(id);
            log.info("*****result:"+result);
             return result;
        }
        @GetMapping(value = "/payment/hystrix/timeout/{id}")
        public String payment_timeout(@PathVariable("id")Integer id){
            String result = paymentHystrixService.paymentinfo_timeout(id);
            log.info("*****result:"+result);
            return result;
        }
    }
    View Code

    2.客户端消费者

    yml文件常规配置需要加上新的feign,hystrix配置

    server:
      port: 80
    #这里只把feign做客户端用,不注册进eureka
    eureka:
      client:
        #表示是否将自己注册进EurekaServer默认为true
        register-with-eureka: false
        fetch-registry: true
        service-url:
          #defaultZone: http://localhost:7001/eureka
          defaultZone: http://eureka7001.com:7001/eureka/
    feign:
      hystrix:
        enabled: true
    View Code

    采用openfeign调用接口,创建service接口,fallback后续介绍

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

    启动类常规配置,加上hystrix注解

    @SpringBootApplication
    @EnableFeignClients
    @EnableHystrix

    controller

    @RestController
    @Slf4j
    public class OrderController {
        @Resource
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
        public String payment_ok(@PathVariable("id")Integer id){
            String result = paymentHystrixService.payment_ok(id);
            return result;
        }
        @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
        /*@HystrixCommand(fallbackMethod = "paymentinfo_Timeouthander",commandProperties = {
                @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
        })*/
        public String payment_timeout(@PathVariable("id")Integer id){
            String result = paymentHystrixService.payment_timeout(id);
            return result;
        }
     
    }

    在面对服务降级的处理中,随着服务的增多,越来越多的方法需要配置一个独立的fallback,而且在同一个类中会造成代码膨胀和耦合度增强,此时需要采取额外的方法,创建额外的fallback类继承openfeign的调用接口

    ,此时fallback的作用就是当服务正常的时候不触发,若是出现方法报错或者服务端宕机就会自动去调取fallback类中对应的方法。

    @Component
    public class PaymentFallback implements PaymentHystrixService
    {
        @Override
        public String payment_ok(Integer id) {
            return "服务失败了,o(╥﹏╥)o";
        }
        @Override
        public String payment_timeout(Integer id) {
            return "服务超时或者运行报错,o(╥﹏╥)o";
        }
    }

     本篇所有代码均在GitHub:

  • 相关阅读:
    mysql access denied for user root Mysql用户无权限
    远程链接调用sql脚本
    CuteEditor使用详解
    如何设置release模式
    ShardingJDBC不分库,只分表例子
    SpringCloud Stream整合RocketMQ实现消息发送与接收
    Spring Cloud Gateway的PrefixPath及StripPrefix功能
    使用MongoDB的Spring Boot和MongoTemplate教程
    ShardingJDBC读写分离案例
    SpringBoot那些好用的连接池HikariCP
  • 原文地址:https://www.cnblogs.com/lin530/p/13927080.html
Copyright © 2011-2022 走看看