【干货】商城项目引入RabbitMQ

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

2
先看看eureka
3

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个、
3

        //发送到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.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值