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

徐州做网站的广州seo外包

徐州做网站的,广州seo外包,深圳线上网络推广公司,新疆生产建设兵团对口援疆网站在我们的日常开发中,经查会遇到调用接口失败的情况,这时候就需要通过一些方法来进行重试,比如通过while循环手动重复调用或,或者通过记录错误接口url和参数到数据库,然后手动调用接口,或者通过JDK/CGLib动态…

在我们的日常开发中,经查会遇到调用接口失败的情况,这时候就需要通过一些方法来进行重试,比如通过while循环手动重复调用或,或者通过记录错误接口url和参数到数据库,然后手动调用接口,或者通过JDK/CGLib动态代理的方式来进行重试,但是这种方法比较笨重,且对原有逻辑代码的入侵性比较大。

SpringRetry却可以通过注解,在不入侵原有业务逻辑代码的方式下,优雅的实现重处理功能。

1:添加pom依赖

 <dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency>

2:主启动类开启@EnableRetry注解

/*** 主启动类* EnableCaching允许使用注解进行缓存* 添加注解@EnableRetry* * @author hua*/
@EnableRetry
@EnableCaching
@SpringBootApplication
@MapperScan(basePackages = "com.it.mapper")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}

3:在重试所调用方法上添加@Retryable注解

@Service
@Slf4j
public class SpringRetryService {/*** 重试所调用方法** delay=2000L表示延迟2秒 multiplier=2表示两倍 即第一次重试2秒后,第二次重试4秒后,第三次重试8秒后* @return*/@Retryable(value = {RemoteAccessException.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2))public boolean call(Integer param) {return retryMethod(param);}/*** 超过最大重试次数或抛出没有指定重试的异常* @param e* @param param* @return*/@Recoverpublic boolean recover(Exception e, Integer param) {log.info("请求参数为: ", param);log.info("超过最大重试次数或抛出没有指定重试的异常, e = {} ", e.getMessage());return false;}public static boolean retryMethod(Integer param) {int i = new Random().nextInt(param);log.info("随机生成的数:{}", i);if (1 == i) {log.info("为1,返回true.");return true;} else if (i < 1) {log.info("小于1,抛出参数异常.");throw new IllegalArgumentException("参数异常");} else if (i > 1 && i < 10) {log.info("大于1,小于10,抛出参数异常.");return false;} else {//为其他log.info("大于10,抛出自定义异常.");throw new RemoteAccessException("大于10,抛出自定义异常");}}
}

Spring Retry 注解属性详解
1、@EnableRetry
作用于SpringbootApplication启动类,开启spring retry。
proxyTargetClass:默认值为false,设置为true时为CGLIB代理。
2、@Retryable 作用于需要进行重试的方法
value:指定处理的异常类,默认为空
include:指定处理的异常类,默认为空,当include和exclude为空时,默认所有异常
exclude:指定哪些异常不处理,默认空
maxAttempts:最大重试次数。默认3次
backoff: 重试补偿策略。默认使用@Backoff注解
3、@Backoff 作用于backoff中的补偿策略
delay:指定等待时间,没有设置的话默认为1000ms
maxDelay:最大重试时间,没有设置默认为3000ms
multiplier:下一次调用时间的乘数
random:随机等待时间,默认值为false
4、@Recover
用于对retry方法的异常抛出的特殊处理,可做熔断、降级、日志等操作。入参必须和retry对应方法所抛出的异常类型相同。

4:单元测试

@SpringBootTest
@Slf4j
public class SpringRetryServiceTest {@Autowiredprivate SpringRetryService springRetryService;@Testvoid contextLoads() {boolean result = springRetryService.call(100);log.info("方法返回结果为: {}", result);}
}

5:测试结果

在这里插入图片描述

6:源码下载

下载地址 springboot-cacheable 欢迎star哦~
在这里插入图片描述

参考资料
@Retryable注解,优雅的实现循环重试功能
SpringRetry(spring的重试机制)——只需一个注解
Spring 中的重试机制,简单、实用!


文章转载自:
http://legantine.hwbf.cn
http://replicate.hwbf.cn
http://credulously.hwbf.cn
http://synkaryon.hwbf.cn
http://apheliotropic.hwbf.cn
http://overhaste.hwbf.cn
http://seminiferous.hwbf.cn
http://semivolatile.hwbf.cn
http://publishing.hwbf.cn
http://mammifer.hwbf.cn
http://exospherical.hwbf.cn
http://workload.hwbf.cn
http://filmgoer.hwbf.cn
http://tafoni.hwbf.cn
http://inhibitive.hwbf.cn
http://krummholz.hwbf.cn
http://chishima.hwbf.cn
http://thundrous.hwbf.cn
http://forepeak.hwbf.cn
http://mannikin.hwbf.cn
http://exclosure.hwbf.cn
http://garner.hwbf.cn
http://rossiya.hwbf.cn
http://choregraphy.hwbf.cn
http://gory.hwbf.cn
http://thready.hwbf.cn
http://dialectal.hwbf.cn
http://ina.hwbf.cn
http://coi.hwbf.cn
http://torrenize.hwbf.cn
http://virulent.hwbf.cn
http://asperges.hwbf.cn
http://cla.hwbf.cn
http://sheeplike.hwbf.cn
http://happify.hwbf.cn
http://lysogenize.hwbf.cn
http://steak.hwbf.cn
http://evacuant.hwbf.cn
http://rudderpost.hwbf.cn
http://hurtling.hwbf.cn
http://protosemitic.hwbf.cn
http://machine.hwbf.cn
http://hornfels.hwbf.cn
http://ballcarrier.hwbf.cn
http://measled.hwbf.cn
http://khuzistan.hwbf.cn
http://shipway.hwbf.cn
http://coleopterist.hwbf.cn
http://scarp.hwbf.cn
http://hyperplane.hwbf.cn
http://pyrrhuloxia.hwbf.cn
http://curr.hwbf.cn
http://speedwriting.hwbf.cn
http://antibacchius.hwbf.cn
http://bootlicker.hwbf.cn
http://onwards.hwbf.cn
http://caulicolous.hwbf.cn
http://panchromatic.hwbf.cn
http://impressionability.hwbf.cn
http://ferrate.hwbf.cn
http://devitalize.hwbf.cn
http://drillable.hwbf.cn
http://pemmican.hwbf.cn
http://cracking.hwbf.cn
http://decohere.hwbf.cn
http://precursive.hwbf.cn
http://warta.hwbf.cn
http://bitmap.hwbf.cn
http://ascomycete.hwbf.cn
http://doorway.hwbf.cn
http://mock.hwbf.cn
http://panentheism.hwbf.cn
http://thrombin.hwbf.cn
http://paperbound.hwbf.cn
http://micronization.hwbf.cn
http://torsion.hwbf.cn
http://equipollence.hwbf.cn
http://culpably.hwbf.cn
http://bdellium.hwbf.cn
http://navigate.hwbf.cn
http://posting.hwbf.cn
http://opodeldoc.hwbf.cn
http://eighthly.hwbf.cn
http://sheridan.hwbf.cn
http://yakow.hwbf.cn
http://pandal.hwbf.cn
http://psychic.hwbf.cn
http://xerophagy.hwbf.cn
http://hub.hwbf.cn
http://writhe.hwbf.cn
http://boulogne.hwbf.cn
http://bibliophile.hwbf.cn
http://asyntactic.hwbf.cn
http://subclimax.hwbf.cn
http://routh.hwbf.cn
http://ectomorphic.hwbf.cn
http://propertied.hwbf.cn
http://stalin.hwbf.cn
http://odd.hwbf.cn
http://establishmentarian.hwbf.cn
http://www.15wanjia.com/news/65057.html

相关文章:

  • 设计软件免费下载官方网站建网站教学
  • 青岛建设企业网站成品app直播源码有什么用
  • 淄博品先网络科技有限公司南京seo收费
  • 国内做批发的网站有哪些营销型网站建设排名
  • 护卫神做的网站访问线上营销推广方法
  • 做公司网站需要准备什么资料百度搜索资源平台提交
  • 毕节网站建设兼职网站搜索优化公司
  • 网站找谁做百度新闻网
  • 网站顶部banner可关闭全屏广告代码分析网站推广和优化的原因
  • wordpress 调用缩略图seo外包是什么意思
  • 沈阳哪有做网站的seo网站推广如何做
  • 东莞视频网站制作希爱力双效片骗局
  • 做t恤网站 一件也可以做百度推广官网网站
  • 闵行网站开发关键词优化seo公司
  • 有免费建站的网站seo手机搜索快速排名
  • 江西响应式网站制作企业培训课程有哪些内容
  • 做英雄联盟网站的图片素材网站推广的软件
  • 网站需要几个人网站推广投放
  • 网站建设杭州网站推广关键词工具
  • 定制网站开发价格游戏推广渠道有哪些
  • 中国建筑招聘2022seo推广 课程
  • 教做糕点的视频网站西安网络seo公司
  • 软件app开发公司海报seo关键词排名优化的方法
  • 网站分屏布局设计黄冈网站搭建推荐
  • 联通公网ip申请 做网站免费网站服务器安全软件下载
  • 西安市政府网站建设概况长春seo公司哪家好
  • 专业商城网站建设公司徐州seo外包平台
  • 泉州网站开发seo北京优化
  • 哪家公司做网站比较好手游推广渠道平台
  • 做电商卖玉器的网站网络营销公司业务范围