最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot集成Resilience4J實(shí)現(xiàn)限流/重試/隔離

 更新時(shí)間:2024年10月11日 12:00:12   作者:HBLOGA  
在Java的微服務(wù)生態(tài)中,對(duì)于服務(wù)保護(hù)組件,像springcloud的Hystrix,springcloud?alibaba的Sentinel,以及當(dāng)Hystrix停更之后官方推薦使用的Resilience4j,所以本文給大家介紹了Spring Boot集成Resilience4J實(shí)現(xiàn)限流/重試/隔離,需要的朋友可以參考下

1.前言

本篇文章主要講述基于Resilience4J實(shí)現(xiàn)限流/重試/隔離。

2.代碼工程

pom.xml

<dependency>
    <groupId>io.github.resilience4j</groupId>
    <artifactId>resilience4j-spring-boot3</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

限流

@RequestMapping("/hello")
@RateLimiter(name="ratelimitApi",fallbackMethod = "fallback")
public ResponseEntity<String> showHelloWorld(){
   return new ResponseEntity<>("success",HttpStatus.OK);
   }
public ResponseEntity fallback(Throwable e){
   log.error("fallback exception , {}",e.getMessage());
   return new ResponseEntity<>("your request is too fast,please low down", HttpStatus.OK);
}

重試

@RequestMapping("/retry")
@Retry(name = "backendA")//use backendA ,if throw IOException ,it will be retried 3 times。
public ResponseEntity<String> retry(String name){
   if(name.equals("test")){
      i++;
      log.info("retry time:{}",i);
      throw  new HttpServerErrorException(HttpStatusCode.valueOf(101));
   }
   return new ResponseEntity<>("retry",HttpStatus.OK);
}

隔離

@RequestMapping("/bulkhead")
@Bulkhead(name = "backendA")
public ResponseEntity<String> bulkhead(){
 
   return new ResponseEntity<>("bulkhead",HttpStatus.OK);
}

配置文件

spring:
  application.name: resilience4j-demo
  jackson.serialization.indent_output: true
 
management:
  endpoints.web.exposure.include:
    - '*'
  endpoint.health.show-details: always
  health.circuitbreakers.enabled: true
 
resilience4j:
  circuitbreaker:
    configs:
      default:
        registerHealthIndicator: true
        slidingWindowSize: 10
        minimumNumberOfCalls: 5
        permittedNumberOfCallsInHalfOpenState: 3
        automaticTransitionFromOpenToHalfOpenEnabled: true
        waitDurationInOpenState: 5s
        failureRateThreshold: 50
        eventConsumerBufferSize: 10
 
 
  ratelimiter: 
    instances:
      ratelimitApi:
        limit-for-period: 5 
        limit-refresh-period: 1s 
        timeout-duration: 100ms 
  retry:
    instances:
      backendA:
        maxAttempts: 3
        waitDuration: 10s
        enableExponentialBackoff: true
        exponentialBackoffMultiplier: 2
        retryExceptions:
          - org.springframework.web.client.HttpServerErrorException
          - java.io.IOException
  bulkhead:
    instances:
      backendA:
        maxConcurrentCalls: 10

以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見下面代碼倉庫

代碼倉庫

GitHub - Harries/springboot-demo: a simple springboot demo with some components for example: redis,solr,rockmq and so on.(Resilience4J)

3.測(cè)試

1.啟動(dòng)Spring Boot應(yīng)用程序

測(cè)試限流

public class ThreadTest {
    public static void main(String[] args) {
       for(int i=0;i<6;i++){
            new Thread(()->{
                System.out.println(new RestTemplate().getForObject("http://localhost:8080/hello",String.class));
            }).start();
        }
     
    }
}

運(yùn)行main方法

io.github.resilience4j.bulkhead.BulkheadFullException: Bulkhead 'backendA' is full and does not permit further calls
 at io.github.resilience4j.bulkhead.BulkheadFullException.createBulkheadFullException(BulkheadFullException.java:49) ~[resilience4j-bulkhead-2.0.2.jar:2.0.2]
 at io.github.resilience4j.bulkhead.internal.SemaphoreBulkhead.acquirePermission(SemaphoreBulkhead.java:164) ~[resilience4j-bulkhead-2.0.2.jar:2.0.2]
 at io.github.resilience4j.bulkhead.Bulkhead.lambda$decorateCheckedSupplier$0(Bulkhead.java:68) ~[resilience4j-bulkhead-2.0.2.jar:2.0.2]
 at io.github.resilience4j.bulkhead.Bulkhead.executeCheckedSupplier(Bulkhead.java:471) ~[resilience4j-bulkhead-2.0.2.jar:2.0.2]
 at io.github.resilience4j.spring6.bulkhead.configure.BulkheadAspect.handleJoinPoint(BulkheadAspect.java:194) ~[resilience4j-spring6-2.0.2.jar:2.0.2]
 at io.github.resilience4j.spring6.bulkhead.configure.BulkheadAspect.proceed(BulkheadAspect.java:147) ~[resilience4j-spring6-2.0.2.jar:2.0.2]
 at io.github.resilience4j.spring6.bulkhead.configure.BulkheadAspect.lambda$bulkheadAroundAdvice$1(BulkheadAspect.java:120) ~[resilience4j-spring6-2.0.2.jar:2.0.2]
 at io.github.resilience4j.spring6.fallback.FallbackExecutor.execute(FallbackExecutor.java:37) ~[resilience4j-spring6-2.0.2.jar:2.0.2]
 at io.github.resilience4j.spring6.bulkhead.configure.BulkheadAspect.bulkheadAroundAdvice(BulkheadAspect.java:121) ~[resilience4j-spring6-2.0.2.jar:2.0.2]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
 at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:637) ~[spring-aop-6.1.2.jar:6.1.2]

測(cè)試重試

訪問http://127.0.0.1:8080/retry?name=test

2024-08-03T23:16:32.092+08:00 INFO 5097 --- [resilience4j-demo] [nio-8080-exec-9] c.e.r.controller.HelloWorldController : retry time:1
2024-08-03T23:16:42.120+08:00 INFO 5097 --- [resilience4j-demo] [nio-8080-exec-9] c.e.r.controller.HelloWorldController : retry time:2
2024-08-03T23:17:02.142+08:00 INFO 5097 --- [resilience4j-demo] [nio-8080-exec-9] c.e.r.controller.HelloWorldController : retry time:3
2024-08-03T23:17:02.165+08:00 ERROR 5097 --- [resilience4j-demo] [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.client.HttpServerErrorException: 101 SWITCHING_PROTOCOLS] with root cause
 
org.springframework.web.client.HttpServerErrorException: 101 SWITCHING_PROTOCOLS
 at com.et.resilience4j.controller.HelloWorldController.retry(HelloWorldController.java:37) ~[classes/:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
 at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
 at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:352) ~[spring-aop-6.1.2.jar:6.1.2]
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) ~[spring-aop-6.1.2.jar:6.1.2]

測(cè)試隔離

public class ThreadTest {
    public static void main(String[] args) {
      /*  for(int i=0;i<6;i++){
            new Thread(()->{
                System.out.println(new RestTemplate().getForObject("http://localhost:8080/hello",String.class));
            }).start();
        }*/
      for(int i=0;i<11;i++){
         new Thread(()->{
            System.out.println(new RestTemplate().getForObject("http://localhost:8080/bulkhead",String.class));
         }).start();
      }
    }
}

運(yùn)行main方法

2024-08-03T23:17:36.943+08:00 ERROR 5097 --- [resilience4j-demo] [nio-8080-exec-5] c.e.r.controller.HelloWorldController : fallback exception , RateLimiter 'ratelimitApi' does not permit further calls

以上就是Spring Boot集成Resilience4J實(shí)現(xiàn)限流/重試/隔離的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成Resilience4j的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringMVC解析post請(qǐng)求參數(shù)詳解

    SpringMVC解析post請(qǐng)求參數(shù)詳解

    今天小編就為大家分享一篇解決SpringMVC接收不到ajaxPOST參數(shù)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-08-08
  • Java 中的vector和list的區(qū)別和使用實(shí)例詳解

    Java 中的vector和list的區(qū)別和使用實(shí)例詳解

    在大家還沒有了解vector,list,deque的知識(shí)之前,我先給大家介紹下stl,本文重點(diǎn)給大家介紹vector和list的區(qū)別及使用,感興趣的的朋友一起看看吧
    2017-09-09
  • SpringBoot 整合線程池的示例詳解

    SpringBoot 整合線程池的示例詳解

    線程池是一種利用池化技術(shù)思想來實(shí)現(xiàn)的線程管理技術(shù),主要是為了復(fù)用線程、便利地管理線程和任務(wù)、并將線程的創(chuàng)建和任務(wù)的執(zhí)行解耦開來,這篇文章主要介紹了SpringBoot 整合線程池的示例詳解,需要的朋友可以參考下
    2024-08-08
  • java.io.IOException:你的主機(jī)中的軟件中止了一個(gè)已建立的連接踩坑實(shí)戰(zhàn)

    java.io.IOException:你的主機(jī)中的軟件中止了一個(gè)已建立的連接踩坑實(shí)戰(zhàn)

    最近在工作中遇到了個(gè)問題,分享給同樣遇到問題的同學(xué),這篇文章主要給大家介紹了關(guān)于java.io.IOException:你的主機(jī)中的軟件中止了一個(gè)已建立的連接的踩坑實(shí)戰(zhàn)記錄,需要的朋友可以參考下
    2023-03-03
  • 淺談JSONObject的使用及示例代碼(JSON解析)

    淺談JSONObject的使用及示例代碼(JSON解析)

    這篇文章主要介紹了淺談JSONObject的使用及示例代碼(JSON解析),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java如何在PDF中添加ToolTip工具提示

    Java如何在PDF中添加ToolTip工具提示

    大家好,本篇文章主要講的是Java如何在PDF中添加ToolTip工具提示,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • Java如何使用spire進(jìn)行word文檔的替換詳解

    Java如何使用spire進(jìn)行word文檔的替換詳解

    創(chuàng)作一份文案經(jīng)常會(huì)高頻率地使用某些詞匯,如地名、人名、人物職位等,若表述有誤,就需要整體撤換,下面這篇文章主要給大家介紹了關(guān)于Java如何使用spire進(jìn)行word文檔的替換的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 解決使用gateway后靜態(tài)資源失效的問題

    解決使用gateway后靜態(tài)資源失效的問題

    這篇文章主要介紹了解決使用gateway后靜態(tài)資源失效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java實(shí)現(xiàn)列表、集合與數(shù)組之間轉(zhuǎn)化的方法

    java實(shí)現(xiàn)列表、集合與數(shù)組之間轉(zhuǎn)化的方法

    這篇文章主要介紹了java實(shí)現(xiàn)列表、集合與數(shù)組之間轉(zhuǎn)化的方法,涉及java中列表、集合與數(shù)組相互轉(zhuǎn)換的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • MybatisPlus3.3.1整合clickhouse的過程

    MybatisPlus3.3.1整合clickhouse的過程

    這篇文章主要介紹了MybatisPlus3.3.1整合clickhouse的過程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-05-05

最新評(píng)論

杭锦后旗| 永丰县| 安岳县| 山西省| 化德县| 兴隆县| 平安县| 正安县| 荥经县| 拜泉县| 乐安县| 西安市| 嘉黎县| 北海市| 西和县| 施甸县| 利川市| 婺源县| 隆林| 临高县| 乐清市| 彰化市| 凤台县| 涿州市| 古浪县| 连云港市| 上高县| 蒲江县| 马公市| 洪江市| 仲巴县| 温宿县| 中方县| 青岛市| 浑源县| 三都| 肇庆市| 罗平县| 宣化县| 黑水县| 左权县|