Springboot | 12th chapter: Integration and use of RABBITMQ

Source: Internet
Author: User
Tags rabbitmq

Objective

In the last section, the use of the cache database redis , in the actual work, generally in the system or application communication or asynchronous notification (after login to send text messages or mail, etc.), will use Message Queuing to solve this business scenario decoupling problem. This section explains the RabbitMQ integration and simple use examples of Message Queuing.

    • RABBITMQ Introduction
    • Springboot Integrated RABBITMQ
    • Summarize
    • At last
    • Cliché
RABBITMQ Introduction

RABBITMQ is an open-source AMQP implementation that is written in Erlang and supported by a variety of clients such as Python, Ruby,. NET, Java, JMS, C, PHP, ActionScript, XMPP, stomp, etc., and support Ajax. It is used to store and forward messages in distributed system, which is very good in ease of use, extensibility, high availability and so on.

About AMQP (excerpt from the Internet):

AMQP, 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 consumer, and vice versa. The main features of AMQP are message-oriented, queue, routing (including point-to-point and publish/subscribe), reliability, and security.

Aside: Redis There are also queue functions available. But I think it redis 's still dedicated to caching.

Springboot Integrated RABBITMQ

0. The usual, the addition of Pom dependency, this is already a Springboot routine.

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

1.application.properties configuration joins RABBITMQ related configuration.

RabbitAutoConfigurationClass is its auto-load configuration class.

# rabbitmq相关配置spring.rabbitmq.host=127.0.0.1spring.rabbitmq.port=5672spring.rabbitmq.username=guestspring.rabbitmq.password=guest

2. About the configuration, the above two steps are finished ( Springboot really too convenient, writing code is pleasant). When normal use, if as a consumer, we will configure a receive queue, here for example, directly with the minimum configuration to demonstrate.

Configure a okong queue that is named

@Configurationpublic class RabbitConfig {        /**     * 定义一个名为:oKong 的队列     * @return     */    @Bean    public Queue okongQueue() {        return new Queue("okong");    }}

Configure the queue processing class, where the queue is the name of the queue configured above. :
Consumer.java

@Component//@RabbitListener 监听 okong 队列@RabbitListener(queues = "okong")@Slf4jpublic class Consumer {    /**     * @RabbitHandler 指定消息的处理方法     * @param message     */    @RabbitHandler    public void process(String message) {        log.info("接收的消息为: {}", message);    }}

Write the message sending class, which is directly written in the API method, convenient debugging.
DemoController.java

/** * 简单示例 发送和接收队列消息 * @author oKong * */@RestControllerpublic class DemoController {        //AmqpTemplate接口定义了发送和接收消息的基本操作,目前spring官方也只集成了Rabbitmq一个消息队列。。    @Autowired    AmqpTemplate rabbitmqTemplate;        @GetMapping("/send")    public String send(String msg) {        //发送消息        rabbitmqTemplate.convertAndSend("okong", msg);        return "消息:" + msg + ",已发送";    }}

3. Start the application, the normal configuration is successful, in Rabbitmq the console, you can see the connection object. The instructions have started normally.

4. Visit:http://127.0.0.1:8080/send?msg=hello,rabbitmq, you can see in the console that consumers have already consumed this message:

2018-07-24 22:59:00.777  INFO 11424 --- [cTaskExecutor-1] c.l.l.springboot.chapter12.Consumer      : 接收的消息为: hello,rabbitmq

The console interface, on the Queues tab page, can also view messages to the queue okong .

Summarize

This chapter is mainly about RabbitMQ integration and simple use, and for high concurrency systems, Message Queuing is a common solution. For example, the implementation of asynchronous message notification, consumer/producer mode. Because rabbitmq there is not too much understanding, detailed usage and related message queue knowledge, can search the relevant information, this is not elaborated here. Some time ago bought a book about the RabbitMQ aspects, and so read, but also hope to write a separate article about the message queue, please look forward to!

At last

At present, many big guys on the internet have a SpringBoot series of tutorials, if there is a similar, please forgive me. This article is the author in front of the computer word knocking, each step is practice. If there is something wrong in the text, also hope to put forward, thank you.

Cliché
    • Personal QQ:499452441
    • Public Number:lqdevOps

Personal blog: https://blog.lqdev.cn

Complete Example: chapter-12

Original address: http://blog.lqdev.cn/2018/07/24/springboot/chapter-twelve/

To install deployment RABBITMQ under Docker, click Docker: Install Deployment RABBITMQ.

Springboot | 12th chapter: Integration and use of RABBITMQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.