26rabbitMq在项目中的使用

本文介绍了如何在项目中使用rabbitMQ作为生产者,当后台商品数据发生变化时,通过rabbitMQ通知前台系统和搜索系统进行数据更新。详细讲解了后台系统整合spring作为生产者的步骤,包括依赖导入、配置文件设置,以及在ItemService中发送消息的实现。同时,阐述了消费者的配置,包括依赖添加、消息处理类的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生产者,当有变化的数据时,需要通知前台系统,搜索系统(消费者)

一对多

1后台系统整合spring(生产者:生产者到交换机)

1-1》导入依赖需要配置在taotao-manage-service中

<dependency>

       <groupId>org.springframework.amqp</groupId>

       <artifactId>spring-rabbit</artifactId>

       <version>1.4.0.RELEASE</version>

 </dependency>


   1-2》添加配置文件

applicationContext-rabbitmq.xml:(因为作为生产者,只需要和交换机建立联系,所以没有消费者,队列)

<?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/rabbit
	http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

	<!-- 定义RabbitMQ的连接工厂 -->
	<rabbit:connection-factory id="connectionFactory"
		host="${rabbitmq.ip}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"
		virtual-host="${rabbitmq.vhost}" />
	<!-- 定义交换器,
		  auto-declare属性:自动声明,如果该交换机不存在则自动创建
	 -->
	<rabbit:topic-exchange name="TT_MANAGE_ITEM_EXCHANGE" auto-declare="true" durable="true"/>
	<!-- 定义Rabbit模板,指定连接工厂以及定义交换机(exchange) -->
	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="TT_MANAGE_ITEM_EXCHANGE" />
	<!-- MQ的管理,包括队列、交换器等 -->
	<rabbit:admin connection-factory="connectionFactory" />
</beans>

rabbitmq.properties

1-3》在后台系统ItemService中发送消息

在修改商品信息时,需要作为生产者发给交换机

 

================================================

2消费者(队列,消费者)

2-1》添加依赖

2-2》编写配置文件

 

applicationContext-rabblitmq.xml

<?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/rabbit
	http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

	<!-- 定义RabbitMQ的连接工厂 -->
	<rabbit:connection-factory id="connectionFactory"
		host="${rabbitmq.ip}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}"
		virtual-host="${rabbitmq.vhost}" />
	<!-- MQ的管理,包括队列、交换器等 -->
	<rabbit:admin connection-factory="connectionFactory" />
	<!-- 定义队列,自动声明 -->
	<rabbit:queue name="TT_WEB_ITEM_QUEUE" auto-declare="true" durable="true"/>
	
	<bean id="itemlistener" class="com.taotao.web.mq.ItemMqListener" />
	<!-- 队列监听 -->
	<rabbit:listener-container connection-factory="connectionFactory">
		<rabbit:listener ref="itemlistener" method="getMsg" queue-names="TT_WEB_ITEM_QUEUE" />
	</rabbit:listener-container>

</beans>

消费者(接受消息)MqItemMsgHandler 类下的getMsg

 

=============================================

应用:后台商品数据发生变化,要通知前台系统和搜索系统进行数据的更新

前台系统:

xml

类:

**
 * 监听后台系统发送的消息,并处理
 */
public class ItemMessageListener {
    @Autowired
    private HttpSolrServer server;
    @Autowired
    private ItemService itemService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    public void consume(String msg) {
        try {
            // 解析消息
            JsonNode jsonNode = MAPPER.readTree(msg);
            Long itemId = jsonNode.get("itemId").asLong();
            String type = jsonNode.get("type").asText();

            // 判断操作类型
            if (StringUtils.equals("insert", type) || StringUtils.equals("update", type)) {
                // 根据ID查询商品
                Item item = this.itemService.queryItemById(itemId);
                if(item != null){
                    // 新增数据到solr
                    this.server.addBean(item);
                    this.server.commit();
                }
            } else if (StringUtils.equals("delete", type)) {
                // 删除solr数据
                this.server.deleteById(itemId.toString());
                this.server.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值