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

展览搭建设计网站软件系统开发公司

展览搭建设计网站,软件系统开发公司,高米店网站建设公司,手机报价更多SpringBoot3内容请关注我的专栏#xff1a;《SpringBoot3》 期待您的点赞??收藏评论 重学SpringBoot3-Spring Retry实践 1. 简介2. 环境准备3. 使用方式 3.1 注解方式 基础使用自定义重试策略失败恢复机制重试和失败恢复效果注意事项 3.2 编程式使用3.3 监听重试过程 监… 更多SpringBoot3内容请关注我的专栏《SpringBoot3》 期待您的点赞??收藏评论 重学SpringBoot3-Spring Retry实践 1. 简介2. 环境准备3. 使用方式 3.1 注解方式 基础使用自定义重试策略失败恢复机制重试和失败恢复效果注意事项 3.2 编程式使用3.3 监听重试过程 监听重试效果 4. 最佳实践5. 总结 1. 简介 Spring Retry是Spring生态系统中的一个重要组件它提供了自动重试失败操作的能力。在分布式系统中由于网络抖动、服务暂时不可用等临时性故障重试机制显得尤为重要。本文将详细介绍如何在 SpringBoot 3 应用中集成和使用 Spring Retry。 2. 环境准备 首先在 SpringBoot 3 项目中添加必要的依赖 dependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactIdversion2.0.5/version /dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion6.1.13/version /dependency在启动类或配置类上添加 EnableRetry 注解以启用重试功能 SpringBootApplication EnableRetry public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);} }3. 使用方式 3.1 注解方式 基础使用 最简单的使用方式是通过 Retryable 注解 Service public class UserService {Retryablepublic void riskyOperation() {// 可能失败的操作} }自定义重试策略 可以通过 Retryable 注解的参数来自定义重试行为 Service Slf4j public class EmailServiceImpl implements IEmailService {Resourceprivate JavaMailSender mailSender;Value(${spring.mail.username})private String from;/*** 发送简单文本邮件** param to* param subject* param text*/OverrideRetryable(retryFor MailSendException.class, maxAttempts 3, backoff Backoff(delay 1000))public void sendSimpleEmail(String to, String subject, String text) {try {SimpleMailMessage message new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(text);mailSender.send(message);log.info(Simple email sent successfully to: {}, to);} catch (Exception e) {log.error(Failed to send simple email, e);throw new MailSendException(Failed to send email, e);}} }当执行发生指定异常将会尝试进行重试一旦达到最大尝试次数但仍有异常发生就会抛出 ExhaustedRetryException。重试最多可进行三次两次重试之间的延迟时间默认为一秒。 失败恢复机制 使用 Recover 注解定义重试失败后的恢复方法 /*** 发送简单文本邮件** param to* param subject* param text*/OverrideRetryable(retryFor MailSendException.class, // 指定异常类型maxAttempts 3, // 最大重试次数backoff Backoff(delay 1000) // 指定退避策略例如延迟时间)public void sendSimpleEmail(String to, String subject, String text) {try {SimpleMailMessage message new SimpleMailMessage();message.setFrom(from);message.setTo(to);message.setSubject(subject);message.setText(text);mailSender.send(message);log.info(Simple email sent successfully to: {}, to);} catch (Exception e) {log.error(Failed to send simple email, e.getMessage());throw new MailSendException(Failed to send email, e);}}Recoverpublic void recover(MailSendException e, String param) {// 处理最终失败的情况log.error(Final recovery : {}, param);}重试和失败恢复效果 注意事项 注意Recover 失效的情况 Recover 方法的参数类型与实际异常不匹配Recover 方法的返回类型与 Retryable 方法不一致Recover 方法的其他参数与 Retryable 方法参数不匹配。 3.2 编程式使用 除了注解方式Spring Retry 还提供了 RetryTemplate 用于编程式重试 Configuration public class RetryConfig {Beanpublic RetryTemplate retryTemplate() {RetryTemplate template new RetryTemplate();// 配置重试策略SimpleRetryPolicy retryPolicy new SimpleRetryPolicy();retryPolicy.setMaxAttempts(3);// 配置退避策略FixedBackOffPolicy backOffPolicy new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(1000L);template.setRetryPolicy(retryPolicy);template.setBackOffPolicy(backOffPolicy);return template;} }使用RetryTemplate Service public class UserService {Autowiredprivate RetryTemplate retryTemplate;public void executeWithRetry() {retryTemplate.execute(context - {// 需要重试的业务逻辑return null;});} }3.3 监听重试过程 通过实现RetryListener接口可以监听重试的整个生命周期 public class CustomRetryListener extends RetryListenerSupport {Overridepublic T, E extends Throwable void onError(RetryContext context, RetryCallbackT, E callback, Throwable throwable) {// 记录错误日志log.error(Retry error occurred, throwable);}Overridepublic T, E extends Throwable void close(RetryContext context,RetryCallbackT, E callback, Throwable throwable) {// 重试结束时的处理log.info(Retry completed);} }将监听器注册到RetryTemplate Configuration public class RetryConfig {Beanpublic RetryTemplate retryTemplate() {RetryTemplate template new RetryTemplate();// ... 其他配置 ...template.registerListener(new CustomRetryListener());return template;} }监听重试效果 4. 最佳实践 明确重试场景只对临时性故障使用重试机制对于业务错误或永久性故障应直接失败。 设置合理的重试次数通常3-5次即可过多的重试可能会加重系统负担。 使用退避策略建议使用指数退避策略(ExponentialBackOffPolicy)避免立即重试对系统造成冲击。 添加监控和日志通过RetryListener记录重试情况便于问题排查。 设置超时时间避免重试过程持续时间过长。 5. 总结 Spring Retry为Spring应用提供了强大而灵活的重试机制既可以通过注解优雅地实现重试也可以使用RetryTemplate进行更细粒度的控制。在实际应用中合理使用重试机制可以提高系统的健壮性和可用性。 需要注意的是重试机制并非万能药在使用时要根据具体场景选择合适的重试策略并做好监控和告警以便及时发现和处理问题。
http://www.w-s-a.com/news/171468/

相关文章:

  • 安徽省建设部网站官网还能用的wap网站
  • 企业网站设计开发网站关键词优化seo
  • 郑州高档网站建设台州网站建设推广
  • 广东省建设信息港网站WordPress手机缩略图设置
  • 优秀网站主题平顶山专业做网站公司
  • wordpress返回顶部插件wordpress站群seo
  • 企业网站建设报价表百度竞价托管哪家好
  • 织梦网站首页打开慢淄博网站推广那家好
  • 苏州高端网站建设kgwl互动网站建设的主页
  • 宿州网站建设哪家公司好个人网站制作方法
  • 网站正能量晚上在线观看视频站长之家关键词挖掘工具
  • 建设网站怎么判断是电脑还是手机仿租号网站源码网站开发
  • seo百度网站排名软件重庆巫山网站设计公司
  • 搭建视频播放网站网站排名诊断
  • 网站域名注册网站centos做网站服务器
  • 网站服务器共享的 vpsh5页面制作软件电脑版
  • 免费手机网站申请上海网站建设设计公司哪家好
  • 站长工具大全企业网上书店网站建设设计
  • 做网站的专业公司公司网站是做的谷歌的
  • 做网站前期工作wordpress图片并排
  • 免费注册网站哪个好wordpress评论修改
  • 合肥模板网站建设软件赤峰公司网站建设
  • 毕业设计都是做网站吗深圳网站制作企业邮箱
  • 网站排名 优帮云小规模公司简介怎么写
  • 那个做头像的网站好选择手机网站建设
  • 设计一个网站花多少时间做视频网站适合用什么服务器
  • asp网站开发环境订单系统单页面网站怎么做
  • 山东网站建设都有那些企业推广策略
  • 网站开发文档是什么概念衣服销售网站建设规划书范文
  • 中国建筑装饰网官网企业网站设计优化公司