1、引用相关依赖,spring-messaging版本尽量高点,3点几的版本一般不支持RabbitMQ
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>4.3.11.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.7.4.RELEASE</version> </dependency>
2、新建消息体
public class RedbagSendMessage { private Long userId; private Long redbagId; private Integer num; //生成get和set方法 }
3、
消息生产者
@Service public class RabbitmqServiceImpl implements RabbitmqService { @Autowired private RabbitTemplate rabbitTemplate; @Override public void sendPlanPlusMqData(long userId) { RedbagSendMessage message = new RedbagSendMessage(); message.setUserId(2l); rabbitTemplate.convertAndSend(BUY_PLAN_PLUS_FANOUT_EXCHANGE, "", message); } }
4、消息消费类
public class RedbagSendConsumer implements MessageListener { private static final Logger LOGGER = LoggerFactory.getLogger(RedbagSendConsumer.class); @Autowired private UserManager userManager; public void onMessage(Message message) { //获取消息内容,转换成对象 String body = new String(message.getBody(), "utf-8"); RedbagSendMessage redbagInfo = JSON.parseObject(body, RedbagSendMessage.class); //处理业务相关 User user = userManager.findById(redbagInfo.getUserId()); } }
5、创建Rabbitmq地址配置文件rabbitmq.properties, host地址替换成生产或者测试地址即可
spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.virtual-host=/ spring.rabbitmq.username=guest spring.rabbitmq.password=guest
6、创建配置文件spring-rabbitmq.xml,配置队列、绑定交换机。我用的是订阅模式,一个生产者,可以配置多个消费者。更多详情可自行查找。mq也可以持久化,业务用不上,所以没加
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> <description>rabbitmq</description> <!-- 消息体 --> <bean id="classMapper" class="org.springframework.amqp.support.converter.DefaultClassMapper"> <property name="idClassMapping"> <map> <entry key="RedbagSendMessage"> <value>com.client.dto.mq.RedbagSendMessage</value> </entry> </map> </property> </bean> <bean id="rabbitmqJsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"> <property name="classMapper" ref="classMapper"></property> </bean> <rabbit:connection-factory id="rabbitmqConnectionFactory" host="${spring.rabbitmq.host}" username="${spring.rabbitmq.username}" password="${spring.rabbitmq.password}" port="${spring.rabbitmq.port}" virtual-host="${spring.rabbitmq.virtual-host}"/> <rabbit:admin connection-factory="rabbitmqConnectionFactory"/> <rabbit:template id="rabbitTemplate" connection-factory="rabbitmqConnectionFactory" message-converter="rabbitmqJsonMessageConverter"/> <!-- 消费队列 --> <rabbit:queue name="integral.mall.send.redbag.queue" durable="true" auto-delete="false" exclusive="false" /> <!-- 交换器 --> <rabbit:fanout-exchange id="integralExchangeRedbagFanoutExchange" name="integral:mall:send:redbag:exchange" durable="true" auto-delete="false" > <rabbit:bindings> <rabbit:binding queue="integral.mall.send.redbag.queue"></rabbit:binding> </rabbit:bindings> </rabbit:fanout-exchange> <!-- 消息处理类 --> <bean id="integralExchangeRedbagReceiver" class="com.caikee.hometimetask.consumer.RedbagSendConsumer" ></bean> <!-- 监听 --> <rabbit:listener-container connection-factory="rabbitmqConnectionFactory"> <rabbit:listener queues="integral.mall.send.redbag.queue" ref="integralExchangeRedbagReceiver" /> </rabbit:listener-container> </beans>