1 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2 配置application.yml文件
server:
port: 8021
spring:
#给项目来个名字
application:
name: rabbitmq-test
#配置rabbitMq 服务器
rabbitmq:
host: 127.0.0.1
port: 5672
username: need
password: 123456
#虚拟host 可以不设置,使用server默认host
virtual-host: /testhost
3 编写配置队列,交换机,绑定的类,这里以fanoutExchange为列,其他的交换机类似
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 1 创建三个队列 :fanout.A fanout.B fanout.C
* 2 定义交换机
* 3 将三个队列都绑定在交换机 fanoutExchange 上
* 因为是扇型交换机, 路由键无需配置,配置也不起作用
*/
@Configuration
public class FanoutRabbitConfig {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
//一般设置一下队列的持久化就好,其余两个就是默认false
@Bean
public Queue queueA() {
return new Queue("fanout.A",true);
}
@Bean
public Queue queueB() {
return new Queue("fanout.B",true);
}
@Bean
public Queue queueC() {
return new Queue("fanout.C",true);
}
@Bean
//DirectExchange Direct交换机
//TopicExchange Topic交换机
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange",true,false);
}
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
//将队列和交换机绑定, 并设置用于路由键 (Direct交换机,Topic交换机使用)
//return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
}
4 发送消息
import cn.huawei.rabbitmqtest1.pojo.User;
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqTest1ApplicationTests_1 {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@Test
void test_1() {
for (int i = 1; i <= 3; i++) {
User user = new User(i + "", "小明 " + i);
rabbitTemplate.convertAndSend("fanoutExchange", "", JSON.toJSONString(user));
}
}
@Test
public void test_2() {
for (int i = 1; i <= 3; i++) {
User user = new User(i + "", "张三 " + i);
rabbitTemplate.convertAndSend("TestTopicExchange", "news.chongqing", JSON.toJSONString(user));
}
}
@Test
public void test_3() {
for (int i = 1; i <= 3; i++) {
User user = new User(i + "", "陈四 " + i);
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", JSON.toJSONString(user));
}
}
}
5 接收消息
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 此代码添加 在消费者的项目中
*/
@Configuration
public class RabbitmqGlobalConfig {
/**
* 当发送的消息为pojo时,为报转换异常
* 解决方法:添加这个类进行序列化解析
* 会自动识别
* @param objectMapper json序列化实现类
* @return mq 消息序列化工具
*/
@Bean
public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
return new Jackson2JsonMessageConverter(objectMapper);
}
}
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class FanoutReceiverA {
/**
* 注意:后4个参数,需要生产者发送消息时加上,否则为报错,注解里添加 required=false 或 删除参数
*/
@RabbitListener(queues = "fanout.A")
public void process(Message message,
Channel channel,
@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
@Header(AmqpHeaders.MESSAGE_ID) String messageId,
@Header(AmqpHeaders.CONSUMER_TAG) String consumerTag,
CorrelationData correlationData) {
System.out.println("FanoutReceiverA消费者收到消息 : " +new String(message.getBody()));
}
}
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class FanoutReceiverB {
/**
* 注意:后4个参数,需要生产者发送消息时加上,否则为报错,注解里添加 required=false 或 删除参数
*/
@RabbitListener(queues = "fanout.B")
public void process(Message message,
Channel channel,
@Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag,
@Header(AmqpHeaders.MESSAGE_ID) String messageId,
@Header(AmqpHeaders.CONSUMER_TAG) String consumerTag,
CorrelationData correlationData) {
System.out.println("FanoutReceiverB消费者收到消息 : " +new String(message.getBody()));
}
}