Jedis客户端使用初探

Jedis 是 Redis 官方首选的 Java 客户端开发包。

虚拟机的IP地址是192.168.30.128

Jedis代码是放在windows上的,启动虚拟机上的Redis服务之后,用Jedis连接。

以下是Jedis 操作Redis的简单示例,直接贴代码.

搭建Jedis之前,需要引入pom依赖


            redis.clients
            jedis
            2.10.0
            jar
            compile

然后手动输入虚拟机ip地址和端口号进行连接

Jedis jedis = new Jedis("192.168.30.128", 6379);

然后第一次连接发现启动报错,无法连接到redis

D:\software\jdk\bin\java.exe "-javaagent:D:\software\IntelliJ IDEA 2018.1.4\lib\idea_rt.jar=59789:D:\software\IntelliJ IDEA 2018.1.4\bin" -Dfile.encoding=UTF-8 -classpath D:\software\jdk\jre\lib\charsets.jar;D:\software\jdk\jre\lib\deploy.jar;D:\software\jdk\jre\lib\ext\access-bridge-64.jar;D:\software\jdk\jre\lib\ext\cldrdata.jar;D:\software\jdk\jre\lib\ext\dnsns.jar;D:\software\jdk\jre\lib\ext\jaccess.jar;D:\software\jdk\jre\lib\ext\jfxrt.jar;D:\software\jdk\jre\lib\ext\localedata.jar;D:\software\jdk\jre\lib\ext\nashorn.jar;D:\software\jdk\jre\lib\ext\sunec.jar;D:\software\jdk\jre\lib\ext\sunjce_provider.jar;D:\software\jdk\jre\lib\ext\sunmscapi.jar;D:\software\jdk\jre\lib\ext\sunpkcs11.jar;D:\software\jdk\jre\lib\ext\zipfs.jar;D:\software\jdk\jre\lib\javaws.jar;D:\software\jdk\jre\lib\jce.jar;D:\software\jdk\jre\lib\jfr.jar;D:\software\jdk\jre\lib\jfxswt.jar;D:\software\jdk\jre\lib\jsse.jar;D:\software\jdk\jre\lib\management-agent.jar;D:\software\jdk\jre\lib\plugin.jar;D:\software\jdk\jre\lib\resources.jar;D:\software\jdk\jre\lib\rt.jar;D:\mavenspace\JedisTest\target\classes;C:\Users\liweiqiang\.m2\repository\redis\clients\jedis\2.10.0\jedis-2.10.0.jar;C:\Users\liweiqiang\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar;C:\Users\liweiqiang\.m2\repository\org\apache\commons\commons-pool2\2.4.3\commons-pool2-2.4.3.jar com.lwq.JTest
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host 192.168.30.128:6379
    at redis.clients.jedis.Connection.connect(Connection.java:207)
    at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:104)
    at redis.clients.jedis.Connection.sendCommand(Connection.java:126)
    at redis.clients.jedis.BinaryClient.get(BinaryClient.java:138)
    at redis.clients.jedis.Client.get(Client.java:68)
    at redis.clients.jedis.Jedis.get(Jedis.java:185)
    at com.lwq.JTest.main(JTest.java:8)
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.Connection.connect(Connection.java:184)
    ... 6 more

Process finished with exit code 1

后经查阅发现是虚拟机redis的配置文件ip指向的并非192.168.30.128而是 127.0.0.1,更改ip指向即可实现连接


Jedis客户端使用初探_第1张图片
image.png

修改完 通过ps -ef|grep redis 查看进程 保证进程ip+端口号和程序连接一直就可以实现访问

image.png

注意 配置文件如果bind只修改为192.168.30.128 那么虚拟机就无法连接cli端了 所以需要绑定两个
bind 192.168.30.128 127.0.0.1
关于其他几种类型的保存和访问 这里不再赘述

你可能感兴趣的:(Jedis客户端使用初探)