Redis远程连接不上的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feiyanaffection/article/details/82862816

看这位博主: https://blog.csdn.net/leisure_life/article/details/78460733

因为之前设置了Redis连接密码,所以连接时必须经过密码认证。
但是为什么会连接不成功,因为重新配置redis.conf之后,需要重新启动Redis才回生效。
在这里插入图片描述

有时候需要重新启动服务器,有时不需要。
如果遇到认证,使用,,命令auth "你的密码",看到OK说明成功连接。
在这里插入图片描述

下面能够正常使用:

package org.westos.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * *************************************
 * Copyright (c) 2018 feiyan.com
 * All rights reserved.
 * *************************************
 * *************************************
 *
 * @Author: think
 * @Project: WebServer
 * @Date: Created in 2018/9/27 9:10
 * @Since: JDK 1.8
 * @Version: 1.0
 * @Modified By:
 * @Description:
 */
public class JedisDemo {

    //Redis服务器IP
    private static String ADDR = "192.168.146.133";

    //Redis的端口号
    private static int PORT = 6379;

    //访问密码
    private static String AUTH = "root";

    //可用连接实例的最大数目,默认值为8;
    //如果赋值为-1,则表示不限制;如果pool已经分配了maxTotal个jedis实例,则此时pool的状态为exhausted(耗尽)。
    private static int MAX_ACTIVE = 1024;

    //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
    private static int MAX_IDLE = 200;

    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    /**
     * 初始化Redis连接池
     */
    static {

        try {

            JedisPoolConfig config = new JedisPoolConfig();

            config.setMaxTotal(MAX_ACTIVE);

            config.setMaxIdle(MAX_IDLE);

            config.setMaxWaitMillis(MAX_WAIT);

            config.setTestOnBorrow(TEST_ON_BORROW);

            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Jedis实例
     *
     * @return
     */
    public synchronized static Jedis getJedis() {

        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    
    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            jedisPool.destroy();
        }
    }

    // 获得连接
    @Test
    public void test() {
        Jedis jedis = getJedis();

        // 获得数据
        String username = jedis.set("username", "paradise");
        System.out.println(jedis.get("username"));

        // 存储
        jedis.set("addr", "西安");
        System.out.println(jedis.get("addr"));

        jedis.close();
    }
}

猜你喜欢

转载自blog.csdn.net/feiyanaffection/article/details/82862816