1、Jedis客户端出现的异常信息
Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException:
DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions:
1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent.
2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server.
3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
2、对于异常信息的解决方法
2.1 在redis服务器,设置访问密码
[root@itcast01 redis-3.2.1]# redis-cli
127.0.0.1:6379> config set requirepass 123456
OK
127.0.0.1:6379>
2.2 在Jedis客户端,加入访问密码
// 设置连接密码
jedis.auth("123456");
2.3 使用密码登录redis客户端
$ ./redis-cli -h 127.0.0.1 -p 6379 -a 123456
3、Jedis连接Redis的简单操作
import redis.clients.jedis.Jedis;
public class JedisDemo {
public static void main(String[] args) {
// 主机地址
String host = "192.168.2.111";
// 构造jedis对象
Jedis jedis = new Jedis(host, 6379);
// 设置连接密码
jedis.auth("123456");
// 向redis中添加数据
jedis.set("mytest", "123");
// 从redis中读取数据
String value = jedis.get("mytest");
System.out.println(value);
// 关闭连接
jedis.close();
}
}
4、JedisPool的配置和使用
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisPoolDemo {
public static void main(String[] args) {
// 主机地址
String host = "192.168.2.105";
// 构建连接池配置信息
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 设置最大连接数
jedisPoolConfig.setMaxTotal(50);
// 超时时间
int timeout = 10000;
// 授权密码
String password = "123456";
// 构建连接池
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, 6379,
timeout, password);
// 从连接池中获取连接
Jedis jedis = jedisPool.getResource();
// 设置访问密码
// 读取数据
System.out.println(jedis.get("mytest"));
// 将连接还回到连接池中
jedisPool.returnResource(jedis);
// 释放连接池
jedisPool.close();
}
}
5、ShardedJedisPool的配置和使用
import java.util.ArrayList;
import java.util.List;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
/**
* 集群式的连接池
*
*/
public class ShardedJedisPoolDemo {
public static void main(String[] args) {
// 构建连接池配置信息
JedisPoolConfig poolConfig = new JedisPoolConfig();
// 设置最大连接数
poolConfig.setMaxTotal(50);
// 定义集群信息
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
// 定义A节点信息
JedisShardInfo infoA = new JedisShardInfo("192.168.2.111", 6379);
// 设置A节点的访问密码
infoA.setPassword("123456");
// 定义B节点信息
JedisShardInfo infoB = new JedisShardInfo("192.168.2.116", 6379);
// 设置B节点的访问密码
infoB.setPassword("123456");
// 加入集群节点
shards.add(infoA);
shards.add(infoB);
// 定义集群连接池
ShardedJedisPool shardedJedisPool = new ShardedJedisPool(poolConfig,
shards);
ShardedJedis shardedJedis = null;
try {
// 从连接池中获取到jedis分片对象
shardedJedis = shardedJedisPool.getResource();
for (int i = 0; i < 100; i++) {
shardedJedis.set("key" + i, "value" + i);
}
// 从redis中获取数据
String value = shardedJedis.get("mytest");
System.out.println(value);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != shardedJedis) {
// 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态
shardedJedis.close();
}
}
// 关闭连接池
shardedJedisPool.close();
}
}
6、源码下载