微服务(Spring Cloud)入门服务断路器Resilience4j

服务断路器简介

断路器(CircuitBreaker)一共有六种状态,CircuitBreaker常用的三种状态(CLOSED、OPEN、HALF_OPEN)
断路器默认是CLOSE关闭状态

  1. closed -> open,关闭状态到熔断状态,当失败调用率的阈值达到50%时会开启断路器
  2. open -> half_open,从熔断状态到半开状态,CircuitBreaker默认60s允许服务调用者将一部分请求发到服务提供者
  3. half_open -> open,当half_open的调用失败率超过给定的阈值,转为open状态
  4. open -> closed,调用失败率低于给定的阈值,打开CircuitBreaker

Resilience4j实现服务降级

在完成这个案例时,要先明白什么时候会出现服务降级

出现服务降级的情况

  1. 程序运行时异常
  2. 超时
  3. 熔断
  4. 线程池/信号量打满

这个案例使用超时来实现服务降级的操作

pom.xml

消费者引入Resilience4j依赖

		<dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-cloud2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
        </dependency>

生产者

创建一个超时方法

// 超时方法
@GetMapping("/timeout")
    public String timeout(){
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "payment successful timeout";
    }

消费者

yml文件

server:
  port: 80

eureka:
  instance:
    instance-id: customer-resilience4j-order80
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/

spring:
  application:
    name: customer-openfeign-order

# 配置resilience4j超时时间,默认超时时间是5s,当出现超时会触发服务降级
resilience4j:
  timelimiter:
    instances:
      delay:
        timeoutDuration: 2

使用openfeign进行远程调用

@FeignClient(value = "服务生产者应用名")
public interface OpenFeignMapper {

    @GetMapping("/payment/timeout")
    String timeout();

}

主启动类

@EnableFeignClients
@SpringBootApplication
@Slf4j
public class OrderResilience4j80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderResilience4j80.class,args);
        log.info("************ OrderResilience4j80 successful ***************");
    }
}

控制层
使用的是Resilience4j,需要遵循规定的编写风格,方法的返回值需要时异步的,需要自己构建带有返回值的异步任务

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    
    @GetMapping("/timeout")
    @TimeLimiter(name="delay",fallbackMethod = "fallback")
    public CompletableFuture<String> timeout(){

        CompletableFuture<String> stringCompletableFuture = CompletableFuture
                .supplyAsync((Supplier<String>) () -> orderService.timeout()); 
        return stringCompletableFuture;

    }

    public CompletableFuture<String> fallback(Exception e){
        e.printStackTrace();
        return CompletableFuture.completedFuture("超时啦");
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿晓晓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值