消息中间件介绍 RabbitMq Docker上安装
消息队列中间件简介

什么是rabbitMq



架构图

主要概念


Docker下安装
下载镜像

创建容器


打开网址: 进入 可以 看到下面的图片

rabbitMq 的模式介绍:
Direcrt 模式
我们需要讲消息发送给唯一一个节点时候使用这种模式 这是最简单的一个模式:

但是这种模式需要注意下面几点:

我们来创建 一个队列: 创建 test 队列

其中的参数是:

接下来我们先写一个简单的Direct模式:
1.首先创建一个模块 rabbitmq_demo
2.pom引入依赖
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
3.配置application.yml 加入配置文件
spring:
rabbitmq:
host: 192.168.1.123
server:
port: 9008
4.接下来创建 启动类

5.接下来创建 消费者

6.最后我们写一个测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RabbitMqDemoApplication.class)
public class MqTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSend() {
rabbitTemplate.convertAndSend("test","我是测试信息");
}
}
7.测试 先启动程序 后启动测试类
观察打印输出

分裂模式 fanout
当我们需要讲消息一次发送给多个队列的时候需要使用到这个模式

但是需要注意下面的几点:

接下来我们需要 创建交换器 名字是testexchange

之后你会发现 在交换器列表出现了此交换器

接下来我们要创建两个队列

进入 交换机的配置页面

将我们想要配置的那两个队列配置到交换器上面

接下来我们写代码 :
第一个队列 消费者
@Component
@RabbitListener(queues = "testexchangequeue")
@Slf4j
public class Customer2 {
@RabbitHandler
public void showMessage(String message) {
log.info("testexchangequeue队列收到的消息"+message);
}
}
第二个队列 消费者
@Slf4j
@Component
@RabbitListener(queues ="xxx" )
public class Customer3 {
@RabbitHandler
public void showMessage(String msg) {
log.info("xxx队列收到的消息" + msg);
}
}
测试类发送消息:
@Test
public void testSendFanout() {
System.out.println("fanout已经分发");
rabbitTemplate.convertAndSend("testexchange",null,"fanout模式发送的消息");
}
测试结果如下: 两个队列都收到了消息。

主题模式 topic

主题模式很复杂 有很多注意点 需要我们注意:


我们先来创建一个交换器 类型选择 tocpic

进入设置 进行配置 记得要配置 规则 routingKey

如上图 一共绑定了三个队列 并定了三个规则
编写测试类 也就是发送消息的 测试类 :
@Test
public void testSendTop1() {
rabbitTemplate.convertAndSend("topictest","goods.aaa","test 。。 goods.# 。。 topic");
}
@Test
public void testSendTop2() {
rabbitTemplate.convertAndSend("topictest","xx.log"," testexchangequeue 。。 #.log .. topic ");
}
@Test
public void testSendTop3() {
rabbitTemplate.convertAndSend("topictest", "goods.log", "xxx 。。goods.log 。。 topic ");
}
分别测试 结果如下所示:
testSendTop1 :

testSendTop2:

testSendTop3 结果 三个队列都收到消息了
