电商平台首页广告缓存更新

首页广告缓存更新
1.1 实现思路
在这里插入图片描述

1.2 发送消息到MQ
在这里插入图片描述

1). 配置MQ中组件
@Configuration
public class RabbitMQConfig {
//定义交换机名称
public static final String GOODS_UP_EXCHANGE=“goods_up_exchange”;
public static final String GOODS_DOWN_EXCHANGE=“goods_down_exchange”;

//定义队列名称
public static final String AD_UPDATE_QUEUE="ad_update_queue";
public static final String SEARCH_ADD_QUEUE="search_add_queue";
public static final String SEARCH_DEL_QUEUE="search_del_queue";

//声明队列
@Bean
public Queue queue(){
    return new Queue(AD_UPDATE_QUEUE);
}

}

2). 数据监控服务中发送消息
for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
if (“position”.equals(column.getName())){
System.out.println(“发送最新的数据到MQ:”+column.getValue());
//发送消息 默认交换机 ----> web_index_lb
rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
}
}

1.3 消费者获取消息执行更新
1). pom.xml

org.springframework.boot
spring-boot-starter-amqp


com.squareup.okhttp3
okhttp
3.9.0

2). application.yml
rabbitmq:
host: 192.168.192.152

3). 消息监听类,消费者的逻辑处理
逻辑 : 接收消息 , 调用接口(lua脚本)
@Component
public class AdListener {

@RabbitListener(queues = "ad_update_queue")
public void receiveMessage(String message){
    System.out.println("接收到的消息为:"+message);

    //发起远程调用
    OkHttpClient okHttpClient = new OkHttpClient();
    String url = "http://192.168.192.152/ad_update?position="+message;
    Request request = new Request.Builder().url(url).build();
    Call call = okHttpClient.newCall(request);
    //设置回调
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            //请求失败
            e.printStackTrace();
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            //请求成功
            System.out.println("请求成功:"+response.message());
        }
    });
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值