java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Cloud Stream整合RocketMQ

Spring Cloud Stream整合RocketMQ的搭建方法

作者:别团等shy哥发育

本文介绍了如何使用SpringCloudStream整合RocketMQ进行消息传递,SpringCloudStream是一个用于构建与共享消息系统连接的框架,支持持久pub/sub语义和消费者组,感兴趣的朋友跟随小编一起看看吧

Spring Cloud Stream整合RocketMQ

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

1、Spring Cloud Stream介绍

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

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

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

Spring Cloud Stream的核心构建块是:

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);
    }
}

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

到此这篇关于Spring Cloud Stream整合RocketMQ的文章就介绍到这了,更多相关Spring Cloud Stream整合RocketMQ内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文