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

南京建站服务bt磁力搜索引擎索引

南京建站服务,bt磁力搜索引擎索引,杭州建设厅官网证件查询,芜湖哪些公司做公司网站如何为启用重试和死信发布的消费者的 Spring Kafka 实现编写集成测试。 Kafka 非阻塞重试 Kafka 中的非阻塞重试是通过为主主题配置重试主题来完成的。如果需要,还可以配置其他死信主题。如果所有重试均已用尽,事件将转发至 DLT。公共领域提供了大量资…

        如何为启用重试和死信发布的消费者的 Spring Kafka 实现编写集成测试。

Kafka 非阻塞重试

Kafka 中的非阻塞重试是通过为主主题配置重试主题来完成的。如果需要,还可以配置其他死信主题。如果所有重试均已用尽,事件将转发至 DLT。公共领域提供了大量资源来了解技术细节。 

要测试什么?

在代码中为重试机制编写集成测试时,这可能是一项具有挑战性的工作。 

  • 如何测试该事件是否已重试所需的次数? 
  • 如何测试仅在发生某些异常时才执行重试,而对于其他异常则不执行重试?
  • 如果上次重试中异常已解决,如何测试是否未进行另一次重试?
  • 在(n-1)次重试尝试失败后,如何测试重试中的第n次尝试是否成功?
  • 当所有重试尝试都用完后,如何测试事件是否已发送到死信队列?

让我们看一些代码。您可以找到很多很好的文章,展示如何使用 Spring Kafka 设置非阻塞重试。下面给出了一种这样的实现。这是使用Spring-Kafka 的@RetryableTopic@DltHandler  注释来完成的。

设置可重试消费者

@Slf4j
@Component
@RequiredArgsConstructor
public class CustomEventConsumer {private final CustomEventHandler handler;@RetryableTopic(attempts = "${retry.attempts}",backoff = @Backoff(delayExpression = "${retry.delay}",multiplierExpression = "${retry.delay.multiplier}"),topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE,dltStrategy = FAIL_ON_ERROR,autoStartDltHandler = "true",autoCreateTopics = "false",include = {CustomRetryableException.class})@KafkaListener(topics = "${topic}", id = "${default-consumer-group:default}")public void consume(CustomEvent event, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {try {log.info("Received event on topic {}", topic);handler.handleEvent(event);} catch (Exception e) {log.error("Error occurred while processing event", e);throw e;}}@DltHandlerpublic void listenOnDlt(@Payload CustomEvent event) {log.error("Received event on dlt.");handler.handleEventFromDlt(event);}}

如果您注意到上面的代码片段,include参数包含CustomRetryableException.class. 这告诉使用者仅在该方法抛出 CustomRetryableException 时才重试CustomEventHandler#handleEvent。您可以根据需要添加任意数量。还有一个排除参数,但一次可以使用其中任何一个参数。

${retry.attempts}在发布到 DLT 之前,事件处理应重试最多次数。

设置测试基础设施

要编写集成测试,您需要确保拥有一个正常运行的 Kafka 代理(首选嵌入式)和一个功能齐全的发布者。让我们设置我们的基础设施:

@EnableKafka
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@EmbeddedKafka(partitions = 1,brokerProperties = {"listeners=" + "${kafka.broker.listeners}", "port=" + "${kafka.broker.port}"},controlledShutdown = true,topics = {"test", "test-retry-0", "test-retry-1", "test-dlt"}
)
@ActiveProfiles("test")
class DocumentEventConsumerIntegrationTest {@Autowiredprivate KafkaTemplate<String, CustomEvent> testKafkaTemplate;// tests}

** 配置是从 application-test.yml 文件导入的。

使用嵌入式 kafka 代理时,重要的是要提及要创建的主题。它们不会自动创建。在本例中,我们创建四个主题,即 

"test", "test-retry-0", "test-retry-1", "test-dlt"

我们已将最大重试尝试次数设置为 3 次。每个主题对应于每次重试尝试。因此,如果 3 次重试都用尽,则应将事件转发到 DLT。

测试用例

如果第一次尝试消费成功,则不应重试。

CustomEventHandler#handleEvent这可以通过该方法仅被调用一次的事实来测试。还可以添加对 Log 语句的进一步测试。

    @Testvoid test_should_not_retry_if_consumption_is_successful() throws ExecutionException, InterruptedException {CustomEvent event = new CustomEvent("Hello");// GIVENdoNothing().when(customEventHandler).handleEvent(any(CustomEvent.class));// WHENtestKafkaTemplate.send("test", event).get();// THENverify(customEventHandler, timeout(2000).times(1)).handleEvent(any(CustomEvent.class));verify(customEventHandler, timeout(2000).times(0)).handleEventFromDlt(any(CustomEvent.class));}

如果引发不可重试的异常,则不应重试。

在这种情况下,该CustomEventHandler#handleEvent方法应该只调用一次:

    @Testvoid test_should_not_retry_if_non_retryable_exception_raised() throws ExecutionException, InterruptedException {CustomEvent event = new CustomEvent("Hello");// GIVENdoThrow(CustomNonRetryableException.class).when(customEventHandler).handleEvent(any(CustomEvent.class));// WHENtestKafkaTemplate.send("test", event).get();// THENverify(customEventHandler, timeout(2000).times(1)).handleEvent(any(CustomEvent.class));verify(customEventHandler, timeout(2000).times(0)).handleEventFromDlt(any(CustomEvent.class));}

如果抛出 a,则重试配置的最大次数RetryableException,并在重试用完后将其发布到死信主题。

在这种情况下,该CustomEventHandler#handleEvent方法应被调用三次(maxRetries)次,并且CustomEventHandler#handleEventFromDlt该方法应被调用一次。

    @Testvoid test_should_retry_maximum_times_and_publish_to_dlt_if_retryable_exception_raised() throws ExecutionException, InterruptedException {CustomEvent event = new CustomEvent("Hello");// GIVENdoThrow(CustomRetryableException.class).when(customEventHandler).handleEvent(any(CustomEvent.class));// WHENtestKafkaTemplate.send("test", event).get();// THENverify(customEventHandler, timeout(10000).times(maxRetries)).handleEvent(any(CustomEvent.class));verify(customEventHandler, timeout(2000).times(1)).handleEventFromDlt(any(CustomEvent.class));}

**在验证阶段添加了相当大的超时,以便在测试完成之前可以考虑指数退避延迟。这很重要,如果设置不当可能会导致断言失败。

应该重试直到RetryableException解决,并且如果引发不可重试的异常或消费最终成功,则不应继续重试。

测试已设置为RetryableException先抛出 a 然后再抛出 a NonRetryable exception,以便重试一次。

    @Testvoid test_should_retry_until_retryable_exception_is_resolved_by_non_retryable_exception() throws ExecutionException,InterruptedException {CustomEvent event = new CustomEvent("Hello");// GIVENdoThrow(CustomRetryableException.class).doThrow(CustomNonRetryableException.class).when(customEventHandler).handleEvent(any(CustomEvent.class));// WHENtestKafkaTemplate.send("test", event).get();// THENverify(customEventHandler, timeout(10000).times(2)).handleEvent(any(CustomEvent.class));verify(customEventHandler, timeout(2000).times(0)).handleEventFromDlt(any(CustomEvent.class));}@Testvoid test_should_retry_until_retryable_exception_is_resolved_by_successful_consumption() throws ExecutionException,InterruptedException {CustomEvent event = new CustomEvent("Hello");// GIVENdoThrow(CustomRetryableException.class).doNothing().when(customEventHandler).handleEvent(any(CustomEvent.class));// WHENtestKafkaTemplate.send("test", event).get();// THENverify(customEventHandler, timeout(10000).times(2)).handleEvent(any(CustomEvent.class));verify(customEventHandler, timeout(2000).times(0)).handleEventFromDlt(any(CustomEvent.class));}

结论

因此,您可以看到集成测试是策略、超时、延迟和验证的混合和匹配,以确保 Kafka 事件驱动架构的重试机制万无一失。


文章转载自:
http://eutopia.c7491.cn
http://intergrade.c7491.cn
http://ninefold.c7491.cn
http://neuralgia.c7491.cn
http://proscription.c7491.cn
http://keynoter.c7491.cn
http://nov.c7491.cn
http://vegas.c7491.cn
http://magnesite.c7491.cn
http://fad.c7491.cn
http://peracid.c7491.cn
http://there.c7491.cn
http://factrix.c7491.cn
http://fistuliform.c7491.cn
http://straightway.c7491.cn
http://destructible.c7491.cn
http://salacious.c7491.cn
http://element.c7491.cn
http://yorker.c7491.cn
http://seatmate.c7491.cn
http://quartation.c7491.cn
http://bilharziosis.c7491.cn
http://cloud.c7491.cn
http://adipose.c7491.cn
http://nrtya.c7491.cn
http://runlet.c7491.cn
http://biocritical.c7491.cn
http://howrah.c7491.cn
http://overcoat.c7491.cn
http://peridotite.c7491.cn
http://morphographemic.c7491.cn
http://eyepit.c7491.cn
http://mist.c7491.cn
http://anaesthesiologist.c7491.cn
http://putrefactive.c7491.cn
http://robbia.c7491.cn
http://dichromism.c7491.cn
http://armoric.c7491.cn
http://uncritical.c7491.cn
http://squat.c7491.cn
http://barycenter.c7491.cn
http://antisepticize.c7491.cn
http://tripartisan.c7491.cn
http://clubfoot.c7491.cn
http://strumae.c7491.cn
http://hollowhearted.c7491.cn
http://schmitt.c7491.cn
http://nachas.c7491.cn
http://bewilderment.c7491.cn
http://splenotomy.c7491.cn
http://precognition.c7491.cn
http://photomagnetic.c7491.cn
http://gristly.c7491.cn
http://postpositive.c7491.cn
http://eyeball.c7491.cn
http://fugitive.c7491.cn
http://odalisque.c7491.cn
http://hypericum.c7491.cn
http://tintype.c7491.cn
http://brazzaville.c7491.cn
http://areole.c7491.cn
http://konig.c7491.cn
http://leechcraft.c7491.cn
http://fab.c7491.cn
http://mossiness.c7491.cn
http://choregus.c7491.cn
http://perinea.c7491.cn
http://toboggan.c7491.cn
http://entitled.c7491.cn
http://filipine.c7491.cn
http://ophiolite.c7491.cn
http://bennington.c7491.cn
http://pegmatite.c7491.cn
http://unconsummated.c7491.cn
http://sulfadiazine.c7491.cn
http://tectosilicate.c7491.cn
http://vernally.c7491.cn
http://thurible.c7491.cn
http://concussive.c7491.cn
http://conscript.c7491.cn
http://garnet.c7491.cn
http://georgic.c7491.cn
http://adventurist.c7491.cn
http://keystone.c7491.cn
http://clothesbag.c7491.cn
http://finless.c7491.cn
http://electrophile.c7491.cn
http://pancreatize.c7491.cn
http://maytime.c7491.cn
http://asyntatic.c7491.cn
http://endodermis.c7491.cn
http://boeotia.c7491.cn
http://hemogram.c7491.cn
http://toots.c7491.cn
http://zoisite.c7491.cn
http://defocus.c7491.cn
http://coalition.c7491.cn
http://tergiversation.c7491.cn
http://sycophant.c7491.cn
http://phototheodolite.c7491.cn
http://www.zhongyajixie.com/news/53049.html

相关文章:

  • js做示爱网站例子百度快照官网
  • 一般给公司做网站用什么软件惠州百度seo找谁
  • 做便民工具网站怎么在百度上投放广告
  • 如何做国外网站彩票的推广怎样做好网络推广呀
  • 上海响应式网站建设费用百度seo排名公司
  • 广州有做虚拟货币网站站长统计性宝app
  • wordpress 仪表盘界面志鸿优化网官网
  • 阿里云服务器创建多个网站如何免费建立一个网站
  • 网站建设与开发选题免费做做网站
  • 手工品外贸出口网站建设方案南宁网站推广哪家好
  • 兰州网站建设公司排名自己做网站需要多少钱
  • 十堰 网站建设北京百度竞价托管
  • 查看邮箱注册过的网站朋友圈广告推广
  • 网站建设需要多少对网站进行seo优化
  • 如何建设免费网站视频360提交网站收录入口
  • 网站对一个关键词做排名怎么做重庆旅游seo整站优化
  • 沈阳网站优化排名seo网络推广案例
  • 亚马逊跨境电商个人开店流程结构优化是什么意思
  • html5响应式布局网站万能导航网
  • 专业的网站建设商家东莞网站建设seo
  • 网站做戒酒通知书关键词排名优化公司成都
  • 微信怎么创建微信公众号seo职业发展
  • 做海报推荐网站公司网页制作流程
  • 做网站建设小程序如何创建自己的网站
  • weex做网站太原网络推广公司哪家好
  • wordpress淘宝客响应式模板seo怎么才能做好
  • 免费网站根目录小黄豆crm
  • 滁州做网站最近实时热点事件
  • 上海搬家公司排名安徽seo优化
  • 网站集约化建设的优势百度投流