日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

分布式工具的一次小升级⏫

發(fā)布時(shí)間:2025/6/16 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分布式工具的一次小升级⏫ 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

之前在做 秒殺架構(gòu)實(shí)踐 時(shí)有提到對(duì) distributed-redis-tool 的一次小升級(jí),但是沒(méi)有細(xì)說(shuō)。

其實(shí)主要原因是:

秒殺時(shí)我做壓測(cè):由于集成了這個(gè)限流組件,并發(fā)又比較大,所以導(dǎo)致連接、斷開(kāi) Redis 非常頻繁。
最終導(dǎo)致獲取不了 Redis connection 的異常。

池化技術(shù)

這就是一個(gè)典型的對(duì)稀缺資源使用不善導(dǎo)致的。

何為稀缺資源?常見(jiàn)的有:

  • 線程
  • 數(shù)據(jù)庫(kù)連接
  • 網(wǎng)絡(luò)連接等

這些資源都有共同的特點(diǎn):創(chuàng)建銷(xiāo)毀成本較高

這里涉及到的 Redis 連接也屬于該類(lèi)資源。

我們希望將這些稀有資源管理起來(lái)放到一個(gè)池子里,當(dāng)需要時(shí)就從中獲取,用完就放回去,不夠用時(shí)就等待(或返回)。

這樣我們只需要初始化并維護(hù)好這個(gè)池子,就能避免頻繁的創(chuàng)建、銷(xiāo)毀這些資源(也有資源長(zhǎng)期未使用需要縮容的情況)。

通常我們稱(chēng)這項(xiàng)姿勢(shì)為池化技術(shù),如常見(jiàn)的:

  • 線程池
  • 各種資源的連接池等。

為此我將使用到 Redis 的 分布式鎖、分布式限流 都升級(jí)為利用連接池來(lái)獲取 Redis 的連接。

這里以分布式鎖為例:

將使用的 api 修改為:

原有:

@Configuration public class RedisLockConfig {@Beanpublic RedisLock build(){//Need to get Redis connection RedisLock redisLock = new RedisLock() ;HostAndPort hostAndPort = new HostAndPort("127.0.0.1",7000) ;JedisCluster jedisCluster = new JedisCluster(hostAndPort) ;RedisLock redisLock = new RedisLock.Builder(jedisCluster).lockPrefix("lock_test").sleepTime(100).build();return redisLock ;}}

現(xiàn)在:

@Configuration public class RedisLockConfig {private Logger logger = LoggerFactory.getLogger(RedisLockConfig.class);@Autowiredprivate JedisConnectionFactory jedisConnectionFactory;@Beanpublic RedisLock build() {RedisLock redisLock = new RedisLock.Builder(jedisConnectionFactory,RedisToolsConstant.SINGLE).lockPrefix("lock_").sleepTime(100).build();return redisLock;} }

將以前的 Jedis 修改為 JedisConnectionFactory,后續(xù)的 Redis 連接就可通過(guò)這個(gè)對(duì)象獲取。

并且顯示的傳入使用 RedisCluster 還是單機(jī)的 Redis。

所以在真正操作 Redis 時(shí)需要修改:

public boolean tryLock(String key, String request) {//get connectionObject connection = getConnection();String result ;if (connection instanceof Jedis){result = ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);((Jedis) connection).close();}else {result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);try {((JedisCluster) connection).close();} catch (IOException e) {logger.error("IOException",e);}}if (LOCK_MSG.equals(result)) {return true;} else {return false;}}//獲取連接private Object getConnection() {Object connection ;if (type == RedisToolsConstant.SINGLE){RedisConnection redisConnection = jedisConnectionFactory.getConnection();connection = redisConnection.getNativeConnection();}else {RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();connection = clusterConnection.getNativeConnection() ;}return connection;}

最大的改變就是將原有操作 Redis 的對(duì)象(T extends JedisCommands)改為從連接池中獲取。

由于使用了 org.springframework.data.redis.connection.jedis.JedisConnectionFactory 作為 Redis 連接池。

所以需要再使用時(shí)構(gòu)件好這個(gè)對(duì)象:

JedisPoolConfig config = new JedisPoolConfig();config.setMaxIdle(10);config.setMaxTotal(300);config.setMaxWaitMillis(10000);config.setTestOnBorrow(true);config.setTestOnReturn(true);RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();redisClusterConfiguration.addClusterNode(new RedisNode("10.19.13.51", 7000));//單機(jī)JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);//集群//JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration) ;jedisConnectionFactory.setHostName("47.98.194.60");jedisConnectionFactory.setPort(6379);jedisConnectionFactory.setPassword("");jedisConnectionFactory.setTimeout(100000);jedisConnectionFactory.afterPropertiesSet();//jedisConnectionFactory.setShardInfo(new JedisShardInfo("47.98.194.60", 6379));//JedisCluster jedisCluster = new JedisCluster(hostAndPort);HostAndPort hostAndPort = new HostAndPort("10.19.13.51", 7000);JedisCluster jedisCluster = new JedisCluster(hostAndPort);redisLock = new RedisLock.Builder(jedisConnectionFactory, RedisToolsConstant.SINGLE).lockPrefix("lock_").sleepTime(100).build();

看起比較麻煩,需要構(gòu)建對(duì)象的較多。

但整合 Spring 使用時(shí)就要清晰許多。

配合 Spring

Spring 很大的一個(gè)作用就是幫我們管理對(duì)象,所以像上文那些看似很復(fù)雜的對(duì)象都可以交由它來(lái)管理:

<!-- jedis 配置 --><bean id="JedispoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="${redis.maxIdle}"/><property name="maxTotal" value="${redis.maxTotal}"/><property name="maxWaitMillis" value="${redis.maxWait}"/><property name="testOnBorrow" value="${redis.testOnBorrow}"/><property name="testOnReturn" value="${redis.testOnBorrow}"/></bean><!-- redis服務(wù)器中心 --><bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="poolConfig" ref="JedispoolConfig"/><property name="port" value="${redis.port}"/><property name="hostName" value="${redis.host}"/><property name="password" value="${redis.password}"/><property name="timeout" value="${redis.timeout}"></property></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="connectionFactory"/><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property></bean>

這個(gè)其實(shí)沒(méi)多少好說(shuō)的,就算是換成 SpringBoot 也是創(chuàng)建 JedispoolConfig,connectionFactory,redisTemplate 這些 bean 即可。

總結(jié)

換為連接池之后再進(jìn)行壓測(cè)自然沒(méi)有出現(xiàn)獲取不了 Redis 連接的異常(并發(fā)達(dá)到一定的量也會(huì)出錯(cuò))說(shuō)明更新是很有必要的。

推薦有用到該組件的朋友都升級(jí)下,也歡迎提出 Issues 和 PR。

項(xiàng)目地址:

https://github.com/crossoverJie/distributed-redis-tool

總結(jié)

以上是生活随笔為你收集整理的分布式工具的一次小升级⏫的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。