一、导入依赖等杂活
依赖
<?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>org.example</groupId>
<artifactId>raabbitmq-example</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>provider</module>
<module>consumer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<version>2.1.4.RELEASE</version>
<artifactId>spring-boot-starter-parent</artifactId>
</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-amqp</artifactId>
</dependency>
</dependencies>
</project>
二、生产者
配置
server:
port: 8021
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: 192.168.136.128
port: 5673
username: admin
password: admin
mq:
provider:
routing: MyRouting #绑定的键
queue: MyQueue #队列
exchange: MyExchange #交换机
配置类
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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class Config{
//用来获取yml中的配置
@Autowired
private Environment env;
//创建队列
@Bean
public Queue MyDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("TestDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue(env.getProperty("mq.provider.queue"),true);
}
//创建交换机
@Bean
DirectExchange MyDirectExchange() {
// return new DirectExchange("TestDirectExchange",true,true);
return new DirectExchange(env.getProperty("mq.provider.exchange"),true,false);
}
//绑定 将交换机与队列绑定
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(MyDirectQueue())
.to(MyDirectExchange())
.with(env.getProperty("mq.provider.routing"));
}
}
创建一个controller
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
public class SendMessageController {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Autowired
private Environment env; //获取配置里的参数值
@GetMapping("/sendDirectMessage")
public String sendDirectMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "test message, hello!";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String,Object> map=new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
rabbitTemplate.convertAndSend(env.getProperty("mq.provider.exchange"), env.getProperty("mq.provider.routing"), map);
return "ok";
}
}
运行的主类就不多讲了
二、消费者
yml配置文件
server:
port: 8022
spring:
application:
name: rabbitmq-consumer
rabbitmq:
host: 192.168.136.128
port: 5673
username: admin
password: admin
mq:
consumer:
queue: MyQueue
exchange: MyExchange
routing: MyRouting
配置类
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.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class Config {
//获取yml中的配置
@Autowired
private Environment env;
//队列
@Bean
public Queue MyDirectQueue() {
return new Queue(env.getProperty("mq.consumer.queue"),true);
}
//Direct交换机
@Bean
DirectExchange MyDirectExchange() {
return new DirectExchange(env.getProperty("mq.consumer.exchange"));
}
//绑定 将队列和交换机绑定
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(MyDirectQueue())
.to(MyDirectExchange())
.with(env.getProperty("mq.consumer.routing"));
}
}
Listener
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "${mq.consumer.queue}")//监听队列
public class Listener {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
}
}
代码在我github上也有https://github.com/Codeouter/Example/tree/master/Project/raabbitmq-example