- MAVEN Configuration
- Producer Configuration
- application.yml Configuration
- application.properties configuration
- Define Output Binding
- Turn on Binding (turn on MQ)
- send message
- Consumer Configuration
- application.yml Configuration
- Define Input Binding
- Turn on Binding (turn on MQ)
- Accept message
MAVEN Configuration
<!-- spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!-- spring-cloud-rocketmq --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rocketmq</artifactId> <version>0.2.2.BUILD-SNAPSHOT</version> </dependency> <!-- spring-cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.M3</version> <type>pom</type> <scope>import</scope> </dependency>
If not, add a configuration
<repositories> <repository> <id>spring-snapshot</id> <name>Spring Snapshot Repository</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> #If using private nexus, when adding this proxy warehouse, note the warehouse type: snapshot allows proxy
Producer Configuration
application.yml Configuration
spring: cloud: stream: default-binder: rocketmq rocketmq: binder: #rocketmq address name-server: 192.168.0.78:9876 bindings: #Custom names correspond to spring.cloud.stream.bindings.output1 output1: producer: group: test-group-user-ouput1 sync: true #Binding: Includes Input Binding and Output Binding. #Binding provides a bridge between messaging middleware and providers and onsumers provided by applications. #Developers can only use the application's Provider or Consumer production or consumption data to shield them from the underlying messaging middleware. bindings: #Custom Name output1: destination: test-topic-user # Topic (first-level classification) content-type: application/json
application.properties configuration
spring.cloud.stream.default-binder=rocketmq spring.cloud.stream.rocketmq.binder.name-server=192.168.0.78:9876 spring.cloud.stream.rocketmq.bindings.output1.producer.group=test-group-user-ouput1 spring.cloud.stream.rocketmq.bindings.output1.producer.sync=true spring.cloud.stream.bindings.output1.destination=test-topic-user spring.cloud.stream.bindings.output1.content-type=application/json
Define Output Binding
Output Binding with name output1
public interface MySource { @Output("output1") MessageChannel output1(); }
Turn on Binding (turn on MQ)
Add Comments to Application s
@SpringBootApplication @EnableBinding({ MySource.class }) //MySource defines the Binding interface above public class RocketMQConsumerApplication { ... }
send message
Injection Definition Bingding Interface
@Autowired private MySource source;
- Send messages directly without tag s and key s
String msg = ...; source.output1().send(MessageBuilder.withPayload(msg).build());
- Send message with tag
Object msg = ...; String tag = "test-tag";//tag is a secondary category, top is a primary category and can be subscribed to by two categories Message message = MessageBuilder.createMessage(msg, new MessageHeaders(Stream.of(tag).collect(Collectors.toMap(str -> MessageConst.PROPERTY_TAGS, String::toString)))); source.output1().send(message);
- Send Object
Object msg = ...; String tag = "test-tag" Message message = MessageBuilder.withPayload(msg) .setHeader(MessageConst.PROPERTY_TAGS, tag) .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); source.output1().send(message);
- Send Transaction Message
// TODO
Consumer Configuration
application.yml Configuration
spring: cloud: stream: default-binder: rocketmq rocketmq: binder: name-server: 192.168.1.179:9876 bindings: #Custom names correspond to spring.cloud.stream.bindings.input1 input1: consumer: tags: test-tag1 # Tags for subscriptions, secondary classification orderly: false # Whether to consume in order bindings: #Custom Name input1: destination: test-topic-user # Subscription Topics, Level 1 Category content-type: application/json group: test-input-group1 #group consumer: concurrency: 20 maxAttempts: 1
Define Input Binding
Input Binding defined as name input1
public interface MySource { @Input("input1") SubscribableChannel input1(); }
Turn on Binding (turn on MQ)
Add Comments to Application s
@SpringBootApplication @EnableBinding({ MySource.class }) //MySource defines the Binding interface above public class RocketMQConsumerApplication { ... }
Accept message
/** * Consumer */ @Service public class ReceiveService { // Accept String // @StreamListener("input1") // public void receiveInput1( String receiveMsg) { // System.out.println("input1 receive: " + receiveMsg); // } /** * Accept Object */ @StreamListener("input1") public void receiveInput3(@Payload List<BranchInfoEntity> list ) { System.out.println("input3 receive: " + JSON.toJSONString(list)); } }