Jedis连接配置

Jedis连接配置

  • maven 配置(http://mvnrepository.com/ 查找你所需依赖)

	
	    redis.clients
	    jedis
	    2.9.0
	
  • maven -update project ..生产所需jar包

Jedis连接配置_第1张图片

 

 

 

  • 新建class JedisTest
package com.raychen.test;

import redis.clients.jedis.Jedis;

public class Jedistest {
	public static void main(String[] args) {
		Jedis jd = new Jedis("119.23.45.136",6379);
		jd.auth("123456");
		jd.set("name", "raychen");
		System.out.println(jd.get("name"));
		jd.close();
		
	}
}
  • 运行报错

Jedis连接配置_第2张图片

 

  • redis配置文件
vi /usr/local/redis/redis.conf

默认绑定本地 需要将其注释

Jedis连接配置_第3张图片

  • 配置完后 ,要重启下redis服务;

[root@localhost redis]#  ./bin/redis-cli shutdown

[root@localhost redis]#  ./bin/redis-server ./redis.conf
  • 继续报错(远程连接redis redis自我保护 拒绝访问)

Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: DENIED Redis is running in protected mode because protected mode is enabled,

解决方式:设置redis连接密码

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> config set requirepass 123456

127.0.0.1:6379> quit

[root@localhost redis]# ./bin/redis-cli

127.0.0.1:6379> auth 123456
  • jedispool连接
package com.raychen.test;

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

public class JedisPooltest {
	public static void main(String[] args) {
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(100);
		config.setMaxWaitMillis(10);
		JedisPool jd = new JedisPool(config,"119.23.45.136",6379);
		Jedis j = null;
		try {
			j = jd.getResource();

			j.auth("123456");
			j.set("name", "raychen");
			System.out.println(j.get("name"));
		} finally {
			if(j != null){
				j.close();
			}if(jd != null){
				jd.close();
			}
		}

		
		
	}
}

Jedis连接配置_第4张图片

你可能感兴趣的:(redis)