5.cloud-Hystrix断路器

2023-12-20 13:13:08

1.概述

? ? 1.1?官网资料

https://github.com/Netflix/Hystrix/wiki/How-To-Use

? ? 1.2?停更进维

https://github.com/Netflix/Hystrix

2.降级

? 2.1 基础应用

? ? ? 1)?pom

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

? ?2) 业务类

? 超过3面提示

@HystrixCommand(fallbackMethod="paymentInfo_TimeOutHandler",
            commandProperties= {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")})
    //失败
    public String paymentInfo_TimeOut(Integer id){

        int timeNumber = 3;
        try { TimeUnit.SECONDS.sleep(timeNumber); }catch (Exception e) {e.printStackTrace();}
        return "线程池:"+Thread.currentThread().getName()+" id:  "+id+"\t"+"呜呜呜"+" 耗时(秒)"+timeNumber;
    }

    //兜底方法
    public String paymentInfo_TimeOutHandler(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"   系统繁忙, 请稍候再试  ,id:  "+id+"\t"+"哭了哇呜";
    }

? ? 3) 主启动

@EnableHystrix
@SpringBootApplication
public class PaymentHystrixMain {

    public static void main(String[] args) {
        SpringApplication.run(PaymentHystrixMain.class, args);
    }
}

此处用@EnableCircuitBreaker也可以,建议使用EnableHystrix注解

? 2.2 统一处理

@RestController
@DefaultProperties(defaultFallback = "paymentInfo_TimeOutHandler")
@RequestMapping("payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @HystrixCommand
    @GetMapping("timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){

        String result = "";
        synchronized (this) {
            G.count++;
            result = paymentService.paymentInfo_TimeOut(id) + " count:" + G.count;;
            log.info("result:"+result);
        }

        return result;
    }

    //兜底方法
    public String paymentInfo_TimeOutHandler(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"   系统繁忙, 请稍候再试  ,id:  "+id+"\t"+"哭了哇呜";
    }

}

? 2.3 代码和业务分开

? ? 1) yml

feign:
  hystrix:
    enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样。
 

? ? 2) 接口类

? ?重载方法中写异常数据


@Component
public class PaymentFallbackService implements PaymentHystrixService {

    @Override
    public String paymentInfo_OK(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_OK , (┬_┬)";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "-----PaymentFallbackService fall back-paymentInfo_TimeOut , (┬_┬)";
    }
}

? ? 3) 接口

@Component
@FeignClient(value = "CLOUD-PAYMENT",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {

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

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

}
 

3.限流

4.熔断

文章来源:https://blog.csdn.net/wang_peng/article/details/134962667
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。