springboot整合RabbitMq
direct方式
发送端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8091
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.RabbitMQ配置类DirectRabbitConfig
@Configuration
public class DirectRabbitConfig {
//队列
@Bean
public Queue directQueue() {
return new Queue("directQueue",true);
}
//Direct交换机
@Bean
DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
//绑定 将队列和交换机绑定
@Bean
Binding bindingDirect(Queue directQueue,DirectExchange directExchange) {
return BindingBuilder.bind(directQueue).to(directExchange).with("routingkey001");
}
}
4.发送消息
@Controller
public class SendController {
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("sendMessage")
@ResponseBody
public String sendMessage(String message){
String messageId = String.valueOf(UUID.randomUUID());
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",message);
map.put("createTime",createTime);
//将消息携带绑定键值:routingkey001发送到交换机directExchange
rabbitTemplate.convertAndSend("directExchange", "routingkey001", map);
return "ok";
}
}
接收端
1.添加pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8092
spring:
application:
name: rabbitmq-consumer
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.为了防止队列不存在报错,这里我们也添加RabbitMQ配置类DirectRabbitConfig
@Configuration
public class DirectRabbitConfig {
//队列
@Bean
public Queue directQueue() {
return new Queue("directQueue",true);
}
//Direct交换机
@Bean
DirectExchange directExchange() {
return new DirectExchange("directExchange");
}
//绑定 将队列和交换机绑定
@Bean
Binding bindingDirect(Queue directQueue,DirectExchange directExchange) {
return BindingBuilder.bind(directQueue).to(directExchange).with("routingkey001");
}
}
4.接收消息
@Component
@RabbitListener(queues = "directQueue")//监听的队列名称 directQueue
public class ReceiveMessage {
/**
* 如果这个方法正常结束,spring会自动确认消息
* 如果这个方法报错,消息不会确认,并且进入死循环
* 因此在处理消息之前需要防重复处理
* @param testMessage
*/
@RabbitHandler
public void process(Map testMessage) {
System.out.println("准备接收消息了");
//1.防重复处理代码
//2.消息处理代码
System.out.println("directReceiver消费者收到消息 : " + testMessage.toString());
}
}
fanout方式
发送端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8091
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.RabbitMQ配置类FanoutRabbitConfig
@Configuration
public class FanoutRabbitConfig {
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
}
4.发送消息
@Controller
public class SendController {
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("sendMessage")
@ResponseBody
public String sendMessage(String message){
String messageId = String.valueOf(UUID.randomUUID());
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",message);
map.put("createTime",createTime);
rabbitTemplate.convertAndSend("fanoutExchange", "", map);
return "ok";
}
}
接收端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8092
spring:
application:
name: rabbitmq-consumer
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.RabbitMQ配置类FanoutRabbitConfig
@Configuration
public class FanoutRabbitConfig {
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
}
4.接收消息
@Component
public class FanoutReceive {
@RabbitListener(bindings = {
@QueueBinding(value =@Queue(),//创建一个随机队列
exchange = @Exchange(name = "fanoutExchange",type = "fanout"))
})
public void fanoutReceive(Map messgae){
System.out.println("fanoutReceiver01消费者收到消息"+messgae.toString());
}
@RabbitListener(bindings = {
@QueueBinding(value =@Queue(),//创建一个随机队列
exchange = @Exchange(name = "fanoutExchange",type = "fanout"))
})
public void fanoutReceive02(Map messgae){
System.out.println("fanoutReceiver02消费者收到消息"+messgae.toString());
}
}
topic方式
发送端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8091
spring:
application:
name: rabbitmq-provider
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.RabbitMQ配置类FanoutRabbitConfig
@Configuration
public class TopicRabbitConfig {
@Bean
TopicExchange topicExchange(){
return new TopicExchange("topicExchange");
}
}
4.发送消息
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("sendMessage")
@ResponseBody
public String sendMessage(String message){
String messageId = String.valueOf(UUID.randomUUID());
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",message);
map.put("createTime",createTime);
rabbitTemplate.convertAndSend("topicExchange", "aa.bb", map);
return "ok";
}
}
接收端
1.添加pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.yml配置
server:
port: 8092
spring:
application:
name: rabbitmq-consumer
rabbitmq:
host: 47.110.157.82
port: 5672
username: liu
password: 123456
3.接收消息
@Component
public class TopicReceive {
@RabbitListener(bindings = {
@QueueBinding(value =@Queue("topicQueue001"),//创建一个随机队列
key = {"aa"},
exchange = @Exchange(name = "topicExchange",type = "topic"))
})
public void topicReceive01(Map messgae){
System.out.println("topicReceive01消费者收到消息"+messgae.toString());
}
@RabbitListener(bindings = {
@QueueBinding(value =@Queue("topicQueue002"),//创建一个随机队列
key = {"aa.*"},
exchange = @Exchange(name = "topicExchange",type = "topic"))
})
public void topicReceive02(Map messgae){
System.out.println("topicReceive02消费者收到消息"+messgae.toString());
}
@RabbitListener(bindings = {
@QueueBinding(value =@Queue("topicQueue003"),//创建一个随机队列
key = {"aa.#"},
exchange = @Exchange(name = "topicExchange",type = "topic"))
})
public void topicReceive03(Map messgae){
System.out.println("topicReceive03消费者收到消息"+messgae.toString());
}
}