目录
一、安装RabbitMQ
二、集成案例
1、为通信的服务添加依赖
2、配置yml
三、直连交换机实现
1、配置类
2、发送消息的类
3、接收消息的类
4、测试使用sendMessage发送消息,服务器可以看到消息
四、主题交换机实现
1、匹配规则
2、主题模式的实现案例
(1)配置类
(2)服务调用
(3)消息接收类
一、安装RabbitMQ
见我的博客:windows 安装 RabbitMQ的安装包_编码语者 Dragon Wu的博客-CSDN博客
windows 安装 RabbitMQ的安装包_编码语者 Dragon Wu的博客-CSDN博客
二、集成案例
1、为通信的服务添加依赖
org.springframework.boot
spring-boot-starter-amqp
2、配置yml
spring:
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672 # rabbitmq对外的服务端口是5672,对外的管理端口是15672
server:
port: 8080
三、直连交换机实现
1、配置类
package com.wxl.rabbitmq.direct;
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 RabbitConfigDirect {
@Bean
public Queue directQueue(){
//消息队列
return new Queue("direct_queue");
}
@Bean
public DirectExchange directExchange(){
//直连交换机
return new DirectExchange("directExchange");
}
@Bean
public Binding bindingDirect(){
//将队列和交换机绑定
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
}
}
org.springframework.boot
spring-boot-starter-amqp
2、配置yml
spring:
rabbitmq:
host: localhost
username: guest
password: guest
port: 5672 # rabbitmq对外的服务端口是5672,对外的管理端口是15672
server:
port: 8080
三、直连交换机实现
1、配置类
package com.wxl.rabbitmq.direct;
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 RabbitConfigDirect {
@Bean
public Queue directQueue(){
//消息队列
return new Queue("direct_queue");
}
@Bean
public DirectExchange directExchange(){
//直连交换机
return new DirectExchange("directExchange");
}
@Bean
public Binding bindingDirect(){
//将队列和交换机绑定
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
}
}
1、配置类
package com.wxl.rabbitmq.direct;
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 RabbitConfigDirect {
@Bean
public Queue directQueue(){
//消息队列
return new Queue("direct_queue");
}
@Bean
public DirectExchange directExchange(){
//直连交换机
return new DirectExchange("directExchange");
}
@Bean
public Binding bindingDirect(){
//将队列和交换机绑定
return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct");
}
}
如果你需要将多个队列绑定到一个交互机上还可以这样写
2、发送消息的类
package com.wxl.rabbitmq.direct;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
//rabbitmq里的直连模式
public class MessageServiceRabbitmqDirect {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String id){
amqpTemplate.convertAndSend("directExchange","direct",id);
}
}
3、接收消息的类
package com.wxl.rabbitmq.direct;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
//消息接收类
public class MessageListener {
@RabbitListener(queues = "direct_queue")
public void receive(String id){
System.out.println("已完成短信发送业务(rabbitmq direct)id:"+id);
}
}
package com.wxl.rabbitmq.direct;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
//消息接收类
public class MessageListener {
@RabbitListener(queues = "direct_queue")
public void receive(String id){
System.out.println("已完成短信发送业务(rabbitmq direct)id:"+id);
}
}
注意这里添加了@RabbitListener注解
4、测试使用sendMessage发送消息,服务器可以看到消息
如果设置了多个监听,则会进行轮询的处理,
四、主题交换机实现
使用主题交互机模式同样可以实现直连交互机模式的效果,主题模式的好处就是即使同样使用一台交换机也能实现不同规则的队列放行。
1、匹配规则
2、主题模式的实现案例
与直连模式类似
(1)配置类
package com.wxl.rabbitmq.topic;
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 RabbitConfigTopic {
@Bean
public Queue topicQueue(){
//消息队列
return new Queue("topic_queue");
}
@Bean
public Queue topicQueue2(){
//消息队列
return new Queue("topic_queue2");
}
@Bean
public DirectExchange directExchange(){
//直连交换机
return new DirectExchange("topicExchange");
}
@Bean
public Binding bindingTopic(){
//将队列和交换机绑定
return BindingBuilder.bind(topicQueue()).to(directExchange()).with("topic.*.id");//支持模糊匹配,如topic.order.id也能被匹配到
}
@Bean
public Binding bindingTopic2(){
//将队列和交换机绑定
return BindingBuilder.bind(topicQueue2()).to(directExchange()).with("topic2");
}
}
(2)服务调用
package com.wxl.rabbitmq.topic;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
//rabbitmq里的主题模式
public class MessageServiceRabbitmqTopic {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String id){
amqpTemplate.convertAndSend("topicExchange","topic.order.id",id);//交换机名,绑定名
}
}
(3)消息接收类
package com.wxl.rabbitmq.topic;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
//消息接收类
public class MessageListener {
@RabbitListener(queues = "topic_queue")
public void receive(String id){
System.out.println("已完成短信发送业务(rabbitmq topic1)id:"+id);
}
@RabbitListener(queues = "topic_queue2")
public void receive2(String id){
System.out.println("已完成短信发送业务(rabbitmq top2)id:"+id);
}
}
package com.wxl.rabbitmq.topic;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
//rabbitmq里的主题模式
public class MessageServiceRabbitmqTopic {
@Autowired
private AmqpTemplate amqpTemplate;
public void sendMessage(String id){
amqpTemplate.convertAndSend("topicExchange","topic.order.id",id);//交换机名,绑定名
}
}



