Introduction to RabbitMQ core terms
Publisher: message producer
Exchange: Exchange, used to receive messages sent by producers and route these messages to the queue in the server according to the binding relationship between the routing key and the queue
There are 4 types of Exchange: direct (default), fanout, topic, and headers:
diract: Complete matching strategy, the routing key of the switch must be exactly the same as the routing key in the binding before sending messages to the corresponding message queue, point-to-point mode;
fanout: broadcast strategy, do not match the routing key, send the message to all the queues bound to the switch, because the routing key is not matched, the speed is the fastest
topic: Wildcard matching topic strategy, the switch assigns the routing key attribute of the message through pattern matching, and matches the routing key with a pattern. At this time, the queue needs to be bound to a pattern. It divides the string of routing key and binding key into words, separated by dots. It also recognizes two wildcard characters: the symbol "#" and the symbol "*". #Match 0 or more words, *match one word
headers: Basically not used, the header in the message is matched, not the matching routing key, the performance is very poor
Queue : message queue
Binding : Binding relationship, used to indicate the association between the binding switch and the queue. A switch can specify multiple queues or a many-to-many relationship
Routing key: match the binding relationship between the switch and the queue through the key of the routing key
Channel :
An independent bidirectional data stream channel in a multiplexed connection. A channel is a virtual connection established within a real TCP connection. AMQP commands are sent through the channel. Whether it is publishing messages, subscribing to queues or receiving messages, these actions are all completed through the channel. Because it is very expensive for the operating system to establish and destroy TCP, the concept of channel is introduced to reuse a TCP connection.
Consumer : message consumer
Virtual Host : Virtual host, which represents a batch of exchanges, message queues and related objects. A virtual host is an independent server domain that shares the same authentication and encryption environment. Each vhost is essentially a mini version of RabbitMQ server with its own queue, switch, binding, and permission mechanism. vhost is the foundation of AMQP concept and must be specified when connecting. The default vhost of RabbitMQ is /.
Broker: Represents the message queue server entity, which is a rabbitmq server
1. Simple installation using docker
Pull the image file
(If the domestic mirror is not configured, use registry.docker-cn.com/library/rabbitmq. If the domestic mirror address is configured, use rabbitmq directly)
docker pull registry.docker-cn.com/library/rabbitmq
start up
docker -d --name myrabbitmq -p 5672:5672 -p 15672:15672 registry.docker-cn.com/library/rabbitmq
Configure access to web pages
Enter the container
docker exec -it b198e5bff969 /bin/bash
The command to start the web management module is as follows (in addition, there are many commands for rabbitmq, which can be known under Baidu)
rabbitmqctl start_app
rabbitmq-plugins enable rabbitmq_managementAccess ip:15672
Account password guest: guest
Use springboot integrated use
amqpAdmin in springboot encapsulates system operations on exchanges, queues, etc., rabbitmqTemplate encapsulates the processing of sending and receiving messages
Add dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
Configure rabbitmq address
Write configuration class
/**
* Created by Administrator on 2018/11/30.
*自动配置类 RabbitAutoConfiguration
* 里面注入了 rabbitTamplate CachingConnectionFactory RabbitConnectionFactoryBean
* AmqpAdmin 系统管理组件
* RabbitProperties 封装了rabbitmq 配置
*@EnableRabbit // 开启rabbitmq 的自动配置
*/
@EnableRabbit // 开启rabbitmq 的自动配置
@Configuration
public class MyRabbitmqConfig {
}
Manually add an exchange to the page exchanges
exchange of direct strategy
Fanout strategy switch
exchange of topic strategy
Add message queue
Binding exchange and message queue
Write message sending server code
@Test
public void send() {
// 路由键默认等于队列名称
Map map = new HashMap();
map.put("123","123");
map.put("lists",new int[]{1,2,3});
rabbitTemplate.convertAndSend("xiaodu.direct","key01",map);
}
View rabbitmq management page
If the transmission is an object, the message is the result of serialization of the object
Accept code
@Test
public void receive() {
// 参数为队列的名称 ,另外接受到的消息能自动反序列化回来,(注意实现序列化接口)
// 消息取出来会自动删除
Object o = rabbitTemplate.receiveAndConvert("xiaodu.queue01");
System.out.println("接受到消息为==" + o);
}
Serialization rules
In springboot, SimpleMessageConverter is automatically used by default as the serialization rule of jdk
Use the messageConverter converted to json and configure it in the configuration class
After use, the content Payload in the message body is of json type, not the result of serialization of the stored object
Test broadcast mode: broadcast mode does not need to specify the routing key, but remember to bind the switch to the queue you need
Use the listener to get the messages in the queue: Use the annotation @RabbitListener (note that you must add @EnableRabbit to the startup class when you turn on rabbitmq configuration)
@Service
public class RabbitmqService {
@RabbitListener(queues = "xiaodu.queue01")
public void receive(User user) {
System.out.println("监听到的消息为 = " + user);
}
}
//使用参数 Message 获取消息内容
@RabbitListener(queues = "xiaodu.queue01")
public void receive02(Message message) {
byte[] body = message.getBody();
System.out.println("接受到的消息体内容 = " + new String(body));
MessageProperties messageProperties = message.getMessageProperties();
System.out.println("接受到的消息headers= " + messageProperties);
}
Use AmqpAdmin to create exchanges, queues, binding relationships, etc.
1 Create the exchange exchenge
According to different strategies, you can create different exchanges, create queues, and create binding relationships
@Autowired
private AmqpAdmin amqpAdmin;
/**
* 创建
*/
@Test
public void createExchange() {
// 参数 交换器名, durable 是否持久化, autoDelete 是否自动删除
Exchange exchange = new DirectExchange("exchange.direct",true,false);
amqpAdmin.declareExchange(exchange);
// 创建 queue-01 队列
amqpAdmin.declareQueue(new Queue("queue-01"));
// 创建绑定关系
// destination 绑定的目的地, Binding.DestinationType destinationType 绑定的类型是交换器还是队列,
// String exchange 绑定到那个交换器上, String routingKey 路由键, Map<String, Object> arguments 命令参数
Binding binding = new Binding("queue-01",Binding.DestinationType.QUEUE,
"exchange.direct","exkey-01",null );
amqpAdmin.declareBinding(binding);
}
Other operations are similar, you can also register your switch directly in the configuration file. Queue etc
Reproduced rabbitmq distributed: https://blog.csdn.net/gokeiryou263/article/details/54312094