安装
在docker下安装,参考:https://blog.csdn.net/dimandsun/article/details/103273606#rabbitmq_130
15672 是默认的管理界面端口
帐号密码:guest/guest
概念
queue 消息队列
- durable: true持久化。不持久化则mq重启就消失
- exclusive true:排外,连接关闭时,队列自动删除。同时只能有一个消费者访问该队列。
- autoDelete true自动删除。消费者为0时,删除队列。
- arguments 其他消息,查看https://blog.csdn.net/fsgsggd/article/details/81349553
exchange 交换机:
可通过交换机路由到队列。
类型:
- direct:单播模式:点对点
- fanout:广播模式。注册了的消息队列都会接受对应路由的消息
- topic:匹配模式,
*匹配一个单词
#匹配0个或多个单词
springboot中的使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
完整配置
spring:
rabbitmq:
host: 127.0.0.1 #ip
port: 5672 #端口
username: guest #账号
password: guest #密码
virtualHost: #链接的虚拟主机
addresses: 127.0.0.1:5672 #多个以逗号分隔,与host功能一样。
requestedHeartbeat: 60 #指定心跳超时,单位秒,0为不指定;默认60s
publisherConfirms: true #发布确认机制是否启用
publisherReturns: #发布返回是否启用
connectionTimeout: #链接超时。单位ms。0表示无穷大不超时
### ssl相关
ssl:
enabled: #是否支持ssl
keyStore: #指定持有SSL certificate的key store的路径
keyStoreType: #key store类型 默认PKCS12
keyStorePassword: #指定访问key store的密码
trustStore: #指定持有SSL certificates的Trust store
trustStoreType: #默认JKS
trustStorePassword: #访问密码
algorithm: #ssl使用的算法,例如,TLSv1.1
verifyHostname: #是否开启hostname验证
### cache相关
cache:
channel:
size: #缓存中保持的channel数量
checkoutTimeout: #当缓存数量被设置时,从缓存中获取一个channel的超时时间,单位毫秒;如果为0,则总是创建一个新channel
connection:
mode: #连接工厂缓存模式:CHANNEL 和 CONNECTION
size: #缓存的连接数,只有是CONNECTION模式时生效
### listener
listener:
type: #两种类型,SIMPLE,DIRECT
## simple类型
simple:
concurrency: #最小消费者数量
maxConcurrency: #最大的消费者数量
transactionSize: #指定一个事务处理的消息数量,最好是小于等于prefetch的数量
missingQueuesFatal: #是否停止容器当容器中的队列不可用
## 与direct相同配置部分
autoStartup: #是否自动启动容器
acknowledgeMode: #表示消息确认方式,其有三种配置方式,分别是none、manual和auto;默认auto
prefetch: #指定一个请求能处理多少个消息,如果有事务的话,必须大于等于transaction数量
defaultRequeueRejected: #决定被拒绝的消息是否重新入队;默认是true(与参数acknowledge-mode有关系)
idleEventInterval: #container events发布频率,单位ms
##重试机制
retry:
stateless: #有无状态
enabled: #是否开启
maxAttempts: #最大重试次数,默认3
initialInterval: #重试间隔
multiplier: #对于上一次重试的乘数
maxInterval: #最大重试时间间隔
direct:
consumersPerQueue: #每个队列消费者数量
missingQueuesFatal:
#...其余配置看上方公共配置
## template相关
template:
mandatory: #是否启用强制信息;默认false
receiveTimeout: #`receive()`接收方法超时时间
replyTimeout: #`sendAndReceive()`超时时间
exchange: #默认的交换机
routingKey: #默认的路由
defaultReceiveQueue: #默认的接收队列
## retry重试相关
retry:
enabled: #是否开启
maxAttempts: #最大重试次数
initialInterval: #重试间隔
multiplier: #失败间隔乘数
maxInterval: #最大间隔
常用配置
spring:
rabbitmq:
host: amqp-cn-st21yo0nk00i.mq-amqp.cn-shenzhen-429403-a-internal.aliyuncs.com
port: 5672
username: MjphbXFwLWNuLXN0MjF5bzBuazAwaTpMVEFJNEc1dVR6Y2h3enJoV0dGN0JIZzE=
password: QkQzRDVCRUNFQkNFOTUwNkE4QjI3MjQ3NzlGNDZFOTc4N0ZDNzhGMDoxNjA4MjUyODQ5MjI0
virtualHost: pos_server
connection-timeout: 3000ms #mq三秒超时
publisher-returns: true # 过时写法: publisher-confirms: true
publisher-confirm-type: correlated
listener:
type: simple
simple:
acknowledge-mode: manual #手动确认
prefetch: 1 #限制每次发送一条数据。
concurrency: 1 #同一个队列启动几个消费者
max-concurrency: 1 #启动消费者最大数量
retry:
enabled: true #开启重试
@EnableRabbit //开启基于注解的RabbitMQ模式
@SpringBootApplication
public class PointApplication {
public static void main(String[] args) {
SpringApplication.run(PointApplication.class, args);
}
}
AmqpAdmin
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
//创建交换机,默认持久化
amqpAdmin.declareExchange(new DirectExchange("czy_direct_exchange"));
//创建消息队列
amqpAdmin.declareQueue(new Queue("czy_queue_ha"));
//交换机与消息队列绑定
amqpAdmin.declareBinding(new Binding("czy_queue_ha", Binding.DestinationType.QUEUE,"czy_direct_exchange","czy.ha",null));
}
监听消息
@Service
public class PointTagServiceImpl{
@RabbitListener(queues = "czy_queue_ha")
public void receiveMQ(Message message){
System.out.println("收到队列[czy_queue_ha]的消息:"+message.getBody());
System.out.println(message.getMessageProperties());
}
}
发送消息
//Message需要自己构造一个;定义消息体内容和消息头
//rabbitTemplate.send(exchage,routeKey,message);
//object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitmq;
//rabbitTemplate.convertAndSend(exchage,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","atguigu.news",new Book("西游记","吴承恩"));
接收消息
//获取消息体
Object o = rabbitTemplate.receiveAndConvert("atguigu.news");