[Examples] Spring Boot combat and advanced integration of RabbitMQ Detailed

Spring Boot is a very good framework, it appears simplifies initial set up and development of new Spring applications, greatly reducing the amount of code, has been recognized and used by most businesses. This column will be Spring Boot frame from simple to complex processes, from combat to advanced, we not only want to know how to use, but also to analyze source code framework, learning its excellent design.

Summary Catalog link: [Spring Boot combat and advanced learning catalog]

I. Introduction

   RabbitMQ is the use of Erlang language AMQP messaging middleware protocol, AMQP stands for Advanced Message Queue Protocolo, Advanced Message Queuing Protocol. It is an open standard application-layer protocol for message-oriented middleware design, based on this protocol client and messaging middleware message can be transmitted, is not subject to the limitations of product development language.
Here Insert Picture Description
There are three common switches:

1, Direct Exchange (Direct Switch)

The key message carrying the routing (routing key) corresponding to the message delivered to the queue, direct exchange of messages suitable for unicast transmission.
Workflow is as follows:

  • Will bind to a queue on a switch, while giving the binding a route key.
  • When R is a carrying route key message is sent to the direct exchange, exchange routes the message to the binding value of R is the same queue. Note Route Key and bind values to fully match the job .
    direct exchange often used in a plurality of tasks assigned worker, when doing so, note that, in the AMQP 0-9-1, load balancing occurs between Consumer message, rather than between queue.
    Here Insert Picture Description

2, Fanout Exchange (fan-shaped switch)
  a fanout exchange messages will be distributed to all bound to the exchange of the queue, the queue regardless of the route key. If there are N Queue is bound to a fanout exchange, so at this time when the exchange receives the message, this message will be distributed to the N queue, because of this nature, fanout exchange also commonly broadcast messages.
Here Insert Picture Description

3, Topic Exchange (theme switch)

  Topic The route key exchange message will be distributed to this message with the route key match and bind this exchange of one or more queue. Here matches the direct exchange of routing rules are not the same exact match, Topic exchange is extended in the matching rules in the following:

  • RoutingKey (routing keys) as a dot "." Separated strings, such as "com.rabbitmq.client", "java.util.concurrent", "com.hidden.client" etc.
  • BindingKey (key bindings) and also as RoutingKey dot "." Separated string
  • BindingKey (binding key) may be present in two special string "*" and "#", used to make fuzzy matching, where "#" is used to match a word, "*" for matching a plurality of word

  for implementing the topic exchange often publish / subscribe model, i.e. the model multicast message. Here Topic Exchange will apply to the publish / subscribe model. RabbitMQ is a principle, the message can not be delivered directly to the Queue must be delivered to the Exchange in the first message, and then by the Exchange in accordance with the routing message delivery rules corresponding to the Queue.
Here Insert Picture Description

Second, the integration of RabbitMQ

1, rely on the introduction of

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

2, the connection configuration information RabbitMQ

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

3, the configuration classes and create a message queue RabbitMQ

@Configuration
public class RabbitConfig {

    @Bean
    public Queue getQueue() {
        return new Queue("QA");
    }
}

4, create producer

@Component
public class MsgProducer {

    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        rabbitTemplate.convertAndSend("QA", "这是一条最新消息");
    }
}

5, create a consumer

@Component
@RabbitListener(queues = "QA")
public class MsgConsumer1 {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("【消费者接收的消息】: " + msg);
    }

}

6, create a test class

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitmqApplicationTests {

    @Autowired
    private MsgProducer msgProducer;

    // 发送单条消息
    @org.junit.Test
    public void contextLoads() {
        msgProducer.send();
    }
}

7, run the project

Console output:

【消费者接收的消息】: 这是一条最新消息

RabbitMQ Admin side:
Here Insert Picture Description

He published 198 original articles · won praise 1174 · Views 1.34 million +

Guess you like

Origin blog.csdn.net/sinat_27933301/article/details/105030216