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>
本系列博客源码GIT地址:https://github.com/RobertoHuang/RGP-RABBITMQ.git
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等
感言
至此本系列博客完结,由于博客篇幅有限所以无法面面俱到,也由于博主个人水平有限所以无法保证文章质量符合所有人要求在此希望大家多多包涵。本系列的每一篇文章都倾注了我的心血,每一个Demo我都用心测试,从0到1覆盖了大部分RabbitMQ常见案例,如果您觉得本系列博客帮助到了您,那就点个赞呗~,又或者您觉得这系列博客写的像屎一样,我也希望您能留下宝贵的建议来帮助我成长