RabbitMq combat: spring boot RabbitMq combined example

I. Introduction

Have you ever encountered the need to synchronize certain data between two (multiple) systems through timing tasks? Are you distressed and struggling with the issue of mutual calls and communication between different processes in a heterogeneous system? If so, congratulations, the messaging service allows you to easily solve these problems.
Message service is good at solving the problem of data exchange (message notification/communication) between multiple systems and heterogeneous systems, and you can also use it for inter-system service calls (RPC). RabbitMQ that this article will introduce is one of the most mainstream messaging middleware.

2. Introduction to RabbitMq:

AMQP, or Advanced Message Queuing Protocol, is an open standard for application layer protocols, designed for message-oriented middleware. Message middleware is mainly used for decoupling between components, the sender of the message does not need to know the existence of the message user, and vice versa.
The main characteristics of AMQP are message-oriented, queue, routing (including point-to-point and publish/subscribe), reliability, and security.
RabbitMQ is an open source AMQP implementation. The server is written in Erlang language and supports a variety of clients, such as: Python, Ruby, .NET, Java, JMS, C, PHP, ActionScript, XMPP, STOMP, etc., and supports AJAX. It is used to store and forward messages in a distributed system, and performs well in terms of ease of use, scalability, and high availability.
The following will introduce the steps of Spring boot combined with RabbitMq:

Three. Springboot integrates RabbitMq

1. Import the pom.xml file into the jar package:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>

2. Add application.yml to the configuration of RabbitMq:

spring:
  rabbitmq:
    host : 127.0.0.1
    port : 5672
    username: guest
    password: guest

3. Create a RabbitMq message queue:

package com.plugs.RabbitMq;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    
    

	@Bean
    public Queue helloQueue() {
    
    
        return new Queue("topic_name");
    }
}

4. Create RabbitMq message queue receiver:

package com.plugs.RabbitMq;

import com.alibaba.fastjson.JSONObject;
import com.enjoypark.model.DemoOrder;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "topic_name")
public class ReceiverMQ {
    
    

	@RabbitHandler
    public void process(String reciverStr) {
    
    
        //对接受的url发送消息
        try {
    
    
            System.out.println(reciverStr);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

5. Create a message queue sender:

package com.plugs.RabbitMq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SenderMQ {
    
    
	@Autowired
    private AmqpTemplate rabbitTemplate;

    public void send(String context) {
    
    
        System.out.println("Sender : " + context);
        this.rabbitTemplate.convertAndSend("topic_name", context);
    }
}

4. Get the source code:

  1. Get the complete source code address: https://download.csdn.net/download/penggerhe/11670196
  2. Follow the official account and receive it for free:
    Insert picture description here

5. Comparison of mainstream message queues:

https://blog.csdn.net/penggerhe/article/details/108404243

Guess you like

Origin blog.csdn.net/penggerhe/article/details/108404141