全栈开发那些事

全栈开发那些事

Spring Cloud Stream整合RocketMQ

135
2024-06-25
Spring Cloud Stream整合RocketMQ

Spring Cloud Stream整合RocketMQ

这里书接上回,默认你已经搭建好了RocketMQ主从异步集群,前面文章已经介绍过搭建方法。

1、Spring Cloud Stream介绍

Spring Cloud Stream是一个框架,用于构建与共享消息系统连接的高度可扩展的事件驱动微服务。

官网:https://spring.io/projects/spring-cloud-stream

image-20240601120416055

该框架提供了一个灵活的编程模型,该模型基于已经建立和熟悉的Spring习惯用法和最佳实践,包括对持久pub/sub语义、消费者组和有状态分区的支持。

image-20240601120020593

Spring Cloud Stream的核心构建块是:

  • Destination Binders:负责提供与外部消息传递系统集成的组件。
  • Destination Bindings:外部消息系统和最终用户提供的应用程序代码(生产者/消费者)之间的桥梁。
  • Message:生产者和消费者用来与目标绑定器(以及通过外部消息系统的其他应用程序)进行通信的规范数据结构。

2、生产者

2.1 引入依赖

<dependencies>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-stream-rocketmq</artifactId>
            <version>2.2.2.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.rocketmq</groupId>
                    <artifactId>rocketmq-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.rocketmq</groupId>
                    <artifactId>rocketmq-acl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-client</artifactId>
            <version>4.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-acl</artifactId>
            <version>4.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

注意,RocketMQ官方维护的Spring-Cloud-Stream依赖中的rocketmq版本为4.4,需要排出后加入4.7.1的依。

2.2 编写配置文件

spring:
  application:
    name: my-spring-cloud-rocketmq-producer
  cloud:
    stream:
      bindings:
        output:
          destination: TopicTest
      rocketmq:
        binder:
          name-server: 192.168.159.34:9876
server:
  port: 8080

2.3 启动类打上注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;

@EnableBinding(Source.class)
@SpringBootApplication
public class MySpringCloudRocketmqProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringCloudRocketmqProducerApplication.class, args);
    }
}

其中,@EnableBinding(Source.class)指向配置文件的output参数。

2.4 编写生产者程序

import org.apache.rocketmq.common.message.MessageConst;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyProducer {

    @Resource
    private Source source;

    public void sendMessage(String msg){
        //封装消息头
        Map<String,Object> headers=new HashMap<>();
        headers.put(MessageConst.PROPERTY_TAGS,"TagA");
        MessageHeaders messageHeaders=new MessageHeaders(headers);
        //创建消息对象
        Message<String> message = MessageBuilder.createMessage(msg, messageHeaders);
        //发送消息
        source.output().send(message);
    }
}

2.5 编写单元测试发送消息

@SpringBootTest
class MySpringCloudRocketmqProducerApplicationTests {

    @Autowired
    private MyProducer producer;

    @Test
    void contextLoads() {
        producer.sendMessage("hello,spring cloud stream message");
    }

}

3、消费者

3.1 引入依赖

与生产者相同。

3.2 编写配置文件

spring:
  application:
    name: my-spring-cloud-rocketmq-consumer
  cloud:
    stream:
      bindings:
      	# input消费者
        input:
          destination: TopicTest
          group: spring-cloud-stream-consumer-group
      # 配置RocketMQ
      rocketmq:
        binder:
          name-server: 192.168.159.34:9876
server:
  port: 8081

3.3 启动类打上注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Sink;

@EnableBinding(Sink.class)
@SpringBootApplication
public class MySpringCloudRocketmqConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringCloudRocketmqConsumerApplication.class, args);
    }

}

其中@EnableBinding(Sink.class)指向配置文件的input参数。

3.4 编写消费者程序

import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

@Component
public class MyConsumer {

    @StreamListener(Sink.INPUT)
    public void processMessage(String message){
        System.out.println("收到的消息:"+message);
    }
}

先启动消费者,使用单元测试发送消息。

image-20240603111535498