Rabbitmq 是一种在公司中很常见的消息队列,今天我简单写了一个demo ,消息发送,消息消费这个过程。
1,准备环境:
下载安装 rabbitmq (Mac) 网上百度一堆一堆,安装启动成功就OK
默认页面控制台 端口为15672 http://localhost:15672/
PS:页面端口 要和Mq 端口区别开 页面端口15672 ,Mq端口5672
2,新建项目springboot
2.1, 引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
2.2,springboot 项目利用注解配置 配置生产者Producer
package com.feijiang.washcar.configure;
import com.feijiang.washcar.BO.EventDetailMessages;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
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;
import java.util.HashMap;
import java.util.Map;
@Configuration
public class ProducerConfiguration {
@Bean
public Queue mailQueue() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x-dead-letter-exchange", "dead_letter_exchange");//设置死信交换机
map.put("x-dead-letter-routing-key", "mail_queue_fail");//设置死信routingKey
Queue queue = new Queue(EventDetailMessages.QUEUE,true, false, false, map);
return queue;
}
@Bean
public DirectExchange mailExchange() {
return new DirectExchange(EventDetailMessages.EXCHANGE, true, false);
}
@Bean
public Binding mailBinding() {
return BindingBuilder.bind(mailQueue()).to(mailExchange())
.with(EventDetailMessages.KEY);
}
// 创建链接
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("127.0.0.1:5672");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
}
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
return rabbitAdmin;
}
// 创建rabbitTemplate 消息模板类
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
template.setMessageConverter(integrationEventMessageConverter());
return template;
}
public MessageConverter integrationEventMessageConverter() {
Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
return messageConverter;
}
}
PS :注意坑来了,Queue,Exchange ,Binding 如果你没有手动配置进行关联,就需要spring容器启动的时候将他们加载进去,相当于Queue,Exchange ,Binding初始化。创建CachingConnectionFactory时 也容易将端口号写成 15672 (15672是控制台端口)
2.3 生产者类
@Slf4j
@RestController
public class HelloController {
@Autowired
private RabbitTemplate rabbitTemplate;
@GetMapping("/hello")
public void get() {
log.info("rabbitmq 发送消息 =========");
rabbitTemplate.setExchange(EventDetailMessages.EXCHANGE);
rabbitTemplate.setRoutingKey(EventDetailMessages.KEY);
rabbitTemplate.setDefaultReceiveQueue(EventDetailMessages.QUEUE);
Map<String,String> map = new HashMap<>();
map.put("name","小宇");
map.put("age","25");
rabbitTemplate.convertAndSend(map);
log.info("rabbitmq 发送消息成功 =========");
return;
}
}
现在启动服务,就可以生产消息了。
2.4 配置消费者
package com.feijiang.washcar.configure;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
@Configuration
public class RabbitListenerConfig implements RabbitListenerConfigurer {
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
registrar.setMessageHandlerMethodFactory(messageHandlerMethodFactory());
}
@Bean
public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory(){
DefaultMessageHandlerMethodFactory defaultMessageHandlerMethodFactory = new DefaultMessageHandlerMethodFactory();
defaultMessageHandlerMethodFactory.setMessageConverter(messageConverter());
return defaultMessageHandlerMethodFactory;
}
@Bean
public MappingJackson2MessageConverter messageConverter(){
return new MappingJackson2MessageConverter();
}
}
2.5 消费者类
package com.feijiang.washcar.schedule;
import com.feijiang.washcar.BO.EventDetailMessages;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DemoListener {
@RabbitListener(bindings = {
@QueueBinding(value = @Queue(value = EventDetailMessages.QUEUE, durable = "true")
, exchange = @Exchange(value = EventDetailMessages.EXCHANGE)
, key = EventDetailMessages.KEY)
})
public void handlerArc(Object o) {
log.info("handlerArc Object = {}", o.toString());
if (o == null) {
log.warn("消息内容为空!");
}
}
}
PS:主要就是@RabbitListener 注解 它指定哪个交换机绑定的队列 进行消费
这就是 简单的RabbitMq demo 未完待续