alibaba / jetcache

JetCache is a Java cache framework.
Apache License 2.0
5.03k stars 1.05k forks source link

基于jetcache-lettuce做redis操作,连接不释放出现io.lettuce.core.RedisConnectionException: Unable to connect #320

Closed yujun32 closed 5 years ago

yujun32 commented 5 years ago

基于SpringBoot 2.1.1

gradle中jetcache: compile group: 'com.alicp.jetcache', name: 'jetcache-starter-redis', version: '2.6.0.M2' compile group: 'com.alicp.jetcache', name: 'jetcache-starter-redis-lettuce', version: '2.6.0.M2'

application.yml:

jetcache:
    statIntervalMinutes: 0 # 统计间隔,0表示不统计
    areaInCacheName: false
    hiddenPackages: com
    local:
        default:
            type: caffeine
            limit: 100
            keyConvertor: fastjson
            expireAfterWriteInMillis: 60000
    remote:
        default:
            type: redis.lettuce
            keyConvertor: fastjson
            valueEncoder: java
            valueDecoder: java
            poolConfig:
                minIdle: 5
                maxIdle: 20
                maxTotal: 50
            uri:
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx
        ghost:
            type: redis.lettuce
            keyConvertor: fastjson
            valueEncoder: java
            valueDecoder: java
            poolConfig:
                minIdle: 5
                maxIdle: 20
                maxTotal: 50
            uri:
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx
                - redis://xxx

注入Redis Config配置类:

@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {

    @Bean(name = "defaultClient")
    @DependsOn(RedisLettuceAutoConfiguration.AUTO_INIT_BEAN_NAME)
    public LettuceFactory defaultClient() {
        return new LettuceFactory("remote.default", RedisClusterClient.class);
    }

    @Bean(name = "ghostClient")
    @DependsOn(RedisLettuceAutoConfiguration.AUTO_INIT_BEAN_NAME)
    public LettuceFactory ghostClient() {
        return new LettuceFactory("remote.ghost", RedisClusterClient.class);
    }

    /**
     * 解决 Idea "Could not autowire" 告警。
     *
     * @return
     */
    @Bean
    public RedisClusterClient redisClusterClient() {
        return null;
    }

具体service应用:

@Service
public class FaqService {
    @Autowired
    private RedisClusterClient defaultClient;

    public String genEpk() {
        String epk = "aaaa";
        String key = "bbbb";
        RedisAdvancedClusterCommands<String, String> sync = defaultClient.connect().sync();
        sync.set(key, epk);
        sync.expire(key, 6000);
        return epk;
    }

然后redis命令行输入info查看,每次请求,connected_clients都会+1,直至io.lettuce.core.RedisConnectionException: Unable to connect

# Clients
connected_clients:39

我是参考 https://github.com/alibaba/jetcache/wiki/RedisWithLettuce_CN 这个这个文档写的,麻烦前辈们帮忙看下是不是我使用方式有问题。非常感谢。 image

areyouok commented 5 years ago

RedisAdvancedClusterCommands<String, String> sync = defaultClient.connect().sync();

这一行有问题,你的connection没关闭。 lettuce是单连接多路复用的,一直用一个就好。

yujun32 commented 5 years ago

RedisAdvancedClusterCommands<String, String> sync = defaultClient.connect().sync();

这一行有问题,你的connection没关闭。 lettuce是单连接多路复用的,一直用一个就好。

非常感谢大大,刚刚ab测试了下,发现隔段时间会对所有connect 进行 Reconnecting

是自己太急,没看到注释描述: image

再次感谢!