Spring Cloud Alibaba 异步通信 - RocketMQ 消费者

POM

主要增加了 org.springframework.cloud:spring-cloud-starter-stream-rocketmq 依赖

  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. <parent>
  6. <groupId>cn.dandelioncloud</groupId>
  7. <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
  8. <version>1.0.0-SNAPSHOT</version>
  9. <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
  10. </parent>
  11. <artifactId>hello-spring-cloud-alibaba-rocketmq-consumer</artifactId>
  12. <packaging>jar</packaging>
  13. <name>hello-spring-cloud-alibaba-rocketmq-consumer</name>
  14. <url>http://www.dandelioncloud.cn</url>
  15. <inceptionYear>2018-Now</inceptionYear>
  16. <dependencies>
  17. <!-- Spring Boot Begin -->
  18. <dependency>
  19. <groupId>org.springframework.boot</groupId>
  20. <artifactId>spring-boot-starter-web</artifactId>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-actuator</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-test</artifactId>
  29. <scope>test</scope>
  30. </dependency>
  31. <!-- Spring Boot End -->
  32. <!-- Spring Cloud Begin -->
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
  36. </dependency>
  37. <!-- Spring Cloud End -->
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. <configuration>
  45. <mainClass>cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.consumer.RocketMQConsumerApplication</mainClass>
  46. </configuration>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </project>

消息消费者服务

主要使用 @StreamListener("input") 注解来订阅从名为 input 的 Binding 中接收的消息

  1. package cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.consumer.receive;
  2. import org.springframework.cloud.stream.annotation.StreamListener;
  3. import org.springframework.stereotype.Service;
  4. @Service
  5. public class ConsumerReceive {
  6. @StreamListener("input")
  7. public void receiveInput(String message) {
  8. System.out.println("Receive input: " + message);
  9. }
  10. }

Application

配置 Input(Sink.class) 的 Binding 信息并配合 @EnableBinding 注解使其生效

  1. package cn.dandelioncloud.hello.spring.cloud.alibaba.rocketmq.consumer;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.stream.annotation.EnableBinding;
  5. import org.springframework.cloud.stream.messaging.Sink;
  6. @SpringBootApplication
  7. @EnableBinding({Sink.class})
  8. public class RocketMQConsumerApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(RocketMQConsumerApplication.class, args);
  11. }
  12. }

application.yml

  1. spring:
  2. application:
  3. name: rocketmq-consumer
  4. cloud:
  5. stream:
  6. rocketmq:
  7. binder:
  8. namesrv-addr: 192.168.10.149:9876
  9. bindings:
  10. input: {consumer.orderly: true}
  11. bindings:
  12. input: {destination: test-topic, content-type: text/plain, group: test-group, consumer.maxAttempts: 1}
  13. server:
  14. port: 9094
  15. management:
  16. endpoints:
  17. web:
  18. exposure:
  19. include: '*'

运行成功后即可在控制台接收到消息:Receive input: Hello RocketMQ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智慧浩海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值