• 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);
    }
  • 相关阅读:
    架构设计:负载均衡层设计方案(4)——LVS原理
    架构设计:负载均衡层设计方案(3)——Nginx进阶
    架构设计:负载均衡层设计方案(2)——Nginx安装
    架构设计:负载均衡层设计方案(1)——负载场景和解决方式
    oracle 触发器number判断空值,:NEW赋值,for each row,sql变量引号,to_date,to_char
    oracle触发器调试
    if elsif;报错;new赋值
    求一行的和
    oracle如何获取当年第一月,如今年是2015年,则需获取 201501
    在其他对象上同步
  • 原文地址:https://www.cnblogs.com/houchen/p/13532614.html
Copyright © 2020-2023  润新知