使用java操作Redis
作者:互联网
插曲:
redis连接超时--Exception in thread "main"
Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host XXXX:6379
at redis.clients.jedis.Connection.connect(Connection.java:204)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)
at redis.clients.jedis.Connection.sendCommand(Connection.java:125)
at redis.clients.jedis.Connection.sendCommand(Connection.java:120)
at redis.clients.jedis.BinaryClient.multi(BinaryClient.java:523)
at redis.clients.jedis.BinaryJedis.multi(BinaryJedis.java:1877)
at com.redis.TestTX.main(TestTX.java:19)
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:181)
解决办法:
1.修改redis配置文件
注释掉bind,protected-mode为no
2.查看远程服务器的防火墙状态
systemctl status firewalld
3.按步骤执行以下命令
//查看防火墙状态
firewall-cmd --state
//开放6379端口
firewall-cmd --permanent --add-port=6379/tcp
//查看端口,执行
firewall-cmd --permanent --query-port=6379/tcp
firewall-cmd --permanent --list-ports
//重启防火墙
firewall-cmd --reload
4.重启redis
redis-cli -p 6379 shutdown
redis-server /opt/redis/back_config/redis.conf
Jedis
1.导入对应依赖
<!--导入jedis的包-->
<dependencies>
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
2.编码测试
常用的API :String, List, Set ,Hash, Zse
SpringBoot整合
标签:java,--,Redis,clients,redis,jedis,操作,net 来源: https://blog.csdn.net/qq_41835151/article/details/120319892