700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java B2B2C 源码 Springcloud多租户电子商城系统- Stream重新入队(RabbitMQ)

java B2B2C 源码 Springcloud多租户电子商城系统- Stream重新入队(RabbitMQ)

时间:2022-02-21 08:19:30

相关推荐

java B2B2C 源码 Springcloud多租户电子商城系统- Stream重新入队(RabbitMQ)

本文将介绍RabbitMQ的binder提供的重试功能:重新入队 需要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六 准备一个会消费失败的例子,可以直接沿用前文的工程,也可以新建一个,然后创建如下代码的逻辑:

@EnableBinding(TestApplication.TestTopic.class)@SpringBootApplicationpublic class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}@RestControllerstatic class TestController {@Autowiredprivate TestTopic testTopic;/*** 消息生产接口** @param message* @return*/@GetMapping("/sendMessage")public String messageWithMQ(@RequestParam String message) {testTopic.output().send(MessageBuilder.withPayload(message).build());return "ok";}}/*** 消息消费逻辑*/@Slf4j@Componentstatic class TestListener {private int count = 1;@StreamListener(TestTopic.INPUT)public void receive(String payload) {log.info("Received payload : " + payload + ", " + count);throw new RuntimeException("Message consumer failed!");}}interface TestTopic {String OUTPUT = "example-topic-output";String INPUT = "example-topic-input";@Output(OUTPUT)MessageChannel output();@Input(INPUT)SubscribableChannel input();}}复制代码

内容很简单,既包含了消息的生产,也包含了消息消费。消息消费的时候主动抛出了一个异常来模拟消息的消费失败。

在启动应用之前,还要记得配置一下输入输出通道对应的物理目标(exchange或topic名)、并设置一下分组,比如:

spring.cloud.stream.bindings.example-topic-input.destination=test-topicspring.cloud.stream.bindings.example-topic-input.group=stream-exception-handlerspring.cloud.stream.bindings.example-topic-input.consumer.max-attempts=1spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.requeue-rejected=truespring.cloud.stream.bindings.example-topic-output.destination=test-topic复制代码

完成了上面配置之后,启动应用并访问localhost:8080/sendMessage?message=hello接口来发送一个消息到MQ中了,此时可以看到程序不断的抛出了消息消费异常。这是由于这里我们多加了一个配置:spring.cloud.stream.rabbit.bindings.example-topic-input.consumer.requeue-rejected=true。在该配置作用之下,消息消费失败之后,并不会将该消息抛弃,而是将消息重新放入队列,所以消息的消费逻辑会被重复执行,直到这条消息消费成功为止。 springboot微服务多用户商城系统java_代码开源_B2B电商系统_B2C电商系统

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。