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

免费网站源码...金坛网站优化

免费网站源码...,金坛网站优化,做自媒体常用的图片网站,让网站快速收录最新1.Retry介绍 Spring Retry 提供了自动重新调用失败的操作的功能。这在错误可能是暂时的#xff08;例如瞬时网络故障#xff09;的情况下很有用。从2.2.0版本开始#xff0c;重试功能已从Spring Batch中撤出#xff0c;成为一个独立的新库#xff1a;Spring Retry 使用场景… 1.Retry介绍 Spring Retry 提供了自动重新调用失败的操作的功能。这在错误可能是暂时的例如瞬时网络故障的情况下很有用。从2.2.0版本开始重试功能已从Spring Batch中撤出成为一个独立的新库Spring Retry 使用场景 在日常开发过程中难免会与第三方接口发生交互例如短信发送、远程服务调用、争抢锁等场景当正常调用发生异常时例如网络抖动这些间歇性的异常在一段时候之后会自行恢复程序为了更加健壮并且更不容易出现故障需要重新触发业务操作以防止间歇性的异常对程序照成的影响。 tips幂等性 非幂等的情况下要小心使用重试。HTTP/1.1中对幂等性的定义是一次和多次请求某一个资源对于资源本身应该具有同样的结果网络超时等问题除外。也就是说其任意多次执行对资源本身所产生的影响均与一次执行的影响相同。 2.代码工程 实验目标模拟三方接口异常触发重试 pom.xml ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringboot-demo/artifactIdgroupIdcom.et/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdSpringRetry/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-autoconfigure/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency!--retry--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactId/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactId/dependency/dependencies /project application.yaml server:port: 8088 RemoteApiService.java Retryable注解value值表示当哪些异常的时候触发重试maxAttempts表示最大重试次数默认为3delay表示重试的延迟时间multiplier表示上一次延时时间是这一次的倍数。Recover注解当重试次数达到设置的次数的时候还是失败抛出异常执行的回调函数。 package com.et.retry.service;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; import org.springframework.stereotype.Service;import java.time.LocalDateTime; import java.time.LocalTime;Service public class RemoteApiService {private Logger logger LoggerFactory.getLogger(getClass());Retryable(value Exception.class,maxAttempts 3,backoff Backoff(delay 2000,multiplier 1.5))public boolean pay(int num) throws Exception{logger.info(invoke third method);logger.info(do something... {}, LocalDateTime.now());//mock exceptionif(num0) {throw new Exception(errorneed retry);}return true;}Recoverpublic boolean recover(int num) throws Exception {logger.info(recover ... {},{}, num, LocalDateTime.now());return false;}} DemoApplication.java 在主类上加上EnableRetry注解表示启用重试机制。 package com.et.retry;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.retry.annotation.EnableRetry;SpringBootApplication EnableRetry public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);} } 以上只是一些关键代码所有代码请参见下面代码仓库 代码仓库 https://github.com/Harries/springboot-demo 3.测试 编写测试类 package com.et.retry;import com.et.retry.service.RemoteApiService; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;RunWith(SpringRunner.class) SpringBootTest(classes DemoApplication.class) public class DemoTests {private Logger log LoggerFactory.getLogger(getClass());AutowiredRemoteApiService remoteApiService;Beforepublic void before() {log.info(init some data);}Afterpublic void after(){log.info(clean some data);}Testpublic void execute() throws Exception {log.info(pay result:remoteApiService.pay(0));}} 运行测试方法结果如下 2024-04-03 10:35:54.738 INFO 13096 --- [ main] com.et.retry.DemoTests : init some data 2024-04-03 10:35:54.756 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method 2024-04-03 10:35:54.759 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:54.758 2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method 2024-04-03 10:35:56.766 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:56.766 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : invoke third method 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : do something... 2024-04-03T10:35:59.776 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.service.RemoteApiService : recover ... 0,2024-04-03T10:35:59.776 2024-04-03 10:35:59.776 INFO 13096 --- [ main] com.et.retry.DemoTests : pay result:false 2024-04-03 10:35:59.778 INFO 13096 --- [ main] com.et.retry.DemoTests : clean some data 4.引用 https://www.jianshu.com/p/314059943f1chttp://www.liuhaihua.cn/archives/710389.html
http://www.pierceye.com/news/404935/

相关文章:

  • 阿里巴巴网站工作流程网站建设 教学设计
  • 电子商务网站建设的方法怎样用织梦做音乐网站
  • 临夏州住房和城乡建设局网站出词
  • 企业网站的综合要求最新领导班子7人名单
  • 通过阿里云建设企业网站联想企业网站建设的思路
  • 网站建设服务器的选择方案建设报名系统是正规网站吗
  • 揭阳高端模板建站WordPress背景音乐6
  • 如何使用云服务建设网站cpa之家 app推广平台
  • 网站设计策划书案例漳浦建设局网站
  • ps做分享类网站效果图设计公司工作室创业规划
  • 个人虚拟机做网站设计实例网站
  • 衡阳企业网站wordpress置顶文章顺序
  • 网站建设宗旨是指郑州有名的做网页的公司
  • 怎么0成本做网站企业网站如何设计网页
  • 做韦恩图网站课程分销平台
  • html5网站建设中企业整站推广
  • 织梦网站打开速度慢做抢单软件的网站
  • 51单片机可以做网站怎么建设游戏试玩平台网站
  • 汕头网站建设方案维护wordpress百度熊掌
  • 牛街网站建设产品vi设计都包括什么
  • 网站需要多大宽带网站发展的方向
  • 陈光锋网站运营推广新动向故城建设银行网站
  • 备案后网站可以改名吗临颖网站建设
  • 临沭县建设局官方网站怎样做外贸网站推广
  • 手机网站支付一个简单的网页代码带图片
  • 向公司申请请做网站广州网站推广教程
  • 用QQ群做网站排名交互式网站app
  • 正规免费发布信息网站国外网站界面
  • 浏览国外网站 dns网店运营推广方案
  • wordpress弹幕视频插件广西seo搜索引擎优化