zoukankan      html  css  js  c++  java
  • Hystrix的服务降级配置

    Hystrix的服务降级,既可以配置在服务提供端,也可以配置在服务调用端,

    但一般来说,配置在服务调用端!

    服务提供者端:

    1)业务类 : 添加fallback方法

    @Service
    public class PaymentService {
    
    
        public String paymentInfo_ok(Integer id){
            return "线程池:"+Thread.currentThread().getName()+
                    " paymentInfo_ok,id:"+id+" O(∩_∩)O哈哈~";
        }
    
    
        @HystrixCommand(fallbackMethod = "paymentInfo_timeout_handler",
        commandProperties = {
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="3000") //程序运行超过3s, 就会进行服务降级
        })
        public String paymentInfo_timeout(Integer id){
            int timenumber=5;
            try {
                TimeUnit.SECONDS.sleep(timenumber);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "线程池:"+Thread.currentThread().getName()+
                    " paymentInfo_timeout,id:"+id+" 耗时3秒!!";
        }
    
        public String paymentInfo_timeout_handler(Integer id){
            return "超时+兜底+ o(╥﹏╥)o";
        }
    
    }

    2)主启动类 :添加@EnableCircuitBreaker注解

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class PaymentHystrixMain8001 {
        public static void main(String[] args) {
            SpringApplication.run(PaymentHystrixMain8001.class,args);
        }
    }

    服务消费者端:

    1)业务类

    @RestController
    @Slf4j
    public class OrderController {
    
        @Autowired
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping("/consumer/payment/hystrix/ok/{id}")
        public String paymentInfo_ok(@PathVariable("id") Integer id)
        {
            String result = paymentHystrixService.paymentInfo_ok(id);
            return result;
        }
    
    
        @GetMapping("/consumer/payment/hystrix/timeout/{id}")
        @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
                @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
        })
        //@HystrixCommand
        public String paymentInfo_timeout(@PathVariable("id") Integer id)
        {
            int age = 10/0;
            String result = paymentHystrixService.paymentInfo_timeout(id);
            return result;
        }
        public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
        {
            return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
        }
    
        // 下面是全局fallback方法
        public String payment_Global_FallbackMethod()
        {
            return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
        }
    }

    2)主启动类

    @SpringBootApplication
    @EnableFeignClients
    @EnableHystrix
    public class ConsumerFeignHystrixMian80 {
        public static void main(String[] args) {
            SpringApplication.run(ConsumerFeignHystrixMian80.class,args);
        }
    }

    Hystrix之全局服务降级DefaultProperties

    出现的问题:

    每个业务方法对应一个兜底的方法,代码碰撞

    错误处理的方法和业务代码混淆在一块

    解决

    @RestController
    @Slf4j
    @DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
    public class OrderController {
    
        @Autowired
        private PaymentHystrixService paymentHystrixService;
    
        @GetMapping("/consumer/payment/hystrix/ok/{id}")
        public String paymentInfo_ok(@PathVariable("id") Integer id)
        {
            String result = paymentHystrixService.paymentInfo_ok(id);
            return result;
        }
    
    
        @HystrixCommand
        public String paymentInfo_timeout(@PathVariable("id") Integer id)
        {
            int age = 10/0;
            String result = paymentHystrixService.paymentInfo_timeout(id);
            return result;
        }
        public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id)
        {
            return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
        }
    
        // 下面是全局fallback方法
        public String payment_Global_FallbackMethod()
        {
            return "Global异常处理信息,请稍后再试,/(ㄒoㄒ)/~~";
        }
    }

    Hystrix之通配服务降级FeignFallback

    【由Feign来处理服务降级】

    1)定义 fullback类

    2)在Feign注解上进行配置

    @Component
    @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT",fallback =PaymentHystrixServiceFullbackImpl.class )
    public interface PaymentHystrixService {
    
        @GetMapping("/payment/hystrix/ok/{id}")
        public String paymentInfo_ok(@PathVariable("id") Integer id);
    
        @GetMapping("/payment/hystrix/timeout/{id}")
        public String paymentInfo_timeout(@PathVariable("id") Integer id);
    }
  • 相关阅读:
    最小生成树 kruskal算法&prim算法
    Floyd算法解决多源最短路问题
    dijkstra算法解决单源最短路问题
    改进后的作业排序
    第一篇 基本结构
    循环轮转算法
    在线工具生成接入信息mqtt.fx快速接入阿里云
    NodeMCU使用ArduinoJson判断指定键值对存在与否
    NodeMCU获取并解析心知天气信息
    快速导出jekyll博客文件进行上传部署
  • 原文地址:https://www.cnblogs.com/houchen/p/13532614.html
Copyright © 2011-2022 走看看