版权声明: https://blog.csdn.net/qq_18298439/article/details/85061842
SpringBoot整合RabbitMQ
使用SpringBoot整合RabbitMQ非常简单,它极大程度的简化了开发成本,使用SpringBoot整合RabbitMQ需导入如下依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
1.创建配置文件 application.yml
spring:
rabbitmq:
virtual-host: /
username: roberto
password: roberto
addresses: 192.168.56.128:5672
2.创建消息生产者配置类
@Configuration
public class RabbitMQProducerConfig {
@Bean
public Exchange exchange() {
return new DirectExchange("roberto.order", true, false, new HashMap<>());
}
}
3.创建消息生产者启动类
@SpringBootApplication
public class ProducerApplication {
private static RabbitTemplate rabbitTemplate;
@Autowired
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
ProducerApplication.rabbitTemplate = rabbitTemplate;
}
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
MessageProperties messageProperties = new MessageProperties();
messageProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
messageProperties.setContentType("UTF-8");
Message message = new Message("订单信息".getBytes(), messageProperties);
rabbitTemplate.send("roberto.order", "add", message);
}
}
4.创建消息消费者配置类
@Configuration
public class RabbitMQConsumerConfig {
@Bean
public Exchange exchange() {
return new DirectExchange("roberto.order", true, false, new HashMap<>());
}
@Bean
public Queue queue() {
return new Queue("roberto.order.add", true, false, false, new HashMap<>());
}
@Bean
public Binding binding() {
return new Binding("roberto.order.add", Binding.DestinationType.QUEUE, "roberto.order", "add", new HashMap<>());
}
@Bean
public MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer messageListenerContainer = new SimpleMessageListenerContainer();
messageListenerContainer.setConnectionFactory(connectionFactory);
messageListenerContainer.setQueueNames("roberto.order.add");
messageListenerContainer.setConcurrentConsumers(5);
messageListenerContainer.setMaxConcurrentConsumers(10);
Map<String, Object> argumentMap = new HashMap();
messageListenerContainer.setConsumerArguments(argumentMap);
messageListenerContainer.setConsumerTagStrategy(new ConsumerTagStrategy() {
@Override
public String createConsumerTag(String s) {
return "RGP订单系统ADD处理逻辑消费者";
}
});
messageListenerContainer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
System.out.println(new String(message.getBody(), "UTF-8"));
System.out.println(message.getMessageProperties());
} catch (Exception e) {
e.printStackTrace();
}
}
});
return messageListenerContainer;
}
}
5.创建消息消费者启动类
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class);
}
}
6.依次启动消息消费者消息生产者,消息消费者控制台输出如下
订单信息
MessageProperties [headers={}, contentType=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=roberto.order, receivedRoutingKey=add, deliveryTag=1, consumerTag=RGP订单系统ADD处理逻辑消费者, consumerQueue=roberto.order.add]
我们发现当使用SpringBoot整合RabbitMQ时,我们不再需要管理RabbitAdmin,RabbitTemplate等