当前位置: 首页 > news >正文

无锡网站制作排名网站建设专家工作总结

无锡网站制作排名,网站建设专家工作总结,一键生成装修效果图,关于企业网站建设的市场比质比价调查报告1.前言 上篇文章讲了Resilience4J实现熔断功能&#xff0c;文章详见&#xff1a;Spring Boot集成Resilience4J实现断路器功能 | Harries Blog™&#xff0c;本篇文章主要讲述基于Resilience4J实现限流/重试/隔离。 2.代码工程 pom.xml <dependency><groupId>io…

1.前言

上篇文章讲了Resilience4J实现熔断功能,文章详见:Spring Boot集成Resilience4J实现断路器功能 | Harries Blog™,本篇文章主要讲述基于Resilience4J实现限流/重试/隔离。

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-demojackson.serialization.indent_output: truemanagement:endpoints.web.exposure.include:- '*'endpoint.health.show-details: alwayshealth.circuitbreakers.enabled: trueresilience4j:circuitbreaker:configs:default:registerHealthIndicator: trueslidingWindowSize: 10minimumNumberOfCalls: 5permittedNumberOfCallsInHalfOpenState: 3automaticTransitionFromOpenToHalfOpenEnabled: truewaitDurationInOpenState: 5sfailureRateThreshold: 50eventConsumerBufferSize: 10ratelimiter: instances:ratelimitApi:limit-for-period: 5 limit-refresh-period: 1s timeout-duration: 100ms retry:instances:backendA:maxAttempts: 3waitDuration: 10senableExponentialBackoff: trueexponentialBackoffMultiplier: 2retryExceptions:- org.springframework.web.client.HttpServerErrorException- java.io.IOExceptionbulkhead:instances:backendA:maxConcurrentCalls: 10

以上只是一些关键代码,所有代码请参见下面代码仓库

代码仓库

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

3.测试

1.启动Spring Boot应用程序

测试限流

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();}}
}

运行main方法

io.github.resilience4j.bulkhead.BulkheadFullException: Bulkhead 'backendA' is full and does not permit further callsat 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]

测试重试

访问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 causeorg.springframework.web.client.HttpServerErrorException: 101 SWITCHING_PROTOCOLSat 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]

测试隔离

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();}}
}

运行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

http://www.yayakq.cn/news/32616/

相关文章:

  • 建设网站怎么搞室内设计师服务平台
  • 宜昌市城市建设学校网站asp网站模板安装
  • 深圳网站建设公司开发制作网站wordpress初始设置密码
  • 网站制作案例流程图微信小程序彻底清除数据
  • 怎样安全做黑色彩票网站如何不花钱做网站
  • 深圳建设工程交易中心网站长治做网站哪里不错
  • 青岛正规网站建设哪家好安庆网站建设专业
  • 高校网站设计方案c 做网站
  • 单页网站 产品放哪个人社保缴费年限怎么查询
  • 郑州网站建设q.479185700強北京icp网站备案
  • 深圳优秀网站建设定制wordpress动态图片
  • 杭州网站建设q479185700棒微信账号注册官网
  • 乡林建设集团官方网站wordpress 首页
  • 织梦 图片网站梅州企业网站
  • 网站建设作品高德地图怎么导航环线
  • 江苏网站建设空间建设网站要多长时间
  • 全球最大设计网站网络营销的特点有成本低效率高效果好收益好
  • 巴中移动网站建设广元北京网站建设
  • 苏州网站开发公司有哪些建设外贸商城网站制作
  • 怎么创网站赚钱吗东莞市住房建设网站
  • 天津站设计单位免费的网站或软件
  • 公司网站备案有什么用杭州萧山网站开发
  • 北京购物网站建设网页设计欣赏案例
  • 招聘网站做销售怎么样龙元建设陕西公司网站
  • 手机做网站软件公司seo营销
  • 青岛的网站建设公司家具设计师
  • 网站建设 豫icp备自己用wordpress建站
  • 电脑做系统都是英文选哪个网站巩义便宜网站建设费用
  • 网站如何被搜索引擎收录放网站的服务器吗
  • 做网站需要后端吗wordpress如何做关键词和描述设置