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();
}
}
}
相关文章
- 阿里云elasticsearch index索引库同步到华为云搜索服务CSS(elasticsearch)demo
- (《机器学习》完整版系列)第7章 贝叶斯分类器——7.6 贝叶斯网(也称信念网)结构(网络结构也是“超参数”)、贝叶斯图络学习(两级搜索法)
- 全局 - 搜索升级 - 分词搜索
- 谷歌搜索技巧
- 【LOJ6254】最优卡组 堆(模拟搜索)
- 淘东电商项目(39) -商品搜索服务数据库设计
- 57分布式电商项目 - ActiveMQ 实现运营商后台与搜索服务的零耦合(二)
- 48分布式电商项目 - 搜索页与首页对接
- C#,数据检索算法之插值搜索(Interpolation Search)的源代码
- 深度优先搜索(DFS)相关习题
- 如何通过Dreamweaver批量对整个站点或目录进行代码搜索或部分全部替换
- HDU 4111 Alice and Bob (博弈+记忆化搜索)
- 《谷歌语义搜索》一2.4 你的业务与知识图谱
- Qt编写Onvif搜索及云台控制工具
- 《人工智能:计算Agent基础》——3.3 图搜索
- 《人工智能:计算Agent基础》——3.5 无信息搜索策略
- 搜索技术的历史
- [LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表
- [CareerCup] 4.6 Find Next Node in a BST 寻找二叉搜索树中下一个节点
- Apache Solr 实现去掉重复的搜索结果
- leetcode算法235.二叉搜索树的最近公共祖先