Spring Boot 2.X - Spring Boot整合AMQP之RabbitMQ

Article Directory
Spring Boot 2.X - Spring Boot AMQP integration of RabbitMQ
RabbitMQ Introduction
introduction rely
write configuration
write an interface
to enable annotation Rabbit
message listener
Messaging Test
Spring Boot 2.X - Spring Boot integrate RabbitMQ AMQP of
Spring Boot 2 integrating RabbitMQ case.

RabbitMQ Introduction
Introduction
RabbitMQ is an erlang developed by the AMQP (Advanved Message Queue Protocol) is an open source implementation.
The core concept
Message
message, the message is anonymous, which consists of a message header and message body. Message body is opaque, and the message header by a series of optional attribute set
to these properties include the routing-key (routing keys), priority (priority relative to other message), delivery-mode (noted that
the message may require persistent storage) and so on.
Publisher
producer of the message, also issued a message to the switch of the client application.
Exchange
switch, for receiving a message sent by the producer and to route the messages to the server queues.
Exchange There are four types: direct (default), fanout, topic, strategy and headers, different types of Exchange forwarded messages have
the distinction.
Queue
message queue, to save the message until the transmission to consumers. It is a container for the message, the message is the end. A message
can be put into one or more queues. Message has been in the queue inside, waiting for consumers to connect to the queue will remove it.
Binding
Binding an association between a message queue and switches. Binding is a key-based switch and routing message queues connected
routing rules pick up, it may be appreciated that the switch into a routing table constituted by the binding.
Exchange Queue and binding can be many to many relationship.
Connection
Network connection, such as a TCP connection.
Channel
one independent flow channel bidirectional data connection channel multiplexer. A virtual channel is established in the real TCP connections
intended to connect, AMQP commands are sent out through the channel, whether it is announced, subscribe or receive a message queue, which
these actions are done through the channel. Since the establishment of TCP and destruction are very expensive overhead for the operating system,
to introduce the concept of the channel, in order to reuse a TCP connection.
Consumer
consumer message indicating a client application to obtain information from the message queue.
Virtual Host
Virtual Host, represents a group of switches, message queues, and related objects. Web hosting is to share the same authentication and processing
standalone server domain dense environment. Is essentially a mini version of RabbitMQ server, with each vhost
own queue, switches, binding and permission mechanism. AMQP is the basis of the concept of vhost must be specified at the time of connection,
RabbitMQ is the default vhost /.
Broker
represents the message queue server entity


The introduction of dependent
use Spring Initializr quickly create a Spring Boot project spring-boot-v2-amqp, mainly depends on the following:

<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>

编写配置
添加RabbitMQ的配置信息。RabbbitMQ安装请参阅 Docker安装RabbitMQ

the Spring:
RabbitMQ:
Host: 192.168.0.2 # RabbitMQ Host IP
Port: 5672 # 5672 default, do not write the same
username: guest # default guest, do not write the same
password: guest # default guest, do not write the same
# virtual-host: / # The default is "/" not necessary to be written, public void setVirtualHost (String virtualHost) {this.virtualHost = "" .equals (virtualHost)? "/": virtualHost;}


Interface write
a new AmqpController, for transmitting a message

@RestController
public class AmqpController {

private final RabbitTemplate rabbitTemplate;

public AmqpController(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}

private final static String SUCCESS = "success";

/**
* 单点
* @param msg
* @return
*/
@GetMapping("/direct")
public String direct(String msg){
rabbitTemplate.convertAndSend("amq.direct", "xudc", msg);
return SUCCESS;
}

@GetMapping("/fanout")
public String fanout(String msg) {
rabbitTemplate.convertAndSend("amq.fanout", "", msg);
return SUCCESS;
}

@GetMapping ( "/ Topic")
public String Topic (String MSG) {
rabbitTemplate.convertAndSend ( "amq.topic", "xudc #.", MSG);
return SUCCESS;
}
}

enable annotations Rabbit
plus the master boot class Notes @EnableRabbit

@SpringBootApplication
@EnableRabbit // RabbitMQ open annotation-based model
public class SpringBootAmqpApplication {

static void main public (String [] args) {
SpringApplication.run (SpringBootAmqpApplication.class, args);
}
}

Message Listener
write message listener

@Component
public class AmqpListener {

@RabbitListener(queues = "xudc")
public void receive1(String message) {
System.err.println("xudc -- receive1接收到消息:" + message);
}

@RabbitListener(queues = "xudc.book")
public void receive2(String message) {
System.err.println("xudc.book -- receive2接收到消息:" + message);
}

@RabbitListener (Queues = "Andy")
public void receive3 (String Message) {
System.err.println ( "Andy - receive3 received message:" Message +);
}
}

Messaging Test
Direct
fanout
Topic
Thus, to achieve the Spring Boot and RabbitMQ simple integration.
----------------
Disclaimer: This article is the original article CSDN bloggers "xudc", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. .
Original link: https: //blog.csdn.net/xudc0521/article/details/89362348

Guess you like

Origin www.cnblogs.com/weizhxa/p/12100192.html