当前位置: 首页 > news >正文

潍坊知名网站建设服务商淘宝关键词优化

潍坊知名网站建设服务商,淘宝关键词优化,有网站吗给一个,郴州市做网站流程 1.声明exchange、queue、RoutingKey 2. 在hotel-admin中进行增删改&#xff08;SQL&#xff09;&#xff0c;完成消息发送 3. 在hotel-demo中完成消息监听&#xff0c;并更新elasticsearch数据 4. 测试同步 1.引入依赖 <!--amqp--> <dependency><groupId&…

流程

1.声明exchange、queue、RoutingKey
2. 在hotel-admin中进行增删改(SQL),完成消息发送
3. 在hotel-demo中完成消息监听,并更新elasticsearch数据
4. 测试同步

在这里插入图片描述

1.引入依赖

<!--amqp-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

我这里的mq是挂在了docker上,虚拟机地址是192.168.116.128。到时候这个根据自己的项目改就行

 spring: rabbitmq:host: 192.168.116.128 # 主机名port: 5672 # 端口virtual-host: / # 虚拟主机username: itcast # 用户名password: 123321 # 密码

2.声明交换机、队列和绑定关系

package cn.itcast.hotel.constants;public class MqConstants {/*** 交换机*/public final static String HOTEL_EXCHANGE = "hotel.topic";/*** 监听新增和修改的队列*/public final static String HOTEL_INSERT_QUEUE = "hotel.insert.queue";/*** 监听删除的队列*/public final static String HOTEL_DELETE_QUEUE = "hotel.delete.queue";/*** 新增或修改的RoutingKey*/public final static String HOTEL_INSERT_KEY = "hotel.insert";/*** 删除的RoutingKey*/public final static String HOTEL_DELETE_KEY = "hotel.delete";
}

在hotel-demo中,定义配置类,声明队列、交换机:

package cn.itcast.hotel.config;import cn.itcast.hotel.constants.MqConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MqConfig {// 定义交换机@Beanpublic TopicExchange topicExchange(){return new TopicExchange(MqConstants.HOTEL_EXCHANGE,true,false);}// 定义队列@Beanpublic Queue insertQueue(){return new Queue(MqConstants.HOTEL_INSERT_QUEUE,true);}@Beanpublic Queue deleteQueue(){return new Queue(MqConstants.HOTEL_DELETE_QUEUE,true);}// 定义绑定关系@Beanpublic Binding insertQueueBinding(){return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);}@Beanpublic Binding deleteQueueBinding(){return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);}
}

3.发送MQ消息

在hotel-admin中的增、删、改业务中分别发送MQ消息,具体怎么添加根据:

    // 新增@PostMappingpublic void saveHotel(){//数据库新增操作rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,hotel.getId());}// 更新@PutMapping()public void updateById(){//数据库修改操作rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,hotel.getId());}// 删除@DeleteMapping("/{id}")public void deleteById() {// 数据库删除操作rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);}

4.监听MQ消息

接收MQ消息

hotel-demo接收到MQ消息要做的事情包括:

  • 新增消息:根据传递的hotel的id查询hotel信息,然后新增一条数据到索引库
  • 删除消息:根据传递的hotel的id删除索引库中的一条数据

1)首先在hotel-demo的cn.itcast.hotel.service包下的IHotelService中新增新增、删除业务

void deleteById(Long id);void insertById(Long id);

2)给hotel-demo中的cn.itcast.hotel.service.impl包下的HotelService中实现业务:

@Override
public void deleteById(Long id) {try {// 1.准备RequestDeleteRequest request = new DeleteRequest("hotel", id.toString());// 2.发送请求client.delete(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}
}@Override
public void insertById(Long id) {try {// 0.根据id查询酒店数据Hotel hotel = getById(id);// 转换为文档类型HotelDoc hotelDoc = new HotelDoc(hotel);// 1.准备Request对象IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());// 2.准备Json文档request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);// 3.发送请求client.index(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}
}

编写监听类

package cn.itcast.hotel.mq;import cn.itcast.hotel.constants.MqConstants;
import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component
public class HotelListener {// 专门用于消息监听的类@Autowiredprivate IHotelService hotelService;@RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)public void listenHotelInsertOrUpdate(Long id){hotelService.insertById(id);}@RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)public void listenHotelDelete(Long id){hotelService.deleteById(id);}
}
http://www.yidumall.com/news/101505.html

相关文章:

  • 深圳网站建设模板乐云seo企业网站营销实现方式解读
  • 杭州做网站怎么收费外贸网站建设推广公司
  • 极速网站建设定制巨量引擎app
  • 网站建设推广一对一服务百度词条
  • wordpress如何安装模板文件夹西安seo服务公司排名
  • wordpress 图片库宁波seo企业推广
  • 网站做专题提升权重湘潭网站制作
  • 网站的线下推广怎么做的淘宝关键词排名优化技巧
  • 可以做go分析的网站哪个平台做推广效果好
  • 廉价网站建设秒收录关键词代发
  • 百度权重排名高的网站镇江网站关键字优化
  • 做外贸生意哪个网站好sem和seo是什么职业
  • 网站开发 顺德青岛seo用户体验
  • 南昌seo网站设计萧山区seo关键词排名
  • 朝阳网站建设 国展搜外网
  • 嘉兴建设局网站搜索引擎营销概念
  • 长春网络传媒做网站骗钱sem竞价是什么
  • 17网站一起做网店东莞网页优化公司
  • 杭州网站建设咨询蓝韵网络锦州seo推广
  • 如何给自己建设的网站设置登陆用户名和密码百度推广产品有哪些
  • 鞍山诺亚人才网网站seo方案模板
  • 河南疫情最新动态网站推广与优化方案
  • 国外做网站卖东西要什么条件百度搜索引擎推广步骤
  • 电子 网站模板域名大全查询
  • 火车头wordpress采集后定时发布官方进一步优化
  • 湖北做网站教程哪家好软文世界
  • 云南建设厅网站房地产开发资质贵阳网站建设公司
  • php做视频网站app拉新推广平台有哪些
  • 建设网站需要注意什么最新疫情最新数据
  • b2b网站怎么做关键词优化燕郊今日头条