使用RabbitMQ
- 1、引入amqp场景;RabbitAutoConfiguration就会自动生效
- 2、给容器中自动配置了RabbitTemplate、AmqpAdmin、CachingConnectionFactory、RabbitNessagingTemplate
- 3、@EnabLeRabbit: @EnableXxXxx、cachingconnectionFactory
1.导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.启动MQ(启动类添加@EnableRabbit)
/**
*使用RabbitMQ
*1、引入amqp场景;RabbitAutoConfiguration就会自动生效*
*2、给容器中自动配置了*
RabbitTemplate、AmqpAdmin、CachingConnectionFactory、RabbitMessagingTemplate;
*
所有的属性都是spring.rabbitmq
*
@configurationProperties(prefix = "spring.rabbitmq")
*
pubLic class RabbitProperties
*
*3、给配置文件中配置spring.rabbitmq信息*4J @EnableRabbit : @EnableXxxXx;开启功能
*/
@EnableRabbit
@EnableDiscoveryClient // 注册
@SpringBootApplication
public class GulimallOrderApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallOrderApplication.class, args);
}
}
3.配置application.properties
spring.rabbitmq.host=192.168.56.10
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/
4.测试类,创建交换机、队列、绑定
/**
*1、如何创建Exchange、Queue、 Binding
1)、使用AmqpAdmin进行创建
*2、如何收发消息
*/
@Autowired
AmqpAdmin amqpAdmin;
@Test
void contextLoads() {
// 创建交换机
DirectExchange exchange = new DirectExchange("hello-java-exchange",true,false);
amqpAdmin.declareExchange(exchange);
log.info("Exchange[{}]创建成功","hello-java-exchange");
}
@Test
void createQueue() {
// 创建队列
Queue queue = new Queue("hello-java-queue",true,false,false);
amqpAdmin.declareQueue(queue);
log.info("Queue[{}]创建成功","hello-java-queue");
}
@Test
void createBinding() {
// 交换机绑定队列
// (String destination【目的地-队列】,
//DestinationType destinationType【目的地类型】,/l /String exchange【交换机】,
//String routingKey【路由键】,
// Map<String, object> arguments【自定义参数】)
//将exchange指定的交换机和destination目的地进行绑定,使用routingKey作为指定的路由键
Binding binding = new Binding(
"hello-java-queue",
Binding.DestinationType.QUEUE,
"hello-java-exchange",
"hello.java",
null);
amqpAdmin.declareBinding(binding);
log.info("[{}]绑定成功","hello.java");
}
}
5.测试如何向mq中发消息
5.1 发送字符串
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void sendMessageTest() {
// 发消息给交换机
String msg = "hello rabbitmq";
rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",msg);
log.info("消息发送完成{}",msg);
}
5.2 发送对象
1、发送消息,如果发送的消息是个对象,我们会使用序列化机制,将对象写出去。对象必须实现serializable
@Autowired
RabbitTemplate rabbitTemplate;
@Test
void sendMessageTest() {
// 发消息给交换机
OrderEntity orderEntity = new OrderEntity();
orderEntity.setCommentTime(new Date());
orderEntity.setDeliveryCompany("阿里巴巴");
orderEntity.setId(1L);
String msg = "hello rabbitmq";
rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",orderEntity);
log.info("消息发送完成{}",msg);
}
2、发送的对象消息,可以是json
- 自定义消息转换策略
package com.atguigu.gulimall.order.config;
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 MyRabbitConfig {