RabbitMQ简说原创
# 前置任务
安装RabbitMQ
基于Linux进行安装
启动命令:rabbitmq-service
停止命令:rabbitmqctl stop
输入http://ip:15672/ (注意关闭防火墙),转到RabbitQ后台管理界面
# 开始上手
本次只需要简单的创建两个springboot的项目即可,provider(生产者)
consumer(消费者)
后续只需要使用单个项目进行发布和接收就好
# 创建Provider
# 导入依赖
<!--rabbitmq-->
<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>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 配置文件
server:
port: 8080
spring:
application:
name: provider # 项目名称
#RabbitMQ
rabbitmq:
host: 192.168.220.128 #RabbitMQ服务的IP地址
port: 5672
#虚拟host 可以不设置,存在默认值
virtual-host: 20230324
username: joyin # 用户名
password: joyin # 密码
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# virtual-host 虚拟host创建
我们可以在Users界面 创建一个新的用户
为我们新创建的用户绑定Virtual Host
# 配置直接交换机
package com.example.rabbitmqprovider.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置直接交换机
*/
@Configuration
public class DirectConfig {
// 交换机名称
public static final String EXCHANGE = "x.direct";
// 队列名称
public static final String QUEUE_1 = "Q1";
// 队列名称
public static final String QUEUE_2 = "Q2";
// 类似路由标识 根据当前路由确认队列
public static final String ORANGE = "orange";
public static final String BLACK = "black";
public static final String GREEN = "green";
@Bean
public DirectExchange directExchange() {
return new DirectExchange(EXCHANGE);
}
@Bean
public Queue queueOne() {
return new Queue(QUEUE_1);
}
@Bean
public Queue queueTwo() {
return new Queue(QUEUE_2);
}
// 交换机 队列 路由绑定
@Bean
public Binding orangeBinding(Queue queueOne, DirectExchange directExchange) {
return BindingBuilder.bind(queueOne)
.to(directExchange)
.with(ORANGE);
}
@Bean
public Binding blackBinding(Queue queueTwo, DirectExchange directExchange) {
return BindingBuilder.bind(queueTwo)
.to(directExchange)
.with(BLACK);
}
@Bean
public Binding greenBinding(Queue queueTwo, DirectExchange directExchange) {
return BindingBuilder.bind(queueTwo)
.to(directExchange)
.with(GREEN);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 消息发布层
package com.example.rabbitmqprovider.controller;
import com.example.rabbitmqprovider.config.DirectConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/direct")
public class DirectController {
@Autowired
RabbitTemplate rabbitTemplate;
// 向队列发送消息
@RequestMapping("/send/{key}/{msg}")
public String send(@PathVariable String key,
@PathVariable String msg) {
rabbitTemplate.convertAndSend(DirectConfig.EXCHANGE,
key, // 设置routing key (路由)
msg); // 消息
return "success"; // 成功标识
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 测试
这是使用postman 浏览器url直接调用也可以
记得启动服务
# 创建Consumer
# 导入依赖
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
1
2
3
4
5
2
3
4
5
# 配置文件
注意端口要避免冲突
server:
port: 8081
spring:
application:
name: consumer # 项目名称
#RabbitMQ
rabbitmq:
host: 192.168.220.128 #RabbitMQ服务的IP地址
port: 5672
#虚拟host 可以不设置,存在默认值
virtual-host: 20230324
username: joyin # 用户名
password: joyin # 密码
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 消息接收层
上次更新: 2024/03/20, 17:21:46