[SpringBoot MQ Series Tutorial] RabbitMq first experience

[SpringBoot MQ Series Tutorial] RabbitMq first experience

mq very prominent advantage of asynchronous decoupling clipping, and now many of the projects will be used to master knowledge mq learn how to use smooth mq, it can be said to be a necessary career skill points

Next we enter the learning process rabbitmq

I. Environmental ready

Before the test, the need to install RabbitMQ, as given below in the installation guide mac + centos

1. mac installation

Installation Commands

brew install rabbitmq

## 进入安装目录
cd /usr/local/Cellar/rabbitmq/3.7.5

# 启动
brew services start rabbitmq
# 当前窗口启动
rabbitmq-server

You need to enable plug-in before you start the console

./rabbitmq-plugins enable rabbitmq_management

Into the console: http: // localhost: 15672 /

User name and password: guest, guest

2. centos installation

Installation Commands

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

Plug-open

rabbitmq-plugins enable rabbitmq_management
# 启动
rabbitmq-server -detached

3. Configuration

Add accounts, set permissions

## 添加账号
./rabbitmqctl add_user admin admin
## 添加访问权限
./rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
## 设置超级权限
./rabbitmqctl set_user_tags admin administrator

4. Environmental Project

We took SpringBoot create a project, for the simple experience of publishing and consuming messages rabbitmq

  • springboot version2.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.ymlconfiguration file, add the relevant attributes of rabbitmq

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

II. Examples of presentation

Next we see a hello worldversion of rabbitmq use posture, a simple news release, consumer news

1. Publish news

News release, we mainly help AmqpTemplateto achieve

@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 that amqpTemplate#convertAndSend, the first parameter is exchangeName, 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. Consumer news

Consumer information, need to specify the Queue, by routingKey binding exchange, 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 for receiving parameters, send messages to mq, and ConsumerDemoconsumption

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

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

II. Other

Project Scope

1. A gray Blog

Believe everything the book is not as good, above, is purely one of the words, due to limited personal capacity, it is inevitable omissions and mistakes, such as find a bug or have better suggestions are welcome criticism and generous gratitude

Here a gray personal blog, recording all study and work in the blog, welcome to go around

A gray blog

Published 206 original articles · won praise 57 · views 160 000 +

Guess you like

Origin blog.csdn.net/liuyueyi25/article/details/104254354