spring boot integrated rocketmq

Integrate Spring Boot and RocketMQ

In modern microservice architecture, message queue has become a common asynchronous processing mode, which can solve problems such as synchronous calls between services, high coupling, and traffic peaks. RocketMQ is Alibaba's open source messaging middleware with excellent performance and complete functions. It is widely used in various business scenarios.

This article will introduce in detail how to integrate RocketMQ in the Spring Boot project to realize the production and consumption of messages.

development environment

  • JDK 1.8 or higher
  • RocketMQ 4.8.0 or higher
  • Spring Boot 2.3.1.RELEASE or higher
  • Maven 3.0 or higher

RocketMQ server deployment

First, we need to deploy RocketMQ locally or on the server. For specific deployment steps, please refer to RocketMQ official documentation. To simplify deployment, we can use Docker for deployment.

Spring Boot project creation

We use Spring Initializr to create a new Spring Boot project and select Web, Lombok and RocketMQ Spring Boot Starter as project dependencies.

pom.xmlExample:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.rocketmq</groupId>
        <artifactId>rocketmq-spring-boot-starter</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
</dependencies>

Configure RocketMQ

application.propertiesConfigure RocketMQ's server address and other related parameters in the file .

rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=my-group

Here, rocketmq.name-serverit is the address of the RocketMQ server and rocketmq.producer.groupthe group name of the producer.

message producer

Next, we create a message producer. In the Spring Boot project, we can use RocketMQTemplateto send messages.

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {
    
    

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/send")
    public String send(String message) {
    
    
        rocketMQTemplate.convertAndSend("test-topic", message);
        return "Message: '" + message + "' sent.";
    }
}

In the above code, we have created a RESTful interface /sendthat will send a message to the topic when the interface is called test-topic.

message consumer

Next, we create a message consumer. In a Spring Boot project, we can use @RocketMQMessageListenerannotations to define a message consumer.

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

@Service
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "my-consumer_test-topic")
public class ConsumerService implements RocketMQListener<String> {
    
    

    @Override
    public void onMessage(String message) {
    
    
        System.out.printf("------- StringConsumer received: %s \n", message);
    }
}

In the above code, we define a message consumer, which will listen for test-topicmessages from the topic. When there is a new message, it will print the message content.

test

At this point, we have completed all the code for Spring Boot integration with RocketMQ. Next, we can run the Spring Boot project, /sendsend messages through the access interface, and check the console output to verify whether the message consumer can receive the message normally.

This is the whole process of Spring Boot integrating RocketMQ. As a powerful message middleware, RocketMQ not only supports basic message production and consumption, but also supports many advanced features, such as transaction messages, sequential messages, delayed messages, etc. In actual project development, we can choose the appropriate message model according to business needs to improve the availability and reliability of the system.

Transaction messages

RocketMQ supports sending transaction messages, that is to say, while sending messages, we can perform local database operations. Only when the local database operations are successful, the messages will actually be sent.

Here is an example of sending a transaction message:

import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

@RestController
public class TransactionProducerController {
    
    

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/sendTransaction")
    public String sendTransaction(String message) {
    
    
        ExecutorService executor = new ThreadPoolExecutor(5, 10, 200, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5000), r -> {
    
    
            Thread thread = new Thread(r);
            thread.setName("client-transaction-msg-check-thread");
            return thread;
        });

        TransactionListener transactionListener = new TransactionListenerImpl();
        TransactionMQProducer producer = rocketMQTemplate.createAndStartTransactionMQProducer("transaction-group",transactionListener,executor);

        producer.sendMessageInTransaction("test-topic", "TagA", message, null);

        return "Transaction Message: '" + message + "' sent.";
    }
}

In the above code, we create one TransactionMQProducerand set it up TransactionListenerto handle transaction commits and rollbacks. When sending a transaction message we need to call sendMessageInTransactionthe method.

sequential message

RocketMQ supports sending sequential messages, that is, messages will be consumed in the order they are sent.

Here is an example of sending sequential messages:

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.messaging.support.MessageBuilder;

@RestController
public class OrderlyProducerController {
    
    

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/sendOrderly")
    public String sendOrderly(String message) {
    
    
        for (int i = 0; i < 100; i++) {
    
    
            rocketMQTemplate.syncSendOrderly("orderly_topic", MessageBuilder.withPayload(message + i).build(), "hashkey");
        }
        return "Orderly Message: '" + message + "' sent.";
    }
}

In the above code, we call syncSendOrderlythe method to send sequential messages. The third parameter of this method is hashkey. RocketMQ will decide which queue to send the message to based on this key. Messages with the same hashkey will be sent to the same queue.

delayed message

RocketMQ supports sending delayed messages, that is, the messages will not be consumed immediately, but will be consumed after a specified time.

Here is an example of sending a delayed message:

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.messaging.support.MessageBuilder;

@RestController
public class DelayProducerController {
    
    

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/sendDelay")
    public String sendDelay(String message) {
    
    
        rocketMQTemplate.syncSend("delay_topic", MessageBuilder.withPayload(message).build(), 1000, 4);
        return "Delay Message: '" + message + "' sent.";
    }
}

In the above code, we call syncSendthe method to send the delayed message. The third parameter of this method is the delay time, and the fourth parameter is the delay level.

The above are the detailed steps and sample code for integrating Spring Boot with RocketMQ. I hope it will be helpful to everyone.

Guess you like

Origin blog.csdn.net/orton777/article/details/132067590
Recommended