几个微服务一启,门户、以及后台、还有ngnix、rabbitMQ、Linux虚拟机一开,我这内存已经吃不消了,不能够动图展示了,还要关掉一个文件上传微服务节省点内存进行MQ中间件的测试:

【干货】商城项目引入RabbitMQ
先看看eureka
【干货】商城项目引入RabbitMQ

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个、
【干货】商城项目引入RabbitMQ

        //发送到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.insert", "item.update"},
            value = @Queue(value = "ly.create.index.queue", durable = "true")
    )
    )
    public void listenCreate(Long id) {
        if (id == null) {
            return;
        }
         this.indexService.createIndex(id);
    }

    /**
     *     监听删除索引库
     * @param id spubo id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "ly.delete.index.queue", durable = "true"),
            exchange = @Exchange(
                    value = "ly.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            ),
            key = {"item.delete"}
    )

    )
    public void listenDelete(Long id) {
        if (id == null) {
            return;
        }
         this.indexService.deleteIndex(id);
    }
}

1.4、对监听结果处理,创建索引库或者删除索引库

   /**
     * 创建索引库
     * @param id  spuBo   id
     */
    public void createIndex(Long id) {

        Spu spu = this.goodsClient.querySpuById(id);
        //把spu 封装到spuBo中
        SpuBo spuBo = new SpuBo();
        BeanUtils.copyProperties(spu, spuBo);
        // 创建索引库
        this.goodsRepository.save(this.buildGoods(spuBo));

    }

    /**
     * 删除索引库
     * @param id  spuBo   id
     */
    public void deleteIndex(Long id) {
        this.goodsRepository.deleteById(id);
    }
2、 goods-page.8084

thymeleaf 页面静态化微服务,对商品详情进行了静态化处理,在商品更新变化时候,需要及时的更新静态化页面,这里同样引入MQ对item-service进行监听;
后台新增了一个 “嘿嘿” 商品,id=185
【干货】商城项目引入RabbitMQ
前端门户系统对 ES搜索微服务 引入MQ时候进行了测试,点击了商品详情,
生成的商品185.html静态页面;
【干货】商城项目引入RabbitMQ

1、依赖、配置、
2、创建监听器,

/**
 * @auther SyntacticSugar
 * @data 2018/12/25 0025下午 3:07
 */
@Component
public class ItemListener {
    @Autowired
    private FileService fileService;

    /**
     * 监听
     * @param id spuBo  id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "ly.create.page.queue",durable = "true"),
            key = {"item.insert", "item.update"},
            exchange = @Exchange(
                    value = "ly.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            )
    ))
    public  void  createPage(Long id) throws Exception {
        if (id == null) {
            return;
        }
        this.fileService.createHtml(id);
    }

    /**
     * 监听删除页面
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "ly.delete.page.queue",durable = "true"),
            key = {"item.delete"},
            exchange = @Exchange(
                    value = "ly.item.exchange",
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC
            )
    ))
    public  void  deletePage(Long id){
        if (id == null) {
            return;
        }
        this.fileService.deleteHtml(id);
    }
}

3、对监听到的结果进行处理
创建方法:删除页面、建新页面;

	/**
     * 异步创建html页面
     * @param id
     */
    public void syncCreateHtml(Long id){
        ThreadUtils.execute(() -> {
            try {
                createHtml(id);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 删除html页面
     * @param id
     */
    public void deleteHtml(Long id) {
        File temp = new File(this.destPath,id + ".html");
        try {
            // 虚拟机终止时候调用该  方法
            temp.deleteOnExit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

相关文章: