前提

已经搭建好RabbitMQ基本环境,搭建步骤查看博客:搭建RabbitMQ基本环境

项目结构

实现过程

application.yml配置

添加一个application.yml配置文件,内容如下:

1
2
3
4
5
6
7
8
9
10
server:
port: 80

spring:
rabbitmq:
host: 192.168.0.145 #虚拟机IP
port: 5672 #RabbitMQ默认TCP监听端口
username: admin #登录RabbitMQ管理系统的用户名
password: admin #登录RabbitMQ管理系统的密码
virtual-host: / #默认就是/,可不加

发送消息页面

命名为:sendInformationPage.html,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>发送消息</title>
</head>
<body>
<form action="/sendInformation" method="post">
消息:<input type="text" name="information">
<input type="submit" value="发送">
</form>
</body>
</html>

RabbitMQ配置类

在rabbitmq-common模块下新建,命名为:RabbitMQConfig,内容如下:

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
package com.ledao.common.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;

/**
* @author LeDao
* @company
* @create 2021-09-04 13:03
*/
@Configuration
public class RabbitMQConfig {

/**
* direct交换机名称
*/
public static final String DIRECT_EXCHANGE = "directExchange";

/**
* direct队列名称
*/
public static final String DIRECT_QUEUE = "directQueue";

/**
* direct路由key
*/
public static final String DIRECT_ROUTINGKEY = "directRoutingKey";

/**
* 定义一个direct交换机
*
* @return
*/
@Bean
public DirectExchange directExchange() {
return new DirectExchange(DIRECT_EXCHANGE);
}

/**
* 定义一个direct队列
*
* @return
*/
@Bean
public Queue directQueue() {
return new Queue(DIRECT_QUEUE);
}

/**
* 定义一个队列和交换机的绑定
*
* @return
*/
@Bean
public Binding directBinding() {
return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTINGKEY);
}
}

生产者Service接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.ledao.producer.service;

/**
* RabbitMQService生产者接口
*
* @author LeDao
* @company
* @create 2021-09-04 13:27
*/
public interface RabbitMQProducerService {

/**
* 发送消息
*
* @param information
*/
void sendInformation(String information);
}

Service接口实现类

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
package com.ledao.producer.service.impl;

import com.ledao.common.config.RabbitMQConfig;
import com.ledao.producer.service.RabbitMQProducerService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* RabbitMQService生产者接口实现类
*
* @author LeDao
* @company
* @create 2021-09-04 13:31
*/
@Service("rabbitMQProducerService")
public class RabbitMQProducerServiceImpl implements RabbitMQProducerService {

@Resource
private AmqpTemplate amqpTemplate;

@Override
public void sendInformation(String information) {
amqpTemplate.convertAndSend(RabbitMQConfig.DIRECT_EXCHANGE, RabbitMQConfig.DIRECT_ROUTINGKEY, information);
}
}

IndexController.java

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
package com.ledao.producer.controller;

import com.ledao.producer.service.RabbitMQProducerService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
* @author LeDao
* @company
* @create 2021-09-04 13:48
*/
@Controller
public class IndexController {

@Resource
private RabbitMQProducerService rabbitMQProducerService;

/**
* 发送消息页面
*
* @return
*/
@RequestMapping("/toSendInformationPage")
public ModelAndView sendInformation() {
ModelAndView mav = new ModelAndView();
mav.setViewName("sendInformationPage");
return mav;
}

/**
* 发送消息
*
* @param information
* @return
*/
@ResponseBody
@RequestMapping("/sendInformation")
public String sendInformation(String information) {
rabbitMQProducerService.sendInformation(information);
return "发送消息到RabbitMQ成功<a href='/toSendInformationPage'><button>继续发送</button></a>";
}
}

测试

先启动rabbitmq-consumer模块再启动rabbitmq-producer模块,在浏览器地址栏输入:http://localhost/toSendInformationPage,输入消息后发送即可

查看消息步骤:

  1. 在浏览器地址栏输入:http://虚拟机IP:15672,输入用户名和密码后进入RabbitMQ管理系统

  2. 点击Queues选项查看所有队列,点击directQueue进入队列内部去查看消息

  3. 打开Get messages选项,在Messages文本框中输入查看消息的条数,点击Get Message(s)按钮即可查看消息