ConnectException:连接被拒绝(连接被拒绝)-通过Jedis连接到Redis时

时间:2019-06-18 06:32:37

标签: java spring-boot redis jedis spring-data-redis

我正在尝试通过Jedis客户端连接到Redis服务器,但是我在连接时遇到了异常和堆栈跟踪-

org.springframework.data.redis.RedisConnectionFailureException:无法获取Jedis连接。嵌套的异常是redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源             在org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:204)             在org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348)             在org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129)             在org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92)             在org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79)             在org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:194)             在org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)             在org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:91)             在org.springframework.data.redis.core.DefaultHashOperations.get(DefaultHashOperations.java:49)             在jdk.internal.reflect.GeneratedMethodAccessor122.invoke(未知来源)             在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)             在java.base / java.lang.reflect.Method.invoke(Method.java:564)             在org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)             在org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)             在org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)             在java.base / java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:514)             在java.base / java.util.concurrent.FutureTask.run(FutureTask.java:264)             在java.base / java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)             在java.base / java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1135)             在java.base / java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:635)             在java.base / java.lang.Thread.run(Thread.java:844)         引起原因:redis.clients.jedis.exceptions.JedisConnectionException:无法从池中获取资源             在redis.clients.util.Pool.getResource(Pool.java:53)             在redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)             在redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)             在org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194)             ...另外24个         引起原因:redis.clients.jedis.exceptions.JedisConnectionException:java.net.ConnectException:连接被拒绝(连接被拒绝)             在redis.clients.jedis.Connection.connect(Connection.java:207)             在redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)             在redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)             在redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)             在org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:888)             在org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:432)             在org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)             在redis.clients.util.Pool.getResource(Pool.java:49)             ...另外27个         引起原因:java.net.ConnectException:连接被拒绝(连接被拒绝)             在java.base / java.net.PlainSocketImpl.socketConnect(本机方法)             在java.base / java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)             在java.base / java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)             在java.base / java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)             在java.base / java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)             在java.base / java.net.Socket.connect(Socket.java:591)             在redis.clients.jedis.Connection.connect(Connection.java:184)

这是Jedis Pool的配置-

@Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWaitMillis(5000);
        jedisPoolConfig.setMaxIdle(8);
        jedisPoolConfig.setMinIdle(1);
        jedisPoolConfig.setMaxTotal(10);
        return jedisPoolConfig;
    }

@Bean
public JedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPoolConfig(jedisPoolConfig());
        factory.setShardInfo(shardInfo());
        factory.setHostName(serviceConfiguration.REDIS_HOST_NAME);
        factory.setPort(serviceConfiguration.REDIS_PORT);
        factory.setUsePool(true);
        return factory;
}

请提出建议,为解决此问题应该更改或尝试什么?

1 个答案:

答案 0 :(得分:0)

确保您还正确配置了连接工厂Bean

 @Bean
  public JedisConnectionFactory redisConnectionFactory() {

    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("server", 6379);
    return new JedisConnectionFactory(config);
  }