具体情境~ 在win下的idea连接linux中已经开启的redis,
看java代码~
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class jredisDemo01 {
public static void main(String[] args) {
try {
Jedis jedis = new Jedis("192.168.135.145",6379);
jedis.set("fruit","lisi");
String fruit = jedis.get("fruit");
System.out.println(fruit);
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行后报错~
redis.clients.jedis.exceptions.JedisConnectionException: Failed to connect to any host resolved for DNS name.
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:63)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
at redis.clients.jedis.Connection.connect(Connection.java:180)
at redis.clients.jedis.Connection.sendCommand(Connection.java:152)
at redis.clients.jedis.Connection.executeCommand(Connection.java:121)
at redis.clients.jedis.Jedis.set(Jedis.java:4725)
at com.gavin.jredisDemo01.main(jredisDemo01.java:16)
Suppressed: java.net.SocketTimeoutException: connect timed out
at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method)
at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107)
at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
at java.base/java.net.Socket.connect(Socket.java:591)
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:73)
... 6 more
异常~Failed to connect to any host resolved for DNS name. connect timed out 异常发生之前----->>
1,注掉redis配置文件中bind—>>使得外部可以连接到redis (也可以指定连接地址)
2,保护模式已经关闭—>>不用输入密码可以连接
3,以设置守护模式 -->>使得redis可以在后台运行
最后查看端口号是否对外开放~
[root@localhost bin]# sudo firewall-cmd --zone=public --add-port=6379/tcp --permanent
success
[root@localhost bin]# systemctl restart firewalld
注意~之后要重启防火墙,使得开放端口生效
再次运行代码~
正常了
|