Spring Boo integrates RocketMQ

1. Introduction

  • Producer : producer, used to send messages
  • Consumer : consumer, used to consume messages
  • NameServer : Service registry, used to register producers and consumers, store Broker routes and provide them to producers and consumers
  • Broker : used to store messages

insert image description here

The project structure is as follows:
First, a parent directory: SpringBoot-RocketMQ
then there are three subdirectories below:
producer: springboot-dubbo-provider
interface: springboot-dubbo-interface
consumer: springboot-dubbo-consumer
(due to the reuse of the previous dubbo project, the directory name has not been changed)
I hung the project source code on github, directly Just pull the master branch: https://github.com/shengwanping/SpringBoot-RocketMQ
insert image description here

Two, build the project

1. pom configuration

1. Add pom.xml in SpringBoot-RocketMQ:

 <!--dependencyManagement 依赖管理,子项目不会继承父依赖,需要重新声明-->
    <dependencyManagement>
        <dependencies>
            <!-- spring-boot依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.3.12.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- rocketmq-apache -->
            <dependency>
                <groupId>org.apache.rocketmq</groupId>
                <artifactId>rocketmq-spring-boot-starter</artifactId>
                <version>2.2.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2. Add pom.xml in springboot-dubbo-provider:

 <dependencies>
        <!--引入 springboot-dubbo-interface 接口服务-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springboot-dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- springboot 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- rocketmq-apache -->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

3. Add pom.xml in springboot-dubbo-consumer:

<dependencies>
        <!--引入 springboot-dubbo-interface 接口服务-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springboot-dubbo-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- springboot 依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- rocketmq-apache -->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
    </dependencies>

4. springboot-dubbo-interface is just an interface toolkit, no additional configuration is required in pom.xml

2. Yaml configuration

1. The producer application.yml configuration is as follows

server:
  port: 8010

rocketmq:
  name-server: localhost:9876 # 连接Rocketmq Name Server服务注册中心
  producer: # 生产者
    group: producer-one # 生产者组.随意取名

2. Consumer application.yml is configured as follows

server:
  port: 8011

rocketmq:
  name-server: localhost:9876 # 连接Rocketmq Name Server服务注册中心
  producer: # 消费者者
    group: consumer-one # 消费者组.随意取名

3. Implementation class

1. There is only one interface under the interface toolkit:

public interface DemoService {
    
    

    void sendHello();
}

2. There are two classes under the producer package:
Here, the startup class is used to directly call the interface

package org.dubbo.provider;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.dubbo.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 模拟 消息生产者
 */
@Service("demoService")
public class DemoServiceImpl implements DemoService {
    
    
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void sendHello() {
    
    
        // 向Proder发送消息                topic           发送的消息
        rocketMQTemplate.convertAndSend("topic_001", "Hello RocketMQ");

    }
}
package org.dubbo.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class ProviderApplication {
    
    
    public static void main(String[] args) {
    
    
        // 获取上下文
        ConfigurableApplicationContext context = SpringApplication.run(ProviderApplication.class, args);
        // 模拟Controller调用这个接口(启动后直接调用sendHello()方法,发送消息)
        DemoServiceImpl demoService = (DemoServiceImpl) context.getBean("demoService");
        demoService.sendHello();
    }
}

3. There are two categories under the consumer package:

package org.dubbo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {
    
    

    public static void main(String[] args) {
    
    
        SpringApplication.run(ConsumerApplication.class, args);
    }
}
package org.dubbo.consumer;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 消息 消费者
 */
@Component
// 指定topic 和 消费者组
@RocketMQMessageListener(topic = "topic_001", consumerGroup = "${rocketmq.producer.group}")
public class ConsumerMode implements RocketMQListener<String> {
    
     // 继承RocketMQListener接口


    @Override
    public void onMessage(String s) {
    
    
        System.out.println("收到的消息是:"+s);
    }
}

After completing the above configuration, the code of the producer and consumer is completed, and then you need to start the NameServer (routing registration center) and BrokerServer of rocketmq

3. RocketMQ

How to start NameServer and Broker, please refer to the following article:
RocketMQ download, RocketMQ visual console download

4. Start the producer and consumer

After starting the NameServer and Broker, we start the producer and the consumer. At this time, if it prints "The message received is: Hello RocketMQ" under the consumer, it means success.
insert image description here
If the RocketMQ visual management platform is installed and started, we can clearly see the messages sent by the producers on it.
insert image description here
I am also just learning RocketMQ. If there are any deficiencies, please leave a message to communicate!

Guess you like

Origin blog.csdn.net/weixin_44183847/article/details/129373714