Skip to content

Commit 5f7e5de

Browse files
committed
Spring Cloud Stream同一通道根据消息内容分发不同的消费逻辑
1 parent 25a8b3b commit 5f7e5de

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
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-content-route</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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.messaging.handler.annotation.Header;
15+
import org.springframework.stereotype.Component;
16+
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.RequestParam;
18+
import org.springframework.web.bind.annotation.RestController;
19+
20+
21+
@EnableBinding(TestApplication.TestTopic.class)
22+
@SpringBootApplication
23+
public class TestApplication {
24+
25+
public static void main(String[] args) {
26+
SpringApplication.run(TestApplication.class, args);
27+
}
28+
29+
@RestController
30+
static class TestController {
31+
32+
@Autowired
33+
private TestTopic testTopic;
34+
35+
/**
36+
* 消息生产接口
37+
*
38+
* @param message
39+
* @return
40+
*/
41+
@GetMapping("/sendMessage")
42+
public String messageWithMQ(@RequestParam String message) {
43+
testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "1.0").build());
44+
testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "2.0").build());
45+
return "ok";
46+
}
47+
48+
}
49+
50+
/**
51+
* 消息消费逻辑
52+
*/
53+
@Slf4j
54+
@Component
55+
static class TestListener {
56+
57+
@StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='1.0'")
58+
public void receiveV1(String payload, @Header("version") String version) {
59+
log.info("Received v1 : " + payload + ", " + version);
60+
}
61+
62+
@StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='2.0'")
63+
public void receiveV2(String payload, @Header("version") String version) {
64+
log.info("Received v2 : " + payload + ", " + version);
65+
}
66+
67+
}
68+
69+
interface TestTopic {
70+
71+
String OUTPUT = "example-topic-output";
72+
String INPUT = "example-topic-input";
73+
74+
@Output(OUTPUT)
75+
MessageChannel output();
76+
77+
@Input(INPUT)
78+
SubscribableChannel input();
79+
80+
}
81+
82+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
spring.application.name=stream-content-route
2+
server.port=8080
3+
4+
spring.cloud.stream.bindings.example-topic-input.destination=test-topic
5+
spring.cloud.stream.bindings.example-topic-input.group=stream-content-route
6+
7+
spring.cloud.stream.bindings.example-topic-output.destination=test-topic

0 commit comments

Comments
 (0)