1、application.yml
spring:
rabbitmq:
host: 192.168.78.169
port: 5672
username: guest
password: guest
virtualHost: /
#publisherConfirms: true # 开启发送确认
publisherReturns: true # 开启发送失败退回
template:
mandatory: true
listener:
type: simple # 容器类型,simple|direct,simple表示监听器可以有多个消费者(起多个线程);direct直接使用当前调用线程,只有一个消费者
simple: # simple监听器配置
acknowledgeMode: manual # 开启消费者手动确认,值:none|auto|manual
prefetch: 3 # 未确认消息数量
concurrency: 3 # 最小的消费者数量
maxConcurrency: 8 # 最大的消费者数量
注意:可以查看RabbitProperties源码可知还有哪些属性可以配置!
2、RabbitmqConfig.java
package com.java.ashare.rabbitmq.sb;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfig {
/**
* 声明交换器
* DirectExchange: direct类型交换器,路由key和绑定key完全匹配
* TopicExchange: topic类型交换器,路由key和绑定key模糊匹配,支持通配符: *表示一个词,#表示零个或多个词,key以.分隔
* FanoutExchange: fanout类型交换器,忽略路由key,发送到所有绑定到此交换器的队列
* HeadersExchange: headers类型交换器,匹配headers的key-value,用的少
*/
@Bean
public DirectExchange directExchange() {
//new DirectExchange(name, durable, autoDelete, arguments);
return new DirectExchange("EXCHANGE_1");
}
/*@Bean
public TopicExchange topicExchange() {
return new TopicExchange("EXCHANGE_2");
}*/
/*@Bean
public DirectExchange directExchange() {
return new DirectExchange("EXCHANGE_3");
}
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("EXCHANGE_4");
}*/
/**
* 声明队列
*/
@Bean
public Queue queue1() {
//new Queue(name, durable, exclusive, autoDelete, arguments);
return new Queue("QUEUE_1");
}
/*@Bean
public Queue queue2() {
return new Queue("QUEUE_2");