Springboot integrates RocketMQ actual combat to send messages

Send messages in action

Note: Remember to start the nameser and broker

  • Quickly create a springboot project  https://start.spring.io/

  • Add related dependencies. Remember that the version here must be consistent with the server version, otherwise the topic will not be automatically created after startup! Remember!

    <dependency>
    		<groupId>org.apache.rocketmq</groupId>
    		<artifactId>rocketmq-client</artifactId>
    		<version>4.3.0</version>
    </dependency>
    
  • Message object

    • topic: topic name
    • tag: tag, used to filter
    • key: The unique identifier of the message, which can be a combination of business fields
    • body: message body, byte array
  • Note that when sending a message to the broker, you need to determine whether there is a topic to start the broker,

    It is recommended to turn on automatic topic creation in the local environment, and turn off automatic topic creation in the production environment.

    It is recommended to create a topic manually first, if it is automatically created by the program, and then the message is delivered, there will be a delay

  • Conceptual model: A topic corresponds to multiple queues, which can be specified when creating a topic, such as order topic

  • View messages through the visual management background

Here we write the message provider!

@Component
public class PayProducer {

    private String producerGroup = "pay_group";

    private String nameServerAddr = "39.96.192.171:9876"; //多节点逗号分隔

    private DefaultMQProducer producer;

    public PayProducer(){
       producer =  new DefaultMQProducer(producerGroup);

       producer.setNamesrvAddr(nameServerAddr);
       start();
    }

    public DefaultMQProducer getProducer(){
        return this.producer;
    }

    /**
     * 对象在用之前必须要调用一次,只能初始化一次
     */
    public void start(){
    try {
        this.producer.start();
    } catch (MQClientException e) {
        e.printStackTrace();
    }
}

    /**
     * 一般在应用上下文,使用上下文监听器,进行关闭
     */
    public void shutdown(){
        this.producer.shutdown();
}

}
@RestController
public class PayController {

    @Autowired
    private PayProducer payProducer;

    private static final String topic = "pay_topic";

    @RequestMapping("/api/v1/pay_cb")
    public Object callback(String text) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {

        Message message = new Message(topic, "taga", ("hello rocketmq!" + text).getBytes());

        SendResult sendResult = payProducer.getProducer().send(message);

        System.out.println(sendResult);
        return null;
    }
}

Start the service and access the path. The following 500 error should be reported. Look at the console:

Reason: Broker prohibits the automatic creation of Topic, and the user does not manually create the Topic, or the broker and the nameserver network cannot communicate. Solution:

by

sh bin/mqbroker -m View configuration autoCreateTopicEnable=true, then topic will be created automatically

Centos7 close the firewall systemctl stop firewalld

Then we revisit, and it will report a timeout error

Reason: Alibaba Cloud has multiple network cards. Rocketmq will select an IP based on the current network card. When your machine has multiple network cards, there may be problems. For example, the problem I encountered is that there are two IPs on my machine, one public IP and one private IP, so I need to configure broker.conf to specify the current public IP, and then restart the broker. New configuration: conf/broker .conf (Property name brokerIP1=The public network ip address where the broker is located) Add this configuration: brokerIP1=120.76.62.13 Start command: nohup sh bin/mqbroker -n localhost:9876 -c ./conf/broker.conf &

The console cannot view the data, and it prompts that the connection 10909 error reason: Rocket has enabled the VIP channel by default, and the VIP channel port is 10911-2=10909

Solution: Alibaba Cloud Security Group needs to add a port 10909

Visit again! The idea console sends a message successfully!

We check the rocketmq console

You can view the message we sent! Next we create consumers to consume messages!

 

 

Guess you like

Origin blog.csdn.net/qq_41023026/article/details/89741534
Recommended