What is the RabbitMQ

RabbitMQ is an open source message broker (message-oriented middleware) that implements AMQP(Advanced Message Queue Protocol).

Rabbitmq core concepts

publisher

The producer (publisher) of the message, and the program that sends the message is the producer.

message

The message is made up of a header and a body, the header is a routing key and a few other things, and the body is a message that we set up to send.

exchange

A switch that receives messages sent by producers and routes them to a specified message queue.

binding

Binding for association between message queues and switches.

queue

Message queue, used to hold the queue of messages sent by producers, is simply a container from which consumers can receive messages.

consumer

Consumers of information are consumers who receive information.

The rabbitMQ structure is as follows:

rabbitmq_jiegoutu.png

Why rabbitMQ

Support a variety of messaging protocols, message queuing, delivery confirmation, flexible routing to queues, a variety of exchange types; Deploy as a cluster for high availability and throughput; Federation across multiple available regions and regions; Plugins, easy to expand; Cross-language, can be developed using any common programming language…

Are copied on the website, is too abstract, here, for example, before the rabbitmq can be understood as the post office, send mail before everyone is letters directly to tell the post office and the addressee address, no need to worry how specific to the recipient, can know, each person can be either a sender, also can be the recipient, in the same way, An application can be both a producer and a consumer. We can’t go to the post the letter directly to each other’s hand, that still use to post the letter, you may feel now who will send a letter, sent WeChat messages directly, also, we send WeChat messages is not directly to each other on the phone, and sent to the first WeChat server there, and then WeChat server there will be information to the user you want to send, We don’t have to know how to do it in the middle, maybe the example is not so appropriate, but it’s good to understand.

Comparison of not using and after using:

rabbitmq_duibi.png

The rabbitmq springboot integration

Installation is no longer redundant, you can look at baidu.

  1. Add the dependent

    <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-amqp</artifactId>

    </dependency>

    Copy the code
  2. Configure the rabbitmq

    spring:

     rabbitmq:

       addresses: 127. 0. 01.

       port: 5672

       usernameguest

       passwordguest

    Copy the code
  3. Environment to prepare

    For the sake of testing, I’ll add a few switches, queues and binding rules to the RabbitMQ UI.

  • Switch exchange

    rabbitmq_exchanges.png
  • Queue queue

    rabbitmq_queues.png
  • Binding rules

    rabbitmq_demo_direct_binding.png
    rabbitmq_demo_fanout_binding.png
    rabbitmq_demo_topic_binding.png
  1. code

    package com.lytw13.demo.controller;



    import org.springframework.amqp.rabbit.core.RabbitTemplate;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.web.bind.annotation.GetMapping;

    import org.springframework.web.bind.annotation.RequestMapping;

    import org.springframework.web.bind.annotation.RestController;



    @RestController

    @RequestMapping("user")

    public class PublicController {

       @Autowired

       RabbitTemplate rabbitTemplate;

       @GetMapping("test1")

       public String test1() {

           rabbitTemplate.convertAndSend("demo.fanout".""."welcome to regist lytw13'blog!");

           return "success";

       }

       @GetMapping("test2")

       public String test2() {

           rabbitTemplate.convertAndSend("demo.direct"."lytw13"."welcome to regist lytw13'blog!");

           return "success";

       }

       @GetMapping("test3")

       public String test3() {

           rabbitTemplate.convertAndSend("demo.topic"."lytw13.hello"."welcome to regist lytw13'blog!");

           return "success";

       }

    }

    Copy the code

Testing:

  • Access localhost: 8080 / user/test01, you can see three queue can receive messages, fanout is whether rounting key, send the information to all queues.
  • Access localhost: 8080 / user/test02, can see only one queue can receive messages, direct is rounting key must fully meet will send the information to the corresponding queue.
  • Access localhost: 8080 / user/test03, can see there are 2 can get message queue, topic is rounting key must match the set of rules, * represents a match a word, # any, in line with the will send the information to the corresponding queue.

I have uploaded the specific code to Github. If you have any questions, please refer to it.To travel to