# RabbitMQDemo
**Repository Path**: smarkWzp/RabbitMQDemo
## Basic Information
- **Project Name**: RabbitMQDemo
- **Description**: RabbitMQ消息中间件学习,虚拟化容器技术-使用Docker-集成安装Rabbitmq,消费者异常处理,消息转移至死信队列等逻辑了解。
- **Primary Language**: Java
- **License**: MulanPSL-1.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 2
- **Forks**: 0
- **Created**: 2020-01-14
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# RabbitMQDemo
#### 介绍
RabbitMQ消息中间件学习,虚拟化容器技术-使用Docker-集成安装Rabbitmq,消费者异常处理,消息转移至死信队列等逻辑了解。
#### 软件架构
RabbitMQ是一个开源的消息队列服务器 ,用来通过普通协议在完全不同
的应用之间共享数据,Rabbitmq是使用Erlang语言(数据传递)来编写
的,并且Rabbitmq是基于AMQP协议(协议是一种规范和约束)的。开源做
到跨平台机制。
消息队列中间件是分布式系统中重要的组件,主要解决应用 解耦,异步消
息,流量削锋等问题,实现高性能,高可用,可伸缩和最终一致性架构。
目前使用较多的消息队列有 、ActiveMQ,RabbitMQ,ZeroMQ,Kafka,
MetaMQ,RocketMQ。
#### 安装教程
利用docker安装rabbitmq
获取rabbit镜像:
创建并运行容器:
docker pull rabbitmq:management 1
其他服务命令:
容器运行正常
使用http://127.0.0.1:15672访问rabbit控制台
下载代码后启动consumer,利用单元测试模拟消息发送等
#### 使用说明
生产者工程:springboot-rabbitmq-fadeout-producers
1、引入依赖
org.springframework.boot
spring-boot-starter-amqp
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
org.projectlombok
2、定义订单的消息生产者 Sender.java
lombok
true
org.springframework.boot
spring-boot-starter-test
test
package
com.itbooking.springbootrabbitmqfadoutproducers.mq;
import org.springframework.amqp.core.AmqpTemplate;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class Sender {
@Autowired
private AmqpTemplate amqpTemplate;
@Value("${mq.config.exchange}")
private String exchangeName;
public void sendMessage() throws
InterruptedException {
amqpTemplate.convertAndSend(exchangeName,"","订
单保存成功,去发送短信和微信.....");
}
}
3、定义配置文件和配置交换机
消费者工程:springboot-rabbitmq-fadeout-consumbers
1:定义消费者
定义配置文件 application.properties
2:定义消息消费类: WeixinReceiver SMSReceiver RedReceiver
server.port=8094
spring.application.name=springboot-rabbitmq-topic-
consumer
# rabbitmq
spring.rabbitmq.host=192.168.153.173
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
# 设置交换机
mq.config.exchange=order.fadout
server.port=8093
spring.application.name=springboot-rabbitmq-topic-
consumer
# rabbitmq
spring.rabbitmq.host=192.168.153.173
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
# 设置交换机
mq.config.exchange=order.fadout
# 定义队列三个消费者队列
# 消息队列
mq.config.queue.sms=order.sms
# 微信队列
mq.config.queue.weixin=order.weixin
# 红包队列
mq.config.queue.red=order.red
增加需求
package
com.itbooking.springbootrabbitmqfadeoutconsumbers.mq;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(bindings =@QueueBinding(
value = @Queue(value =
"${mq.config.queue.red}",autoDelete = "true"),
exchange = @Exchange(value =
"${mq.config.exchange}",type = ExchangeTypes.FANOUT)
))
public class RedReceiver {
@RabbitHandler
public void process(String msg) {
System.out.println("Red发送10元红包----------->接
受到的的消息是:" + msg);
}
}
package
com.itbooking.springbootrabbitmqfadeoutconsumbers.mq;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
1
2
3
4
import org.springframework.stereotype.Component;
@Component
@RabbitListener(bindings =@QueueBinding(
value = @Queue(value =
"${mq.config.queue.sms}",autoDelete = "true"),
exchange = @Exchange(value =
"${mq.config.exchange}",type = ExchangeTypes.FANOUT)
))
public class SMSReceiver {
@RabbitHandler
public void process(String msg) {
System.out.println("SMS----------->接受到的的消息
是:" + msg);
}
}
package
com.itbooking.springbootrabbitmqfadeoutconsumbers.mq;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(bindings =@QueueBinding(
value = @Queue(value =
"${mq.config.queue.weixin}",autoDelete = "true"),
exchange = @Exchange(value =
"${mq.config.exchange}",type = ExchangeTypes.FANOUT)
))
public class WeixinReceiver {
@RabbitHandler
public void process(String msg) {
System.out.println("WEIXIN----------->接受到的的
消息是:" + msg);
}
测试
1:启动消费者工程即可
2:在 springboot-rabbitmq-fadout-producers 工程定义测试类:
}
18
19
20
package
com.itbooking.springbootrabbitmqfadoutproducers;
import
com.itbooking.springbootrabbitmqfadoutproducers.mq.Sen
der;
import org.junit.Test;
import org.junit.runner.RunWith;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class
SpringbootRabbitmqFadoutProducersApplicationTests {
@Autowired
private Sender sender;
@Test
public void contextLoads() throws
InterruptedException {
sender.sendMessage();
}
结果
06:SpringBoot-整合RabbitMQ发布订阅机
制-Direct
目标
使用springboot完成rabbitmq的direct模式
图解
步骤
1、配置依赖 pom.xml
}
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request