码迷,mamicode.com
首页 > 其他好文 > 详细

redis代码连接及遇到的问题

时间:2018-04-30 13:31:47      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:list   col   cmd   lld   resource   0.12   exception   from   native   

package com.study.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisUtil {

    private static final String host = "192.168.150.129";
    
    private static final int port = 6379;
    
    private static final String password = "1234";
    
    private static JedisPool jedisPool;
    
    
    public static Jedis getJedis() {
        
        JedisPoolConfig config = new JedisPoolConfig(); 
        config.setMaxTotal(20); 
        config.setMaxIdle(5); 
        config.setTestOnBorrow(false); 
        
        jedisPool = new JedisPool(config,host, port,3000,password);
        Jedis jedis = jedisPool.getResource(); 
        String code = jedis.auth(password); //如果没有配置密码验证,这注掉这句话
        //System.out.println("auth code :" + code);
        jedis.select(1);//选择第二个数据库演示
        return jedis;
    }
    
    
    
    public static void main(String[] args) {
        Jedis jedis = getJedis();
        jedis.close();
    }
    
}

redis连接时遇到的问题:Caused by: java.net.ConnectException: Connection refused: connect

技术分享图片
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at com.study.util.RedisUtil.main(RedisUtil.java:50)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 2 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
View Code

解决方法:

查看是否将默认只能本地访问redis改为所有IP均可以访问

bind 127.0.0.1 改为 bind 0.0.0.0 

修改redis的配置文件redis.conf 将bind改为0.0.0.0 

技术分享图片

遇到的问题二:Caused by: java.net.SocketTimeoutException: connect timed out

技术分享图片
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:53)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at com.study.util.RedisUtil.getJedis(RedisUtil.java:29)
    at com.study.util.RedisUtil.main(RedisUtil.java:39)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
    at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
    at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
    at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:889)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:433)
    at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:362)
    at redis.clients.util.Pool.getResource(Pool.java:49)
    ... 3 more
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at redis.clients.jedis.Connection.connect(Connection.java:184)
    ... 10 more
View Code

解决方法:

  • 1.关闭防火墙
  • systemctl stop firewalld.service

     技术分享图片

  • 2   .开启6379一个端口

  • firewall-cmd --zone=public --add-port=6379/tcp --permanent

    --permanent永久生效,没有此参数重启后失效

  • 重新载入
firewall-cmd --reload

技术分享图片

 

代码git地址:https://gitee.com/sjcq/redis.git

redis代码连接及遇到的问题

标签:list   col   cmd   lld   resource   0.12   exception   from   native   

原文地址:https://www.cnblogs.com/sjcq/p/8973176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!