0
点赞
收藏
分享

微信扫一扫

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

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

前言

之前在做 ​​秒杀架构实践​​ 时有提到对 distributed-redis-tool 的一次小升级,但是没有细说。

其实主要原因是:

秒杀时我做压测:由于集成了这个限流组件,并发又比较大,所以导致连接、断开 Redis 非常频繁。 最终导致获取不了 Redis connection 的异常。

池化技术

这就是一个典型的对稀缺资源使用不善导致的。

何为稀缺资源?常见的有:

  • 线程
  • 数据库连接
  • 网络连接等

这些资源都有共同的特点:创建销毁成本较高

这里涉及到的 Redis 连接也属于该类资源。

我们希望将这些稀有资源管理起来放到一个池子里,当需要时就从中获取,用完就放回去,不够用时就等待(或返回)。

这样我们只需要初始化并维护好这个池子,就能避免频繁的创建、销毁这些资源(也有资源长期未使用需要缩容的情况)。

通常我们称这项姿势为池化技术,如常见的:

  • 线程池
  • 各种资源的连接池等。

为此我将使用到 Redis 的 ​​分布式锁​​​、​​分布式限流​​ 都升级为利用连接池来获取 Redis 的连接。

这里以分布式锁为例:

将使用的 api 修改为:

原有:

  1. @Configuration
  2. public class RedisLockConfig {

  3.    @Bean
  4.    public RedisLock build(){
  5.        //Need to get Redis connection
  6.        RedisLock redisLock = new RedisLock() ;
  7.        HostAndPort hostAndPort = new HostAndPort("127.0.0.1",7000) ;
  8.        JedisCluster jedisCluster = new JedisCluster(hostAndPort) ;
  9.        RedisLock redisLock = new RedisLock.Builder(jedisCluster)
  10.                .lockPrefix("lock_test")
  11.                .sleepTime(100)
  12.                .build();

  13.        return redisLock ;
  14.    }

  15. }

现在:

  1. @Configuration
  2. public class RedisLockConfig {
  3.    private Logger logger = LoggerFactory.getLogger(RedisLockConfig.class);


  4.    @Autowired
  5.    private JedisConnectionFactory jedisConnectionFactory;

  6.    @Bean
  7.    public RedisLock build() {
  8.        RedisLock redisLock = new RedisLock.Builder(jedisConnectionFactory,RedisToolsConstant.SINGLE)
  9.                .lockPrefix("lock_")
  10.                .sleepTime(100)
  11.                .build();

  12.        return redisLock;
  13.    }
  14. }

将以前的 Jedis 修改为 ​JedisConnectionFactory​,后续的 Redis 连接就可通过这个对象获取。

并且显示的传入使用 RedisCluster 还是单机的 Redis。

所以在真正操作 Redis 时需要修改:

  1.    public boolean tryLock(String key, String request) {
  2.        //get connection
  3.        Object connection = getConnection();
  4.        String result ;
  5.        if (connection instanceof Jedis){
  6.            result =  ((Jedis) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
  7.            ((Jedis) connection).close();
  8.        }else {
  9.            result = ((JedisCluster) connection).set(lockPrefix + key, request, SET_IF_NOT_EXIST, SET_WITH_EXPIRE_TIME, 10 * TIME);
  10.            try {
  11.                ((JedisCluster) connection).close();
  12.            } catch (IOException e) {
  13.                logger.error("IOException",e);
  14.            }
  15.        }

  16.        if (LOCK_MSG.equals(result)) {
  17.            return true;
  18.        } else {
  19.            return false;
  20.        }
  21.    }

  22.    //获取连接
  23.    private Object getConnection() {
  24.        Object connection ;
  25.        if (type == RedisToolsConstant.SINGLE){
  26.            RedisConnection redisConnection = jedisConnectionFactory.getConnection();
  27.            connection = redisConnection.getNativeConnection();
  28.        }else {
  29.            RedisClusterConnection clusterConnection = jedisConnectionFactory.getClusterConnection();
  30.            connection = clusterConnection.getNativeConnection() ;
  31.        }
  32.        return connection;
  33.    }    

最大的改变就是将原有操作 Redis 的对象( ​TextendsJedisCommands​)改为从连接池中获取。

由于使用了 ​org.springframework.data.redis.connection.jedis.JedisConnectionFactory​ 作为 Redis 连接池。

所以需要再使用时构件好这个对象:

  1.        JedisPoolConfig config = new JedisPoolConfig();
  2.        config.setMaxIdle(10);
  3.        config.setMaxTotal(300);
  4.        config.setMaxWaitMillis(10000);
  5.        config.setTestOnBorrow(true);
  6.        config.setTestOnReturn(true);

  7.        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
  8.        redisClusterConfiguration.addClusterNode(new RedisNode("10.19.13.51", 7000));

  9.        //单机
  10.        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(config);

  11.        //集群
  12.        //JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration) ;
  13.        jedisConnectionFactory.setHostName("47.98.194.60");
  14.        jedisConnectionFactory.setPort(6379);
  15.        jedisConnectionFactory.setPassword("");
  16.        jedisConnectionFactory.setTimeout(100000);
  17.        jedisConnectionFactory.afterPropertiesSet();
  18.        //jedisConnectionFactory.setShardInfo(new JedisShardInfo("47.98.194.60", 6379));
  19.        //JedisCluster jedisCluster = new JedisCluster(hostAndPort);

  20.        HostAndPort hostAndPort = new HostAndPort("10.19.13.51", 7000);
  21.        JedisCluster jedisCluster = new JedisCluster(hostAndPort);
  22.        redisLock = new RedisLock.Builder(jedisConnectionFactory, RedisToolsConstant.SINGLE)
  23.                .lockPrefix("lock_")
  24.                .sleepTime(100)
  25.                .build();

看起比较麻烦,需要构建对象的较多。

但整合 Spring 使用时就要清晰许多。

配合 Spring

Spring 很大的一个作用就是帮我们管理对象,所以像上文那些看似很复杂的对象都可以交由它来管理:

  1.   <!-- jedis 配置 -->
  2.    <bean id="JedispoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  3.        <property name="maxIdle" value="${redis.maxIdle}"/>
  4.        <property name="maxTotal" value="${redis.maxTotal}"/>
  5.        <property name="maxWaitMillis" value="${redis.maxWait}"/>
  6.        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
  7.        <property name="testOnReturn" value="${redis.testOnBorrow}"/>
  8.    </bean>
  9.    <!-- redis服务器中心 -->
  10.    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  11.        <property name="poolConfig" ref="JedispoolConfig"/>
  12.        <property name="port" value="${redis.port}"/>
  13.        <property name="hostName" value="${redis.host}"/>
  14.        <property name="password" value="${redis.password}"/>
  15.        <property name="timeout" value="${redis.timeout}"></property>
  16.    </bean>
  17.    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  18.        <property name="connectionFactory" ref="connectionFactory"/>
  19.        <property name="keySerializer">
  20.            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  21.        </property>
  22.        <property name="valueSerializer">
  23.            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  24.        </property>
  25.    </bean>

这个其实没多少好说的,就算是换成 SpringBoot 也是创建 ​JedispoolConfig,connectionFactory,redisTemplate​ 这些 bean 即可。

总结

换为连接池之后再进行压测自然没有出现获取不了 Redis 连接的异常(并发达到一定的量也会出错)说明更新是很有必要的。

推荐有用到该组件的朋友都升级下,也欢迎提出 Issues 和 PR。

项目地址:

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


举报

相关推荐

0 条评论