几个微服务一启,门户、以及后台、还有ngnix、rabbitMQ、Linux虚拟机一开,我这内存已经吃不消了,不能够动图展示了,还要关掉一个文件上传微服务节省点内存进行MQ中间件的测试:
先看看eureka
1、item-service
后台管理微服务item-service,对外提供API接口,后台对商品进行CUD操作时候;
1、search-service:8083 ES搜索微服务需要及时能够搜索的到,也就是在索引库中,根据数据id 对数据(商品)创建索引库;
2、search-service:8083 ES搜索微服务需要根据 后台删除时,删除索引库操作;
所以,需要对后台管理微服务item-service的CUD操作进行监听,并使用MQ把CUD操作的spuBo 的 id 发送到队列中,在search-service中进行接收,对搜索索引库进行操作;
1.1、具体实现如下:
1.1、 item-service、search-service、goods-page中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
spring:
rabbitmq:
host: 127.0.0.1
username: doudou
password: doudou
virtual-host: /hei
1.2、对item-service的service的CUD操作监听,并发送至rabbitMQ中、
CUD操作存在2个、
//发送到mq
this.amqpTemplate.convertAndSend("item.insert", spuBo.getId());
//mq
this.amqpTemplate.convertAndSend("item.update", spuBo.getId());
1.3、在搜索微服务search-service中监听器如下:
1、CU操作:更新、新增
绑定的交换机 : ly.item.exchange
路由:key = {“item.insert”, “item.update”},
queue队列:ly.create.index.queue
2、D操作:删除
绑定的交换机 : ly.item.exchange
路由:key = {“item.delete”},
queue队列:ly.delete.index.queue
/**
* @auther SyntacticSugar
* @data 2018/12/25 0025下午 2:31
*/
@Component
public class GoodsListener {
// 监听 item-service
@Autowired
private IndexService indexService;
/**
* 监听创建索引库
* @param id
*/
@RabbitListener(bindings = @QueueBinding(
exchange = @Exchange(
value = "ly.item.exchange",
type = ExchangeTypes.TOPIC,
ignoreDeclarationExceptions = "true"
),
key = {
"item.