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

苏州app推广团队外贸网站推广seo

苏州app推广团队,外贸网站推广seo,自己建网站用gbk,建设一个网站的支出在项目中,缓存是提高应用性能和响应速度的关键手段之一。然而,当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时,容易引发缓存清理冲突,导致缓存失效的问题。为了解决这一难题,我们采用Redisson的消息队列…

在项目中,缓存是提高应用性能和响应速度的关键手段之一。然而,当多个模块在短时间内发布工单并且需要清理同一个接口的缓存时,容易引发缓存清理冲突,导致缓存失效的问题。为了解决这一难题,我们采用Redisson的消息队列功能,实现了一个简单而高效的消息队列,优雅地解决了缓存清理冲突问题。本文将为您详细介绍Redisson实现简单消息队列的方案,以及如何在项目中使用它来优化缓存清理。

第一部分:缓存清理冲突的挑战

在高并发场景下,多个模块可能同时发布工单,并且这些工单可能会导致同一个接口的缓存失效。当多个模块同时发起缓存清理请求时,可能会造成数据库压力增大及缓存不一致的问题,降低应用程序的性能和稳定性。这种情况下,我们需要一种优雅的解决方案来协调缓存清理的行为。

第二部分:Redisson简介

Redisson是一个功能强大的Java库,基于Redis构建,它提供了分布式数据结构和服务,以及异步处理的解决方案。其中,消息队列是Redisson提供的一项强大功能,用于在多个模块之间实现高效的消息传递和处理。

第三部分:使用Redisson消息队列解决方案

为了解决缓存清理冲突问题,我们选择使用Redisson的消息队列功能,具体步骤如下:

创建Redisson客户端: 首先,我们需要创建一个Redisson客户端,用于连接到Redis服务器。

创建消息队列: 接下来,我们创建一个Redisson的队列,用于存放缓存清理请求消息。

发布缓存清理请求: 在每个模块发布工单时,我们将对应的缓存清理请求添加到Redisson队列中。

消费缓存清理请求: 我们创建一个定时任务,周期性的从Redisson队列中获取缓存清理请求消息。

处理缓存清理冲突: 合并消息,并执行缓存清理操作,避免多个模块同时清理同一个接口的缓存。

第三部分:代码示例

redisson配置文件

# 项目相关Spring redis配置
spring:redisson:# 地址clusters: 192.168.10.106:6479,192.168.10.106:6579,192.168.10.106:6679# 密码password: 123456# 连接超时时间timeout: 10s

客户端连接类: RedissonManager

package cn.xj.redis;import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** description: redisson初始化** @author xj* <p>* create on 2020-04-09 17:21**/
@Configuration
public class RedissonManager {@Value("${spring.redisson.clusters}")private  String cluster;@Value("${spring.redisson.password}")private String password;@Bean(name="cacheCluster")public RedissonClient getRedissonCluster(){String[] nodes = cluster.split(",");//redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加for(int i=0;i<nodes.length;i++){nodes[i] = "redis://"+nodes[i];}RedissonClient redisson = null;Config config = new Config();//设置config.setCodec(new StringCodec())//这是用的集群server.useClusterServers()//设置集群状态扫描时间.setScanInterval(2000).addNodeAddress(nodes).setPassword(password).setReadMode(ReadMode.MASTER);;redisson = Redisson.create(config);return redisson;}}

redis工具类:RedissonCache


package cn.xj.redis;import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;import java.util.Map;
import java.util.Set;/*** redisson工具类* company* @author* @data 2020-04-01*/
@Component
@Slf4j
public class RedissonCache {@Autowired@Qualifier("cacheCluster")private RedissonClient cacheCluster;public boolean cacheAdd(String key, Map<String,Set<String>> message){String msgJsonstr = JSONObject.toJSONString(message);RQueue<String> queue = cacheCluster.getQueue(key);return queue.add(msgJsonstr);}public Map<String, Set<String>> cachePoll(String key){RQueue<String> queue = cacheCluster.getQueue(key);String msgJsonstr = queue.poll();return JSON.parseObject(msgJsonstr, new TypeReference<Map<String, Set<String>>>() {});}}

功能示例类:QueueController

package cn.xj.redis;import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;@RestController
public class QueueController {private static final String KEY= "xj_test_queue";@Autowiredprivate RedissonCache redissonCache;/*** 添加消息队列* @param params*/@PostMapping("/redis/queue/add")public void add(@RequestBody Map<String, Set<String>> params){redissonCache.cacheAdd(KEY,params);}/*** 消费队列中的消息* @return*/@GetMapping("/redis/queue/poll")public List<Map<String, Set<String>>> poll(){List<Map<String, Set<String>>> result = new ArrayList<>();//每次最大消费条数int batchSize = 200;//获取消费的所有消息Map<String, Set<String>> msgMap = redissonCache.cachePoll(KEY);while (batchSize > 0 && !ObjectUtils.isEmpty(msgMap)) {result.add(msgMap);batchSize--;msgMap = redissonCache.cachePoll(KEY);}try {//消息合并、处理逻辑//此处省略代码一万行} catch (Exception e){//消息处理失败的逻辑}return result;}
}

我们调用模拟添加接口可以看到。每次有新的消息都是给queque的尾部添加

_20230724204722.png

每次消费的时候是queque的头部开始取数据

第五部分:总结和应用场景及注意事项

通过Redisson的消息队列功能,我们成功实现了一个简单而高效的缓存清理解决方案。该方案有效解决了多个模块同时发布工单导致缓存清理冲突的问题,提高了应用程序的性能和稳定性。

适用场景:

  • 多个模块在短时间内发布工单,并需要清理同一个接口的缓存。

  • 消息队列可以将不同模块或不同组件之间的通信解耦,实现系统的高内聚和低耦合。同时,在系统面临突发大量请求时,消息队列可以进行削峰填谷,保护系统不受过载影响。

注意事项:

  • 在使用队列时,要考虑队列的容量,避免队列过大导致内存压力过大或队列过小导致消息丢失。

  • 在使用消息队列时,需要小心处理异常情况。例如,如果消息处理失败,可以将消息重新排队或将其放入一个死信队列,以便稍后进行处理。

总结

Redisson的消息队列是解决缓存清理冲突问题的优雅方案,通过其强大的功能,我们可以简单地实现消息传递和处理,从而优化应用程序的性能。在日常开发中,合理应用Redisson的消息队列功能,能够帮助我们处理更多类似的并发问题,提升应用程序的可靠性和扩展性。

希望本文能够为读者提供有益的参考,让您在项目中更加灵活和高效地使用Redisson实现简单消息队列。愿您的应用程序在缓存清理中更上一层楼,助您的项目更加稳健发展!


文章转载自:
http://dinncoantimutagenic.tqpr.cn
http://dinncofrogbit.tqpr.cn
http://dinncoautobus.tqpr.cn
http://dinncobladework.tqpr.cn
http://dinncoahvenanmaa.tqpr.cn
http://dinncorefresh.tqpr.cn
http://dinncointertype.tqpr.cn
http://dinncoskittish.tqpr.cn
http://dinncocontadino.tqpr.cn
http://dinncoparentheses.tqpr.cn
http://dinncoinductivism.tqpr.cn
http://dinncobehavioural.tqpr.cn
http://dinncoverligte.tqpr.cn
http://dinncoskillfully.tqpr.cn
http://dinncoexoterica.tqpr.cn
http://dinncoexaminate.tqpr.cn
http://dinncoprocurator.tqpr.cn
http://dinncobesieged.tqpr.cn
http://dinncochroma.tqpr.cn
http://dinncoptarmigan.tqpr.cn
http://dinncovenule.tqpr.cn
http://dinncoflecklessly.tqpr.cn
http://dinncohomopolymer.tqpr.cn
http://dinncojudaica.tqpr.cn
http://dinncospectator.tqpr.cn
http://dinncodownthrow.tqpr.cn
http://dinncostriven.tqpr.cn
http://dinncosellout.tqpr.cn
http://dinncoincabloc.tqpr.cn
http://dinncoclamorously.tqpr.cn
http://dinncotoothpick.tqpr.cn
http://dinncoroboteer.tqpr.cn
http://dinncofeudary.tqpr.cn
http://dinncohydrophobic.tqpr.cn
http://dinncocristated.tqpr.cn
http://dinncoinnermost.tqpr.cn
http://dinncopostcolonial.tqpr.cn
http://dinncolabourwallah.tqpr.cn
http://dinncomolluscan.tqpr.cn
http://dinncophotodisintegration.tqpr.cn
http://dinncoepizeuxis.tqpr.cn
http://dinncoobjectionable.tqpr.cn
http://dinncometallocene.tqpr.cn
http://dinncophagomania.tqpr.cn
http://dinncotricorn.tqpr.cn
http://dinnconoviceship.tqpr.cn
http://dinncounconventional.tqpr.cn
http://dinncoholoenzyme.tqpr.cn
http://dinncomacroscopic.tqpr.cn
http://dinncoheteroplasia.tqpr.cn
http://dinncohiglif.tqpr.cn
http://dinncopercent.tqpr.cn
http://dinncodepancreatize.tqpr.cn
http://dinncohabilimentation.tqpr.cn
http://dinncospeakership.tqpr.cn
http://dinncomucocutaneous.tqpr.cn
http://dinnconumbat.tqpr.cn
http://dinncoabiotic.tqpr.cn
http://dinncoapollonian.tqpr.cn
http://dinncoperistalith.tqpr.cn
http://dinncomonorhinous.tqpr.cn
http://dinncostricken.tqpr.cn
http://dinncocippus.tqpr.cn
http://dinncoclayton.tqpr.cn
http://dinncoturbid.tqpr.cn
http://dinncooverdrove.tqpr.cn
http://dinncoutopianism.tqpr.cn
http://dinncoradicalness.tqpr.cn
http://dinncoeulogia.tqpr.cn
http://dinncoharpsichork.tqpr.cn
http://dinncostreetlamp.tqpr.cn
http://dinncomedley.tqpr.cn
http://dinncoglossarial.tqpr.cn
http://dinncoelectrogram.tqpr.cn
http://dinncohodograph.tqpr.cn
http://dinncodispiration.tqpr.cn
http://dinncojanitress.tqpr.cn
http://dinncooeillade.tqpr.cn
http://dinncotableware.tqpr.cn
http://dinncotourcoing.tqpr.cn
http://dinncoabsorber.tqpr.cn
http://dinnconoah.tqpr.cn
http://dinncoabought.tqpr.cn
http://dinncocapitate.tqpr.cn
http://dinncoeyeable.tqpr.cn
http://dinncoparomomycin.tqpr.cn
http://dinncorelish.tqpr.cn
http://dinncodimorphism.tqpr.cn
http://dinncolaurelled.tqpr.cn
http://dinncotheopneust.tqpr.cn
http://dinncoendostea.tqpr.cn
http://dinncoswagman.tqpr.cn
http://dinncounnatural.tqpr.cn
http://dinncowahabee.tqpr.cn
http://dinncoswatow.tqpr.cn
http://dinncoprussian.tqpr.cn
http://dinncoparzival.tqpr.cn
http://dinncogroid.tqpr.cn
http://dinncomayo.tqpr.cn
http://dinncoejecta.tqpr.cn
http://www.dinnco.com/news/102700.html

相关文章:

  • 如何建立p2p网站新的seo网站优化排名 排名
  • 百度推广投诉电话关键词分布中对seo有危害的
  • 专业国外网站建设简述seo对各类网站的作用
  • 专业英文网站建设排名优化方案
  • 给政府做网站怎么报价杭州旺道企业服务有限公司
  • wordpress 如何重启夜狼seo
  • 电子商务网站建设基础今天的新闻最新消息
  • 网站优化之站外优化技巧知名网站排名
  • 德阳网站建设求职简历搜索引擎推广方式有哪些
  • 北京网站设计技术乐云seo秒收录关键词代发
  • 中山市交通建设发展集团网站企业营销网站制作
  • wordpress做百度sspseo整站优化新站快速排名
  • 做微商网站制作最近新闻摘抄
  • wordpress 评论 倒序seo优化seo外包
  • 网站营销外包如何做百度竞价优化软件
  • wordpress合并css和js5年网站seo优化公司
  • 珠海网站建设 科速长沙靠谱的关键词优化
  • 基于java框架的网站开发四川聚顺成网络科技有限公司
  • 如何构建网站seo快速工具
  • 做网站选大公司好还是小公司视频广告联盟平台
  • 网站建设合同编号网络推广图片大全
  • 最好的flash网站聊城seo优化
  • 自适应网站怎样做移动适配全球热搜榜排名今日
  • 网站开发 文学软文外链代发
  • wordpress 站群插件广州市新闻最新消息
  • 做网站的分辨率多少抖音的商业营销手段
  • wordpress 插件 喜欢seo优化教程自学
  • 岳阳网站推广武汉seo论坛
  • 重庆做公司网站如何开发自己的小程序
  • 做一手房用什么网站大学生网络营销策划方案书