• SpringCloud集成RocketMQ


    SpringCloud集成RocketMQ

    前言

    RocketMQ 是阿里巴巴在2012年开源的分布式消息中间件,2017年成为 Apache 的顶级项目;以其高性能、低延时和高可靠等特性近年来已经也被越来越多的企业使用; 今天这个文章就介绍一下在SpringCloud里如何集成RocketMQ作为消息中间件的使用,本文介绍的是通过SpringCloud Stream方式进行集成;

    引入依赖包

    1. <dependency>
    2. <groupId>com.alibaba.cloudgroupId>
    3. <artifactId>spring-cloud-starter-stream-rocketmqartifactId>
    4. dependency>

    生产者Producer

    加注解

    在主启动类上加上@EnableBinding(Source.class)

    1. @SpringBootApplication
    2. @EnableBinding(Source.class)
    3. public class ProducerApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(ProducerApplication.class, args);
    6. }
    7. }

    Stream生产者配置

    1. spring:
    2. cloud:
    3. # Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
    4. stream:
    5. rocketmq:
    6. # RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
    7. binder:
    8. name-server: 192.168.56.101:9876 # RocketMQ Namesrv 地址
    9. # Binding 配置项,对应 BindingProperties Map
    10. bindings:
    11. output:
    12. destination: TOPIC-01 # 目的地。这里使用 RocketMQ Topic

    生产者发送消息

    在Controller类里, 通过IOC,获取Source,作为消费者,产生消息,然后使用Source发送消息

    1. @Autowired
    2. private Source source;
    3. @GetMapping("/produce")
    4. public boolean produce(String msg) {
    5. MyMessage message = new MyMessage(msg).setId(new Random().nextInt());
    6. Message springMessage = MessageBuilder.withPayload(message)
    7. .build();
    8. return source.output().send(springMessage);
    9. }

    消费者(Consumer)

    加注解

    在主启动类上加上@EnableBinding(Sink.class)

    1. @EnableBinding(Sink.class)
    2. public class ConsumerApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(ConsumerApplication.class, args);
    5. }
    6. }

    Stream消费者配置

    1. spring:
    2. cloud:
    3. # Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
    4. stream:
    5. rocketmq:
    6. # RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
    7. binder:
    8. name-server: 192.168.56.101:9876 # RocketMQ Namesrv 地址
    9. # Binding 配置项,对应 BindingProperties Map
    10. bindings:
    11. input:
    12. # Topic和生产者一样,这样才能接收到生产者的Message
    13. destination: TOPIC-01 # 目的地。这里使用 RocketMQ Topic
    14. # Group组设置, 可以按自己的需要进行名称设置
    15. group: group-001

    消费者处理消息

    定义Component;在方法上,添加 @StreamListener 注解,声明对应的 Input Binding。该方法就可以处理对应队列中的消息了,类似于以前的onMessage这样的方式

    1. @Component
    2. public class MyConsumer {
    3. private Logger logger = LoggerFactory.getLogger(MyConsumer.class);
    4. @StreamListener(Sink.INPUT)
    5. public void onMessage(@Payload MyMessage message) {
    6. logger.info("[onMessage][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
    7. }
    8. }

    自定义SOURCE

    我们在做生产者的时候,使用

    @EnableBinding(Source.class)

    来进行Source的定义,在生产者stream的配置里,默认为output;我们可以使用自己定义的source来扩展stream里的生产者配置

    定义接口

    1. public interface MySource {
    2. @Output("logger-output")
    3. MessageChannel logger();
    4. @Output("data-output")
    5. MessageChannel data();
    6. }

    可以将@EnableBinding改为

    @EnableBinding(Source.class, MySource.class)

    生产者Stream配置扩展

    1. spring:
    2. cloud:
    3. # Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
    4. stream:
    5. rocketmq:
    6. # RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
    7. binder:
    8. name-server: 192.168.56.101:9876 # RocketMQ Namesrv 地址
    9. # Binding 配置项,对应 BindingProperties Map
    10. bindings:
    11. logger-output:
    12. destination: TOPIC-LOGGER-01 # 目的地。这里使用 RocketMQ Topic
    13. content-type: application/json # 内容格式。这里使用 JSON
    14. data-output:
    15. destination: TOPIC-DATA-01 # 目的地。这里使用 RocketMQ Topic
    16. content-type: application/json # 内容格式。这里使用 JSON

    发送消息

    1. @Autowired
    2. private MySource mySource;
    3. @GetMapping("/produce/logger")
    4. public boolean produceLogger(String msg) {
    5. MyMessage message = new MyMessage(msg).setId(new Random().nextInt());
    6. Message springMessage = MessageBuilder.withPayload(message)
    7. .build();
    8. return mySource.logger().send(springMessage);
    9. }
    10. @GetMapping("/produce/data")
    11. public boolean produceData(String msg) {
    12. MyMessage message = new MyMessage(msg).setId(new Random().nextInt());
    13. Message springMessage = MessageBuilder.withPayload(message)
    14. .build();
    15. return mySource.data().send(springMessage);
    16. }

    自定义SINK

    我们在做消费者的时候,使用

    @EnableBinding(Sink.class)

    来进行Sink的定义,在消费者stream的配置里,默认为input;我们可以使用自己定义的Sink来扩展stream里的消费者配置

    定义接口

    1. public interface MySink {
    2. String LOGGER_INPUT = "logger-input";
    3. String DATA_INPUT = "data-input";
    4. @Input(LOGGER_INPUT )
    5. SubscribableChannel logger();
    6. @Input(DATA_INPUT )
    7. SubscribableChannel data();
    8. }

    可以将@EnableBinding改为

    @EnableBinding(Sink.class, MySink.class)

    消费者Stream配置扩展

    1. spring:
    2. cloud:
    3. # Spring Cloud Stream 配置项,对应 BindingServiceProperties 类
    4. stream:
    5. rocketmq:
    6. # RocketMQ Binder 配置项,对应 RocketMQBinderConfigurationProperties 类
    7. binder:
    8. name-server: 192.168.56.101:9876 # RocketMQ Namesrv 地址
    9. # Binding 配置项,对应 BindingProperties Map
    10. bindings:
    11. logger-input:
    12. # Topic和生产者一样,这样才能接收到生产者的Message
    13. destination: TOPIC-LOGGER-01 # 目的地。这里使用 RocketMQ Topic
    14. # Group组设置, 可以按自己的需要进行名称设置
    15. group: group-001
    16. data-input:
    17. # Topic和生产者一样,这样才能接收到生产者的Message
    18. destination: TOPIC-DATA-01 # 目的地。这里使用 RocketMQ Topic
    19. # Group组设置, 可以按自己的需要进行名称设置
    20. group: group-001

    处理消息

    1. @Component
    2. public class MyConsumer {
    3. private Logger logger = LoggerFactory.getLogger(MyConsumer.class);
    4. @StreamListener(MySink.LOGGER_INPUT)
    5. public void onLogger(@Payload MyMessage message) {
    6. logger.info("[onLogger][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
    7. }
    8. @StreamListener(MySink.DATA_INPUT)
    9. public void onData(@Payload MyMessage message) {
    10. logger.info("[onData][线程编号:{} 消息内容:{}]", Thread.currentThread().getId(), message);
    11. }
    12. }

    结束语

    通过上面的集成方式,我们使用springcloud的stream框架,分别完成了RocketMQ的消息生产者和用来处理消息的消费者;通过springcloud stream的这种集成方式,在下一个文章里,我们将基于本文中的实现过程,深入到springcloud的stream框架里去进行stream的认知。

    谢谢大家继续关注

  • 相关阅读:
    架构师范文(AI写作)两篇
    Flask详解
    零基础入门MATLAB(一篇十分钟)
    Rust变量与数据类型
    OFDM 十六讲 7 - Inter-Symbol-Interference
    使用C语言实现查找
    rtmp推流异常分析
    Bash脚本自学 - 变量和位置自变量
    【老板要你啥都会系列】| 前端晋升全栈--项目日志
    《当代教育实践与教学研究》期刊简介及投稿要求
  • 原文地址:https://blog.csdn.net/inthirties/article/details/126677781