[SpringBoot MQ Series tutorial] RabbitMq initial experience

[SpringBoot MQ Series tutorial] RabbitMq initial experience

The advantage of mq in asynchronous decoupling peak cutting is very prominent. Now many projects will use it. Mastering the knowledge of mq and how to use it smoothly can be said to be a necessary professional skill point

Next, we enter the learning process of rabbitmq

<!-- more -->

1. Environmental preparation

Before the test, rabbitmq needs to be installed. The following are the installation tutorials of mac + centos

1. mac installation

Installation command

brew install rabbitmq

## Enter the installation directory
cd /usr/local/Cellar/rabbitmq/3.7.5

# start-up
brew services start rabbitmq
# Current window launch
rabbitmq-server

Plug in needs to be opened before starting the console

./rabbitmq-plugins enable rabbitmq_management

To access the console: http://localhost:15672/

User name and password: guest,guest

2. centos installation

Installation command

yum install erlang
wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el6.noarch.rpm
yum install rabbitmq-server-3.6.15-1.el6.noarch.rpm

Plugin opening

rabbitmq-plugins enable rabbitmq_management
# start-up
rabbitmq-server -detached

3. configuration

Add account and set permissions

## Add account
./rabbitmqctl add_user admin admin
## Add access
./rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
## Set super permissions
./rabbitmqctl set_user_tags admin administrator

4. Project environment

Next, we will create a spring boot project to simply experience the release and consumption of rabbitmq

  • springboot version is 2.2.1.RELEASE
  • rabbitmq version 3.7.5

Dependent configuration file pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-amqp</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>
</build>
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <name>Spring Snapshots</name>
        <url>https://repo.spring.io/libs-snapshot-local</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release-local</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

In the application.yml configuration file, add the related properties of rabbitmq

spring:
  rabbitmq:
    virtual-host: /
    username: admin
    password: admin
    port: 5672

2. Example demonstration

Next, let's look at the usage posture of a hello world version of rabbitmq, which is a simple post and consumption message

1. Publish news

We mainly use AmqpTemplate to publish messages

@Component
public class PublishDemo {
    @Autowired
    private AmqpTemplate amqpTemplate;

    public String publish2mq(String ans) {
        String msg = "hello world = " + ans;
        System.out.println("publish: " + msg);
        amqpTemplate.convertAndSend(Pkg.exchange, Pkg.routing, msg);
        return msg;
    }
}

In the above case, the main method is amqptemplate ා convertandsend. The first parameter is exchangeName and the second is routingKey

Constant configuration is as follows

class Pkg {
    final static String exchange = "topic.e";
    final static String routing = "r";
    final static String queue = "topic.a";
}

2. Consumption news

To consume a message, you need to specify a Queue and bind the exchange through routingKey, as follows

@Service
public class ConsumerDemo {

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = Pkg.queue, durable = "false", autoDelete = "true"),
            exchange = @Exchange(value = Pkg.exchange, ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC), key = Pkg.routing))
    public void consumer(String msg) {
        System.out.println("consumer msg: " + msg);
    }
}

3. Test demo

Write a simple rest interface to receive parameters, publish messages to mq, and be consumed by ConsumerDemo

@RestController
public class PubRest {
    @Autowired
    private PublishDemo publishDemo;

    @GetMapping(path = {"", "/", "/publish"})
    public String publish(String name) {
        return publishDemo.publish2mq(name);
    }
}

II. other

0. project

1. A grey Blog

The best letter is not as good as the above. It's just a one-of-a-kind remark. Due to the limited personal ability, there are inevitably omissions and mistakes. If you find a bug or have better suggestions, you are welcome to criticize and correct. Thank you very much

Here is a grey personal blog, recording all the blogs in study and work. Welcome to visit

Tags: Programming Spring RabbitMQ SpringBoot Mac

Posted on Mon, 10 Feb 2020 05:00:51 -0800 by MaxD