SpringBoot整合RabbitMQ
创建一个空项目
生产者
创建一个springboot模块myproducer
yml配置文件
spring:
rabbitmq:
host: 192.168.118.130
port: 5672
virtual-host: /
username: guest
password: guest
发送消息
package com.wn.myproducer8080.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping("/sendemail")
public String sendemail(){
//发送信息
rabbitTemplate.convertAndSend("exchangetopic","email","email");
return "sendemail";
}
}
String exchange:交换机名称
String routingkey:路由名
Object object:消息
消费者
创建一个SpringBoot模块mqcomsumer8081
在启动类上加注解
@EnableRabbit //开启基于注解的RabbitMQ模式
创建一个配置类
创建交换机、队列、绑定关系
package com.wn.mqcomsumer8081.configuration;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConsumerConfiguration {
/***
* 交换机
*/
@Bean
public TopicExchange topicExchange(){
return new TopicExchange("exchangetopic");
}
/**
* 队列
*/
@Bean
public Queue queueemail(){
return new Queue("queueemail");
}
@Bean
public Queue queuesms(){
return new Queue("queuesms");
}
/**
* 绑定
*/
@Bean
public Binding bindingemail(TopicExchange exchangetopic,Queue queueemail){
return BindingBuilder.bind(queueemail).to(exchangetopic).with("#.email.#");
}
@Bean
public Binding bindingsms(TopicExchange exchangetopic,Queue queuesms){
return BindingBuilder.bind(queuesms).to(exchangetopic).with("#.sms.#");
}
}
绑定还可以这样写
//调用方法
@Bean
public Binding bindingemail(){
return BindingBuilder.bind(queueemail()).to(topicExchange()).with("#.email.#");
}
//利用注解
@Bean
public Binding bindingemail(TopicExchange exchangetopic,
@Qualifier("queueemail") Queue queue){
return BindingBuilder.bind(queue).to(exchangetopic).with("#.email.#");
}
交换机默认属性
队列默认属性
监听
package com.wn.mqcomsumer8081.consumer;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
@Component
public class MyConsumer {
@RabbitListener(queues = "queueemail")
public void handlerEmail(String str, Message message, Channel channel){
System.out.println("----------------");
System.out.println("处理邮件");
System.out.println("str:"+str);
System.out.println("message:"+message);
System.out.println("channel:"+channel);
System.out.println("----------------");
}
@RabbitListener(queues = "queuesms")
public void handlerSms(String str, Message message, Channel channel){
System.out.println("----------------");
System.out.println("处理短信");
System.out.println("str:"+str);
System.out.println("message:"+message);
System.out.println("channel:"+channel);
System.out.println("----------------");
}
}
注解@RabbitListener,会监控指定的队列
处理方法可以添加三个参数:
String str:消息体
Message message:完整的消息对象
Channel channel:信道
注意:导包不要导错了import com.rabbitmq.client.Channel;