spring - 如何配置 Spring Cloud Stream 发布者重试?
问题描述
我正在使用 RabbitMQ 活页夹。
Spring Cloud Stream 允许开发人员在消费消息发生异常时重试。
当 RabbitMQ 连接丢失时,生产者可能会失败。我们如何配置 SCS 以便在生成消息时发生任何错误时重试?或者有没有办法在那里应用断路器?
谢谢
解决方案
您可以使用标准的Spring Boot 属性(retry.enabled
等)——向下滚动到 rabbitmq——在生产者端配置重试。binder 会将重试模板连接到出站适配器的RabbitTemplate
.
spring.rabbitmq.template.retry.enabled=false # Whether publishing retries are enabled.
spring.rabbitmq.template.retry.initial-interval=1000ms # Duration between the first and second attempt to deliver a message.
spring.rabbitmq.template.retry.max-attempts=3 # Maximum number of attempts to deliver a message.
spring.rabbitmq.template.retry.max-interval=10000ms # Maximum duration between attempts.
spring.rabbitmq.template.retry.multiplier=1 # Multiplier to apply to the previous retry interval.
这是活页夹中的代码...
if (rabbitProperties != null && rabbitProperties.getTemplate().getRetry().isEnabled()) {
Retry retry = rabbitProperties.getTemplate().getRetry();
RetryPolicy retryPolicy = new SimpleRetryPolicy(retry.getMaxAttempts());
ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy();
backOff.setInitialInterval(retry.getInitialInterval().toMillis());
backOff.setMultiplier(retry.getMultiplier());
backOff.setMaxInterval(retry.getMaxInterval().toMillis());
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setBackOffPolicy(backOff);
rabbitTemplate.setRetryTemplate(retryTemplate);
}
推荐阅读
- sql-server - 使用 SQL Server 脚本远程移动文件
- delphi - Delphi通过子字符串搜索比较2个字符串列表并将匹配项合并到另一个字符串列表中
- ios - 访问通话期间出现的 AlertController
- angular - 在 Angular 2+ 中使用 Observables 时发生的错误
- spring-boot - Spring Boot中的分页带有表单参数
- python - 在 Hy 中指定元类的语法
- javascript - 单击单选按钮时如何激活下拉列表
- .htaccess - 如何在不同端口上的 codeigniter 中删除 index.php
- java - Spring WebSocket StompHeaderAccessor
- branch - 在 Fossil 中更新分支