1.获取
(1).简介
Jedis是Redis官方首选的Java客户端开发包,是一个基于Java的redis-cli,遵循Redis提供的协议,对Redis进行操作。
(2).Maven依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
2.直连方式
(1).操作流程
- 生产Jedis对象
- Jedis执行命令
- 返回执行结果
- 关闭Jedis连接
(2).string
public class JedisDemo {
public static void main(String[] args) {
//生产一个Jedis对象,这个对象负责和指定Redis节点进行通信
Jedis jedis = new Jedis("39.***.***.3", 6379, 1000, 1000);
//jedis执行set操作
jedis.set("name", "steven");
//jedis执行get操作
String name = jedis.get("name");
System.out.println("name:" + name);
}
}
- host:Redis节点所在主机的IP
- port:Redis节点所在主机的端口
- connectionTimeout:客户端连接超时时间
- soTimeout:客户端读写超时
(3).hash
public class JedisDemo {
public static void main(String[] args) {
//生产一个Jedis对象,这个对象负责和指定Redis节点进行通信
Jedis jedis = new Jedis("39.***.***.3", 6379, 1000, 1000);
//jedis执行set操作
jedis.hset("people_1", "name", "steven");
jedis.hset("people_1", "age", "30");
//jedis执行get操作
System.out.println("name:" + jedis.hgetAll("people_1"));
}
}
(4).list
public class JedisDemo {
public static void main(String[] args) {
//生产一个Jedis对象,这个对象负责和指定Redis节点进行通信
Jedis jedis = new Jedis("39.***.***.3", 6379, 1000, 1000);
//jedis执行set操作
jedis.rpush("hobby", "swimming");
jedis.rpush("hobby", "ridding");
//jedis执行get操作
System.out.println("hobby:" + jedis.lrange("hobby", 0, 1));
}
}
(5).set
public class JedisDemo {
public static void main(String[] args) {
//生产一个Jedis对象,这个对象负责和指定Redis节点进行通信
Jedis jedis = new Jedis("39.***.***.3", 6379, 1000, 1000);
//jedis执行set操作
jedis.sadd("hobby", "swimming");
jedis.sadd("hobby", "ridding");
//jedis执行get操作
System.out.println("hobby:" + jedis.smembers("hobby"));
}
}
(6).zset
public class JedisDemo {
public static void main(String[] args) {
//生产一个Jedis对象,这个对象负责和指定Redis节点进行通信
Jedis jedis = new Jedis("39.***.***.3", 6379, 1000, 1000);
//jedis执行set操作
jedis.zadd("score", 99, "steven");
jedis.zadd("score", 88, "owen");
jedis.zadd("score", 78, "mike");
//jedis执行get操作
System.out.println("score:" + jedis.zrangeWithScores("score", 0, -1));
}
}
3.异常解决
(1).Failed connecting to ip:port
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to 39.***.***.3:6379
at redis.clients.jedis.Connection.connect(Connection.java:165)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:109)
at redis.clients.jedis.Connection.sendCommand(Connection.java:114)
at redis.clients.jedis.BinaryClient.set(BinaryClient.java:133)
at redis.clients.jedis.Client.set(Client.java:58)
at redis.clients.jedis.Jedis.set(Jedis.java:153)
at com.example.app.JedisDemo.main(JedisDemo.java:15)
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:53)
at redis.clients.jedis.Connection.connect(Connection.java:158)
... 6
- 机器之间网络无法联通
- ip和端口号不正确
- 虚拟机中防火墙的原因(可能性较大,通过firewall-cmd --reload命令重启防火强)
- redis.conf中bind 127.0.0.1未用(需要注释掉)
(2).DENIED Redis is running in protected mode
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.
at redis.clients.jedis.Protocol.processError(Protocol.java:132)
at redis.clients.jedis.Protocol.process(Protocol.java:166)
at redis.clients.jedis.Protocol.read(Protocol.java:220)
at redis.clients.jedis.Connection.readProtocolWithCheckingBroken(Connection.java:278)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:215)
at redis.clients.jedis.Connection.getBulkReply(Connection.java:205)
at redis.clients.jedis.Jedis.get(Jedis.java:185)
at com.example.app.JedisDemo.main(JedisDemo.java:18)
- 在客户端执行config set protected-mode no命令