SpringBoot-RabbitMQ
一、开启rabbitMq
启动:service rabbitmq-server start
关闭:service rabbitmq-server stop
重启:service rabbitmq-server restart
rabbitmq-可视化界面:http://ip:15672
注:guest 账号只能在localhost 登录,倘若安装成功但进不去页面看看是不是虚拟机端口未开放
开放15672端口:firewall-cmd --zone=public --add-port=15672/tcp --permanent
关闭15672端口:firewall-cmd --zone=public --remove-port=15672/tcp --permanent
配置立即生效 :firewall-cmd --reload
执行关闭防火墙命令: systemctl stop firewalld.service
执行开启防火墙命令:systemctl start firewalld.service
二、环境搭建
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.rabbitmq</groupId>
<artifactId>springboot-rabbitmq</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--springboot集成了单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
</project>
application.yml
spring:
application:
name: springboot-rabbitmq
#配置rabbitMq 服务器
rabbitmq:
host: 192.168.128.244
port: 5672 # AMQP协议端口
username: admin
password: admin
virtual-host: /admin-host
RabbitTemplate
: 用来简化操作, 使用时直接注入
三、五种工作模式
本文只测试前五种
1、hello word
模式简介
一个生产者对应一个消费者!!!
消息生产者
@SpringBootTest(classes = SpringBootStart.class)
@RunWith(SpringRunner.class)
public class SpringBootStartTest {
@Resource
RabbitTemplate rabbitTemplate;
@Test
public void helloWord(){
rabbitTemplate.convertAndSend("hello","hello rabbitmq");
}
}
消息消费者
package com.application.consumer;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queuesToDeclare =
// value 消息队列名字, durable 队列是否持久化, exclusive 队列是否独占,autoDelete 是否自动删除
@Queue(value="hello",durable="false",exclusive="false",autoDelete="true")
)
public class HelloConsumer {
@RabbitHandler
public void receive(String message){
System.out.println(message);
}
}
测试结果:
2、Work (工作模式)
模式简介
一个生产者对应多个消费者,但是一条消息只能有一个消费者获得消息!!!
-
公平模式
生产者:
//work @Test public void work(){ for (int i = 0; i < 10; i++) { rabbitTemplate.convertAndSend("work","work message"+ i ); } }
消费者:
@Component public class WorkConsumer { @RabbitListener(queuesToDeclare = @Queue("work")) public void receive(String message){ System.out.println("work1 = "+message); } @RabbitListener(queuesToDeclare = @Queue("work")) public void receive2(String message){ System.out.println("work2 = "+message); } }
测试结果:
3、 Publish(广播模式)
模式简介:
一个消费者将消息首先发送到交换器,交换器绑定到多个队列,然后被监听该队列的消费者所接收并消费。
ps:X表示交换器,在RabbitMQ中,交换器主要有四种类型:direct、fanout、topic、headers,这里的交换器是 fanout。
消息生产者:
//fanout
@Test
public void fanout(){
rabbitTemplate.convertAndSend("logs","","work message");
}
消息消费者:
@Component
public class FanoutConsumer {
@RabbitListener(bindings = {
@QueueBinding(exchange = @Exchange(value = "logs",type = "fanout"),//绑定交换机 类型为fanout
value=@Queue // 创建临时队列
)
})
public void receive(String message){
System.out.println("receive1 =" +message);
}
@RabbitListener(bindings = {
@QueueBinding(exchange = @Exchange(value = "logs",type = "fanout"),//绑定交换机 类型为fanout
value=@Queue // 创建临时队列
)
})
public void receive2(String message){
System.out.println("receive2 =" +message);
}
}
结果:
4 route(路由模式)
模式简介:
生产者将消息发送到direct交换器,在绑定队列和交换器的时候有一个路由key,生产者发送的消息会指定一个路由key,那么消息只会发送到相应key相同的队列,接着监听该队列的消费者消费消息。
也就是让消费者有选择性的接收消息。
消息生产者
//route
@Test
public void route(){
rabbitTemplate.convertAndSend("route","info","info message");
rabbitTemplate.convertAndSend("route","error","error message");
}
消息消费者
@Component
public class RouteConsumer {
@RabbitListener(
bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(value = "route",type = "direct"),
key = {"info","error"}
)
)
public void receive(String mes){
System.out.println("receive1 = " + mes);
}
@RabbitListener(
bindings = @QueueBinding(
value = @Queue,
exchange = @Exchange(value = "route",type = "direct"),
key = {"error"}
)
)
public void receive1(String mes){
System.out.println("receive2 = " + mes);
}
}
5、Topics(主题模式)
模式简介:
上面的路由模式是根据路由key进行完整的匹配(完全相等才发送消息),这里的通配符模式通俗的来讲就是模糊匹配。
通配符 | 作用 |
---|---|
* | 一次匹配一个单词 |
# | 一次匹配多个单词 |
. | 单词分割 |
消息生产者:
//Topics
@Test
public void topics(){
rabbitTemplate.convertAndSend("Topics","user.he.info","user.he.info");
rabbitTemplate.convertAndSend("Topics","user.hell.zz.info","user.hell.zz.info");
}
消息消费者:
@Component
public class TopicConsumer {
@RabbitListener( bindings = @QueueBinding(
value = @Queue, //创建临时队列
exchange = @Exchange(value = "Topics", type = "topic"),
key = {"user.hell.info"}
)
)
public void receive1(String message){
System.out.println("receive 1 = " + message);
}
@RabbitListener( bindings = @QueueBinding(
value = @Queue, //创建临时队列
exchange = @Exchange(value = "Topics", type = "topic"),
key = {"user.*.info"}
)
)
public void receive2(String message){
System.out.println("receive 2 = " + message);
}
@RabbitListener( bindings = @QueueBinding(
value = @Queue, //创建临时队列
exchange = @Exchange(value = "Topics", type = "topic"),
key = {"user.#.info"}
)
)
public void receive3(String message){
System.out.println("receive 3 = " + message);
}
}