首页 > 其他分享 >3.1 Jedis客户端之直连

3.1 Jedis客户端之直连

时间:2022-10-24 21:36:12浏览次数:38  
标签:clients java Redis 之直 redis jedis 3.1 Jedis


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命令


标签:clients,java,Redis,之直,redis,jedis,3.1,Jedis
From: https://blog.51cto.com/u_15843693/5791300

相关文章

  • Jedis和Lettuce
    Lettuce和Jedis都是Redis的client,所以他们都可以连接RedisServer。Jedis在实现上是直接连接的RedisServer,如果在多线程环境下是非线程安全的。每个线程都去拿自己的......
  • win11系统下python3.10安装face_recognition和dlib
     作者:......
  • puzzle(103.1)网格图一笔画
    目录​​一,一笔画完(网格图带起点)​​​​二,网格图不带起点​​​​三,六边形网格图​​一,一笔画完(网格图带起点)一笔画完(微信小程序游戏):这个游戏和我攻略过的另外2个游戏相关......
  • 番外篇: 3.1无法访问githbu的解决办法,修改host教程
     https://www.bilibili.com/read/cv18030018数码 2022-08-1017:14261阅读 · 1喜欢 · 0评论大冰兄弟粉丝:271文章:13关注  TVBox相关知识......
  • 个人翻译Introduction to Linear Algebra, 5th Edition 3.1节(仅用于交流学习,非盈利)
    本书的翻译仅为交流学习!才疏学浅,不当的地方还望指正。请勿于其它用途!PDF文件 链接一:  https://pan.baidu.com/s/1owQJo1oq_yb7sqnnjpIPzA提取码:27t1  链接二:htt......
  • asp.net core 3.1 引用的元包dll版本兼容性问题解决方案
    自从.netcore3.1出来后,大家都想立马升级到最新版本。我也是如此,微软也对​​.netcore3.1​​的官方组件不断升级,几乎每隔几天就会有部分元包可以升级。每次打开Nuget包管......
  • redis:jedis客户端
    导入依赖<!--jedis--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version></dependency><!--单元......
  • redis:jedis连接池
    Jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,因此我们推荐大家使用Jedis连接池代替Jedis的直连方式。importredis.clients.jedis.*;publicclassJe......
  • python3.10新特性
    python3.10新特性1.指定1个或多个属性类型.deffunc(num:int|float|str):print(num)func(['a',])#当传入参数与要求不符时,会给予提示2.更严格zip()......
  • python 3.10 内置函数 71个 笔记
    内置函数Python解释器内置了很多函数和类型,任何时候都能使用。以下按字母顺序给出列表。截止python3.10.8,一共是71个内置函数1.输入输出:2个print()input()......