An example of using Aliyun message queue RocketMQ

Keywords: Programming Java Apache Python

See: https://gitee.com/imlichao/RocketMQ-example for code examples in this article

Apache RocketMQ document: http://rocketmq.apache.org/docs/quick-start/

Aliyun RocketMQ document: https://help.aliyun.com/product/29530.html

brief introduction

Message Queue RocketMQ is a professional message middleware developed by Alibaba Group. Based on high availability distributed cluster technology, it provides a series of message cloud services, such as message subscription and publication, message trajectory query, timing (delay) message, resource statistics, monitoring and alarm, and so on. It is the core product of enterprise-level Internet architecture. Message queue RocketMQ has a history of more than 9 years. It provides the ability of asynchronous decoupling, peak-shaving and valley-filling for distributed application systems. It also has the characteristics of mass message accumulation, high throughput, reliable retry and other Internet applications. It is the core product used by Alibaba Shuang11.

Message queue RocketMQ is a formal commercial product of Aliyun. At present, high availability message cloud service is provided in many regions of Aliyun. Multi-room deployment is adopted in a single domain, and its usability is very high. Even if the whole computer room is not available, it can still provide message publishing service for applications. The product stability and usability are implemented in accordance with Alibaba internal standards. Single point.

Message queue RocketMQ currently provides TCP and HTTP protocol-level access, supports Java, C++,. NET, Go, Python, Nodejs, PHP seven programming languages, and facilitates rapid access to message queue RocketMQ message cloud services developed by different programming languages. Users can deploy applications in Aliyun ECS, enterprise self-built cloud, or embedded in mobile devices and Internet of Things devices to establish a connection with the message queue RocketMQ for message sending and receiving. At the same time, local developers can access the message queue RocketMQ service through the public network for message sending and receiving.

 

Message Receiving and Sending Model

Message queue RocketMQ supports the "Publish/Subscribe" model, in which a message publisher (producer) can subscribe to a Topic on a message sending server and multiple message receivers (consumers) can subscribe to the Topic to receive the message, as shown in the following figure:

 

Example

This example uses Aliyun RocketMQ products, where user name, password address, etc. are expressed by "XXXXXX".

Increase maven dependency

<!-- increase RocketMQ rely on -->
<dependency>
  <groupId>com.aliyun.openservices</groupId>
  <artifactId>ons-client</artifactId>
  <version>1.8.0.Final</version>
</dependency>

Configuration class

This example uses two consumers in the same group to consume together, and the message uses exactly-once semantics to ensure that each message is consumed only once.

package pub.imlichao.config;

import com.aliyun.openservices.ons.api.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;


/**
 * RocketMQ To configure
 */
@Configuration
public class RocketMQConfig {
	/**
	 * Producer allocation
	 * @return
	 */
	@Bean
	public Producer  producer () {
		Properties properties = new Properties();
		// Authentication with AccessKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.AccessKey,"XXXXXX");
		// Authentication with SecretKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.SecretKey, "XXXXXX");
		// Set up the TCP access domain name, enter the instance management page of the console, select the instance at the top of the page, and view the "Access Point Information" area in the instance information.
		properties.put(PropertyKeyConst.NAMESRV_ADDR,"XXXXXX");
		//Open exactly-once delivery semantics through PropertyKeyConst.EXACTLYONCE_DELIVERY (guaranteeing that messages are consumed only once with multiple consumers)
		properties.put(PropertyKeyConst.EXACTLYONCE_DELIVERY, "true");
		Producer producer = ONSFactory.createProducer(properties);
		// Before sending a message, you must call the start method to start the Producer, just once.
		producer.start();

		return producer;
	}

	/**
	 * Consumer 1 Configuration
	 * @return
	 */
	@Bean
	public Consumer consumer1 () {
		Properties properties = new Properties();
		// Group ID you created in the console
		properties.put(PropertyKeyConst.GROUP_ID, "GID_pmall_consumer");
		// Authentication with AccessKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.AccessKey,"XXXXXX");
		// Authentication with SecretKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.SecretKey, "XXXXXX");
		// Set up the TCP access domain name, enter the instance management page of the console, select the instance at the top of the page, and view the "Access Point Information" area in the instance information.
		properties.put(PropertyKeyConst.NAMESRV_ADDR,"XXXXXX");
		Consumer consumer = ONSFactory.createConsumer(properties);
		//Create message listening and message processing logic
		consumer.subscribe("pmall_message", "data_storage", new MessageListener() {
			@Override
			public Action consume(Message message, ConsumeContext context) {
				System.out.println("Receive1: " +  new String (message.getBody()) + " " + message.getMsgID());
				return Action.CommitMessage;
			}
		});
		//lsnrctl start
		consumer.start();

		return consumer;
	}

	/**
	 * Consumer 2 Configuration
	 * @return
	 */
	@Bean
	public Consumer consumer2 () {
		Properties properties = new Properties();
		// Group ID you created in the console
		properties.put(PropertyKeyConst.GROUP_ID, "GID_pmall_consumer");
		// Authentication with AccessKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.AccessKey,"XXXXXX");
		// Authentication with SecretKey, Created in Aliyun Server Management Console
		properties.put(PropertyKeyConst.SecretKey, "XXXXXX");
		// Set up the TCP access domain name, enter the instance management page of the console, select the instance at the top of the page, and view the "Access Point Information" area in the instance information.
		properties.put(PropertyKeyConst.NAMESRV_ADDR,"XXXXXX");
		Consumer consumer = ONSFactory.createConsumer(properties);
		//Create message listening and message processing logic
		consumer.subscribe("pmall_message", "data_storage", new MessageListener() {
			@Override
			public Action consume(Message message, ConsumeContext context) {
				System.out.println("Receive2: " + new String (message.getBody()) + " " + message.getMsgID());
				return Action.CommitMessage;
			}
		});
		//lsnrctl start
		consumer.start();

		return consumer;
	}
}

send message

package pub.imlichao;

import com.aliyun.openservices.ons.api.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import pub.imlichao.config.RocketMQConfig;

import javax.annotation.Resource;
import java.util.Date;

@Controller
public class ProducerTest {
    @Resource
    private RocketMQConfig rocketMQConfig;

    /**
     * Send a message to RocketMQ
     * @return
     */
    @GetMapping(value = "/send")
    public String send(){
        //Send messages 100 times in a loop
        for (int i =0;i<100;i++){
            //Create message
            Message msg = new Message(
                    // Topic created in the console, which is the Topic name to which the message belongs
                    "pmall_message",
                    // Message Tag. It can be understood as a tag in Gmail, which can reclassify messages to facilitate Consumer to specify filtering conditions in the message queue RocketMQ server.
                    "data_storage",
                    // Message Body. The message queue RocketMQ does not interfere with any binary data. Need a serialization and deserialization approach that Producer agrees with Constumer
                    ("pmall MQ "+ new Date()).getBytes());

            // Set the key business attributes that represent the message, as globally and uniquely as possible, so that you can query the message through the console and replenish it if you can't receive the message normally. Note: Not setting will not affect the normal sending and receiving of messages.
            msg.setKey("data_id");
            // Sending a message without throwing an exception is successful
            SendResult sendResult = rocketMQConfig.producer().send(msg);
            //Print Message ID for message sending status queries
            System.out.println("Send Message success. Message ID is: " + sendResult.getMessageId());
        }
        return "redirect:/";
    }
}

Posted by hoodlumpr on Thu, 09 May 2019 07:27:39 -0700