zl程序教程

您现在的位置是:首页 >  其他

当前栏目

56分布式电商项目 - ActiveMQ 实现运营商后台与搜索服务的零耦合(一)

2023-09-11 14:15:41 时间

运用消息中间件ActiveMQ实现运营商后台与搜索服务的零耦合。运营商执行商品审后,向 ActiveMQ 发送消息(SKU 列表),搜索服务从 ActiveMQ 接收到消息并导入到 solr 索引库。

消息生产者(运营商后台)

1)pinyougou-manager-web 的 pom.xml,引入依赖:

<dependency> 
	<groupId>org.apache.activemq</groupId> 
	<artifactId>activemq-client</artifactId> 
	<version>5.13.4</version>
</dependency>

2)pinyougou-sellergoods-service 工程添加 spring-activemq.xml内容如下:
在这里插入图片描述
在这里插入图片描述
3)代码实现:

@Autowired
private Destination queueSolrDestination;//用于发送 solr 导入的消息

@Autowired
private JmsTemplate jmsTemplate;

@RequestMapping("/updateStatus")
public Result updateStatus(Long[] ids,String status){
	try {
		goodsService.updateStatus(ids, status);
		//按照 SPU ID 查询 SKU 列表(状态为 1)
		if(status.equals("1")){
		//审核通过List<TbItem> 
		itemList = goodsService.findItemListByGoodsIdandStatus(ids, status);//调用搜索接口实现数据批量导入
		if(itemList.size()>0){
		final String jsonString = JSON.toJSONString(itemList);
		jmsTemplate.send(queueSolrDestination, new MessageCreator() {
		@Override
		public Message createMessage(Session session) throws JMSException {
		return session.createTextMessage(jsonString);
		}
		});
		}else{
		System.out.println("没有明细数据");
		}
		 }return new Result(true, "修改状态成功"); 
		 } catch (Exception e) {
		 e.printStackTrace();return new Result(false, "修改状态失败");
		 }
		  }

消息消费者(搜索服务)

1)修改 pinyougou-search-service ,在 pom.xml 中添加 activemq 依赖

<dependency> 
	<groupId>org.apache.activemq</groupId> 
	<artifactId>activemq-client</artifactId> 
	<version>5.13.4</version>
</dependency>

2)添加 spring 配置文件 applicationContext-jms-consumer.xml

<!-- 真正可以产生 Connection 的 ConnectionFactory,由对应的 JMS 服务厂商提供--> 
<bean id="targetConnectionFactory"class="org.apache.activemq.ActiveMQConnectionFactory"> 
	<property name="brokerURL" value="tcp://192.168.25.135:61616"/>
</bean>
<!-- Spring 用于管理真正的 ConnectionFactory 的 ConnectionFactory -->
 <bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory">
	  <property name="targetConnectionFactory" ref="targetConnectionFactory"/>
  </bean>
  <!--这个是队列目的地,点对点的 文本信息--> 
  <bean id="queueSolrDestination"class="org.apache.activemq.command.ActiveMQQueue"> 
  	<constructor-arg value="pinyougou_queue_solr"/>
  </bean>
<!-- 消息监听容器 --> 
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
	  <property name="connectionFactory" ref="connectionFactory" />
	  <property name="destination" ref="queueSolrDestination" />		<property name="messageListener" ref="itemSearchListener" /
	 </bean>

3) 代码实现

@Component
public class ItemSearchListener implements MessageListener{

@Autowired
private ItemSearchService itemSearchService;

@Override
public void onMessage(Message message) {
	System.out.println("监听接收到消息...");
	try {
			TextMessage textMessage=(TextMessage)message;
			String text = textMessage.getText();
			List<TbItem> list = JSON.parseArray(text,TbItem.class);
			for(TbItem item:list){
			System.out.println(item.getId()+" "+item.getTitle());
			Map specMap= JSON.parseObject(item.getSpec());//将 spec 字段中的 json
			字符串转换为 map
			item.setSpecMap(specMap);//给带注解的字段赋值
		}
		itemSearchService.importList(list);//导入
		System.out.println("成功导入到索引库");
	} catch (Exception e) {
		e.printStackTrace();
	} 
} 
}