resilience4j 重试,熔断,限流

pom.xml引入

我的是springboot3.x的版本

 <!--限流-->
        <dependency>
            <groupId>io.github.resilience4j</groupId>
            <artifactId>resilience4j-spring-boot3</artifactId>
            <version>2.2.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

application.yml

server:
  port: 8080


resilience4j:
  retry:               #重试机制
    instances:         #定义多个重试策略
      retryApi:        #第一个重试策略的名称 可以随便命名
        max-attempts: 5 #失败的时候,最多重试5次
        wait-duration: 1s  #重试等待时间为1秒

  circuitbreaker:  #断路器 防止对失败或者不稳定的服务 连续调用
    instances:     #定义多个断路器实例
      circuitbreakerApi:    #第一个断路器名称 可以随便命名
        register-health-indicator: true #是否将断路器的状态注册为健康指标
        sliding-window-size: 10       #计算失败率的滑动窗口大小为10  最近10次调用失败
        permitted-number-of-calls-in-half-open-state: 3        #当断路器处于半开状态时,允许最大调用次数为3
        sliding-window-type: TIME_BASED  #滑动窗口的类型基于时间
        minimum-number-of-calls: 5 #当断路器开始计算失败率之前 至少需要5次调用
        wait-duration-in-open-state: 5s #当断路器打开后(服务就不可以用了),会等待5秒在尝试进入半开状态
        failure-rate-threshold: 20 #当失败率达到20%的时候,断路器会打开,组织进一步的调用
        event-consumer-buffer-size: 10 #用于存储断路器相关事件的缓冲区大小为10

  ratelimiter: #限流配置 防止过多的请求进来
    instances: #定义多个限流的实例
      ratelimiterApi: #第一个限流的名称 可以随便命名
        limit-for-period: 1 #在特定的时间内,最大请求数量是1
        limit-refresh-period: 1s #每1秒会重置请求次数
        timeout-duration: 100ms # 超时时间为100毫秒  1秒进1个请求





重试只需要关心这一部分的配置

断路器只需要关心这一部分的配置

 限流只需要关注这一部分的配置

代码部分

package com.dmg.demo2.demos;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    //重试
    @Retry(name = "retryApi",fallbackMethod = "fallback")
    @GetMapping("/aa")
    public String aa(){
        System.out.println("aaaaaaaaaaaaa");
        String forObject = restTemplate.getForObject("http://localhost:8888", String.class);
        System.out.println("======="+forObject);
        return "aaaa";
    }


    //断路器
    @CircuitBreaker(name = "circuitbreakerApi",fallbackMethod = "fallback")
    @GetMapping("/bb")
    public String bb(){
        int a= new Random().nextInt(10);
        if(a==7){
            //随机出错
            int b=1/0;
        }
        return "bbbbb";
    }


    //限流
    @RateLimiter(name = "ratelimiterApi",fallbackMethod = "fallback")
    @GetMapping("/cc")
    public String cc(){

        return "ccc";
    }

    //回调方法
    private String fallback(Throwable throwable){
        System.out.println("回调方法触发,请稍后访问:"+throwable.getMessage());
        return throwable.getMessage();
    }
}
@SpringBootApplication
public class Demo2Application {

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

    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

重试

http://localhost:8080/aa

访问一个不存在的服务,会重试5次 

熔断

 进行10次压测

随机触发熔断 ,窗口失败最大为10次,当失败5次之后 断路器就会打开

当失败率达到20%的时候,断路器就会打开,会显示下面的内容,等5秒之后进入半开状态

 

限流

http://localhost:8080/cc

1秒内只能1个请求,超过就限流

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值