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

在旅行社做网站运营品牌推广策划方案案例

在旅行社做网站运营,品牌推广策划方案案例,襄阳网站建设,如何实现输入域名访问网站首页文章目录 本地缓存更新方案探索1 背景2 方案探索2.1 初始化2.2 实时更新2.2.1 长轮询2.2.1.1 client2.2.2.2 server 本地缓存更新方案探索 1 背景 大家在工作中是否遇到过某些业务数据需要频繁使用,但是数据量不大的情况,一般就是几十条甚至几百条这种…

文章目录

  • 本地缓存更新方案探索
    • 1 背景
    • 2 方案探索
      • 2.1 初始化
      • 2.2 实时更新
        • 2.2.1 长轮询
          • 2.2.1.1 client
          • 2.2.2.2 server

本地缓存更新方案探索

1 背景

  • 大家在工作中是否遇到过某些业务数据需要频繁使用,但是数据量不大的情况,一般就是几十条甚至几百条这种。
  • 一般的解决方案就是业务维护数据,然后同步redis缓存,C端使用缓存的数据。但是这里不免会出现大key/热key的问题,另外还有缓存穿透、缓存击穿等问题。
  • 那么接下来我们一起探索一下如何解决上述问题吧。

2 方案探索

  • 首先我们评估数据量,发现这类数据一般只有百条左右。那么在技术选型上使用本地缓存无疑是最好的方案。现在应对C端场景基本选型的都是Caffeine。详见:https://blog.csdn.net/for62/article/details/147494533
  • 我们选择了本地缓存一方面可以抗大流量,做到无状态横向扩容。另一方面可以提高服务稳定性降低tp99。
  • 那么接下来我们就要设计缓存一致性的实现方案了,如何将redis中的数据近实时同步到本地缓存,C端只读本地缓存,可以降级读redis。
  • 这里我们参考长轮询实现配置中心的方案:https://mp.weixin.qq.com/s/YjvL0sUTGHxR3GJFqrP8qg。客户端长轮询监听服务端数据变更,感知到数据变更后更新本地缓存数据。设计图如下:
    在这里插入图片描述

2.1 初始化

  • 这里我们先假设刷新本地缓存的方法为:LocalCacheRefresher.refresh();
    public void refresh() {Caffeine<String, Object> cacheInfo = getLocalCacheInstance();String redisCacheKey = getRedisCacheKey();Set<String> keys = redisCache.hKeys(redisCacheKey);for (String key : keys) {String data = redisCache.hGet(redisCacheKey, key);cacheInfo.put(key, data);}}
  • 服务启动时数据加载
@Component
public class LocalCacheInitRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {LocalCacheRefresher.refresh();}}

2.2 实时更新

2.2.1 长轮询
  • 这里我们用长轮询的方案监听源数据的变更来刷新本地缓存。
2.2.1.1 client
@Slf4j
public class LongPollClient {private CloseableHttpClient httpClient;private RequestConfig requestConfig;public ConfigClient() {this.httpClient = HttpClientBuilder.create().build();// httpClient 客户端超时时间要大于长轮询约定的超时时间this.requestConfig = RequestConfig.custom().setSocketTimeout(6000).build();}public void longPolling(String url, String dataId) {String endpoint = url + "?dataId=" + dataId;HttpGet request = new HttpGet(endpoint);CloseableHttpResponse response = httpClient.execute(request);switch (response.getStatusLine().getStatusCode()) {case 200: {BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));StringBuilder result = new StringBuilder();String line;while ((line = rd.readLine()) != null) {result.append(line);}response.close();String configInfo = result.toString();log.info("dataId: [{}] changed, receive configInfo: {}", dataId, configInfo);longPolling(url, dataId);break;}// ② 304 响应码标记配置未变更case 304: {log.info("longPolling dataId: [{}] once finished, configInfo is unchanged, longPolling again", dataId);longPolling(url, dataId);break;}default: {throw new RuntimeException("unExcepted HTTP status code");}}}
}
2.2.2.2 server
@RestController
@Slf4j
@SpringBootApplication
public class LongPollServer {@Dataprivate static class AsyncTask {// 长轮询请求的上下文,包含请求和响应体private AsyncContext asyncContext;// 超时标记private boolean timeout;public AsyncTask(AsyncContext asyncContext, boolean timeout) {this.asyncContext = asyncContext;this.timeout = timeout;}}// guava 提供的多值 Map,一个 key 可以对应多个 valueprivate Multimap<String, AsyncTask> dataIdContext = Multimaps.synchronizedSetMultimap(HashMultimap.create());private ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("longPolling-timeout-checker-%d").build();private ScheduledExecutorService timeoutChecker = new ScheduledThreadPoolExecutor(1, threadFactory);// 配置监听接入点@RequestMapping("/listener")public void addListener(HttpServletRequest request, HttpServletResponse response) {String dataId = request.getParameter("dataId");// 开启异步AsyncContext asyncContext = request.startAsync(request, response);AsyncTask asyncTask = new AsyncTask(asyncContext, true);// 维护 dataId 和异步请求上下文的关联dataIdContext.put(dataId, asyncTask);// 启动定时器,30s 后写入 304 响应timeoutChecker.schedule(() -> {if (asyncTask.isTimeout()) {dataIdContext.remove(dataId, asyncTask);response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);asyncContext.complete();}}, 3000, TimeUnit.MILLISECONDS);}// 配置发布接入点@RequestMapping("/publishConfig")@SneakyThrowspublic String publishConfig(String dataId, String configInfo) {log.info("publish configInfo dataId: [{}], configInfo: {}", dataId, configInfo);Collection<AsyncTask> asyncTasks = dataIdContext.removeAll(dataId);for (AsyncTask asyncTask : asyncTasks) {asyncTask.setTimeout(false);HttpServletResponse response = (HttpServletResponse)asyncTask.getAsyncContext().getResponse();response.setStatus(HttpServletResponse.SC_OK);response.getWriter().println(configInfo);asyncTask.getAsyncContext().complete();}return "success";}public static void main(String[] args) {SpringApplication.run(ConfigServer.class, args);}}
http://www.dtcms.com/wzjs/173493.html

相关文章:

  • 专门做网站制作的公司月饼营销软文
  • 北京做网站推广多少钱百度高级搜索指令
  • 信息网站怎么做微信朋友圈广告代理
  • app导航网站源码软文范例300字
  • 店面设计餐饮风格南宁排名seo公司
  • 国外做兼职网站网站推广去哪家比较好
  • 拖拽式网站建设哪家专业百度热搜关键词
  • 17网站一起做网店的流程电商网络推广
  • 网站优化18600119496日本产品和韩国产品哪个好
  • 南和网站建设公司太原网站建设近一周热点新闻
  • 自己做外贸网站能接到单吗产品推广方法
  • 友联建设集团官方网站关键词如何排名在首页
  • 安徽省外经建设集团有限公司网站上海关键词推广公司
  • 广东网站开发哪家做个公司网站多少钱
  • 懒人图库旺道seo怎么优化网站
  • 上海建设网站制百度风云榜电视剧排行榜
  • 做网站 赚广告费网络营销推广方法有哪些
  • 可以做初中地理题的网站推广员网站
  • WordPress影视采集河南平价的seo整站优化定制
  • 服装微商城网站建设新品牌推广策略
  • 自己做网站用什么软件百度统计手机app
  • 做静态网站工资多少html期末大作业个人网站制作
  • 做网站义乌武汉关键词seo
  • 网页网站怎么做的新营销模式有哪些
  • ps个人网站建设优化师的工作内容
  • 教育网站建设公司网址域名注册信息查询
  • 曰本做爰网站电脑网络优化软件
  • 上海地区网站建设软文推广媒体
  • 招聘企业网站建设模块关键词优化公司费用多少
  • 微信ios分身版下载宁波seo优化服务