简体   繁体   English

Spring 数据 Redis 集群生菜连接设置

[英]Spring data Redis Cluster Lettuce Connection Settings

We are using spring data redis with lettuce, lettuce uses single connection but in web application it is better to use connection pool as per my assumption.我们将 spring 数据 redis 与生菜一起使用,生菜使用单连接,但在 web 应用程序中,根据我的假设,最好使用连接池。 Below is the code for java config下面是 java 配置的代码

 @Configuration
    @ComponentScan(basePackages = { "com.test.*" })
    public class AppConfig {
    
        @Bean
        public LettuceConnectionFactory getLettuceConnectionFactory() {
              List<String> clusterNodes = Arrays.asList("redis-cluster----0001-001.redis-cluster---.usw2.cache.amazonaws.com:6379", "redis-cluster----0001-002.redis-cluster---.usw2.cache.amazonaws.com:6379");
              final LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(new RedisClusterConfiguration(clusterNodes));
              lettuceConnectionFactory.setTimeout(10000);
              lettuceConnectionFactory.setUseSsl(true);
              lettuceConnectionFactory.setVerifyPeer(false);
              lettuceConnectionFactory.setStartTls(false);
              lettuceConnectionFactory.afterPropertiesSet();
              return lettuceConnectionFactory;
        }
        
        @Bean
        public RedisTemplate<String, Object> redisTemplate() {
              final RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
              redisTemplate.setKeySerializer(new StringRedisSerializer());                                           
              redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
              redisTemplate.setConnectionFactory(getLettuceConnectionFactory());
              return redisTemplate;
        }
    
    }

Since we are using Spring data redis 1.8.23 and Lettuce 4.5.0.final, we cannot use LettucePoolingClientConfiguration.由于我们使用 Spring 数据 redis 1.8.23 和 Lettuce 4.5.0.final,我们不能使用 LettucePoolingClientConfiguration。
Does making use of DefaultLettucePool for AWS Elastic Cache is good optoin, what is disadvantage of setting using setShareNativeConnection to false.为 AWS Elastic Cache 使用 DefaultLettucePool 是否是好的选择,使用 setShareNativeConnection 设置为 false 的缺点是什么。

Any other better option for to have connection pool.拥有连接池的任何其他更好的选择。

Does making use of DefaultLettucePool for AWS Elastic Cache is good option将 DefaultLettucePool 用于 AWS Elastic Cache 是否是一个不错的选择

From the functional perspective it works just fine the only problem is that it has been deprecated and thus, using it creates a technical debt.从功能的角度来看,它工作得很好,唯一的问题是它已被弃用,因此使用它会产生技术债务。

what is disadvantage of setting using setShareNativeConnection to false使用 setShareNativeConnection 设置为 false 的缺点是什么

It will increase the network I/O of your application since every operation will open and close a socket.它将增加应用程序的网络 I/O,因为每个操作都会打开和关闭一个套接字。 Keeping it to true ensures that multiple LettuceConnection objects could reuse the native connection.将其保持为 true 可确保多个 LettuceConnection 对象可以重用本机连接。

As an alternative, try to use Jedis as your engine instead of Lettuce.作为替代方案,尝试使用Jedis作为您的引擎,而不是 Lettuce。 I've had some great performance using this library and it supports connection pooling as well.使用这个库我有一些很棒的表现,它也支持连接池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM