Skip to content

Commit a7fb2e8

Browse files
committed
Spring Cloud Stream消费失败后的处理策略(一):自动重试
1 parent d679bc6 commit a7fb2e8

File tree

4 files changed

+171
-1
lines changed

4 files changed

+171
-1
lines changed

4-Finchley/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,17 @@
99
<version>1.0</version>
1010

1111
<modules>
12-
<module>stream-consumer-self</module> <!-- 消费自己的消息 -->
12+
<!-- 消费自己的消息 -->
13+
<module>stream-consumer-self</module>
14+
<!-- 消息重试 -->
15+
<module>stream-exception-handler-1</module>
16+
17+
<!-- 重新入队 -->
18+
19+
<!-- 自定义降级方法 -->
20+
21+
<!-- 使用DLQ队列(RabbitMQ) -->
22+
1323

1424
</modules>
1525

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>stream-exception-handler-1</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.5.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.projectlombok</groupId>
27+
<artifactId>lombok</artifactId>
28+
<version>1.18.2</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework.cloud</groupId>
38+
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-actuator</artifactId>
43+
</dependency>
44+
</dependencies>
45+
46+
<dependencyManagement>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.springframework.cloud</groupId>
50+
<artifactId>spring-cloud-dependencies</artifactId>
51+
<version>Finchley.SR1</version>
52+
<type>pom</type>
53+
<scope>import</scope>
54+
</dependency>
55+
</dependencies>
56+
</dependencyManagement>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.springframework.boot</groupId>
62+
<artifactId>spring-boot-maven-plugin</artifactId>
63+
</plugin>
64+
</plugins>
65+
</build>
66+
</project>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.didispace.stream;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.stream.annotation.EnableBinding;
8+
import org.springframework.cloud.stream.annotation.Input;
9+
import org.springframework.cloud.stream.annotation.Output;
10+
import org.springframework.cloud.stream.annotation.StreamListener;
11+
import org.springframework.integration.support.MessageBuilder;
12+
import org.springframework.messaging.MessageChannel;
13+
import org.springframework.messaging.SubscribableChannel;
14+
import org.springframework.stereotype.Component;
15+
import org.springframework.web.bind.annotation.GetMapping;
16+
import org.springframework.web.bind.annotation.RequestParam;
17+
import org.springframework.web.bind.annotation.RestController;
18+
19+
20+
@EnableBinding(TestApplication.TestTopic.class)
21+
@SpringBootApplication
22+
public class TestApplication {
23+
24+
public static void main(String[] args) {
25+
SpringApplication.run(TestApplication.class, args);
26+
}
27+
28+
@RestController
29+
static class TestController {
30+
31+
@Autowired
32+
private TestTopic testTopic;
33+
34+
/**
35+
* 消息生产接口
36+
*
37+
* @param message
38+
* @return
39+
*/
40+
@GetMapping("/sendMessage")
41+
public String messageWithMQ(@RequestParam String message) {
42+
testTopic.output().send(MessageBuilder.withPayload(message).build());
43+
return "ok";
44+
}
45+
46+
}
47+
48+
/**
49+
* 消息消费逻辑
50+
*/
51+
@Slf4j
52+
@Component
53+
static class TestListener {
54+
55+
int counter = 1;
56+
57+
@StreamListener(TestTopic.INPUT)
58+
public void receive(String payload) {
59+
log.info("Received: " + payload + ", " + counter);
60+
throw new RuntimeException("Message consumer failed!");
61+
62+
// 计数,模拟重试过程中成功消费
63+
// if (counter == 3) {
64+
// counter = 1;
65+
// return;
66+
// } else {
67+
// counter++;
68+
// throw new RuntimeException("Message consumer failed!");
69+
// }
70+
}
71+
72+
}
73+
74+
interface TestTopic {
75+
76+
String OUTPUT = "example-topic-output";
77+
String INPUT = "example-topic-input";
78+
79+
@Output(OUTPUT)
80+
MessageChannel output();
81+
82+
@Input(INPUT)
83+
SubscribableChannel input();
84+
85+
}
86+
87+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.application.name=stream-exception-handler-1
2+
server.port=8080
3+
4+
spring.cloud.stream.bindings.example-topic-input.destination=test-topic
5+
spring.cloud.stream.bindings.example-topic-input.consumer.max-attempts=1
6+
7+
spring.cloud.stream.bindings.example-topic-output.destination=test-topic

0 commit comments

Comments
 (0)