java.net.UnknownHostException: "localhost": 名称或服务未知
java.net.UnknownHostException: "localhost": Name or service not known
我正在尝试使用 Jedis 连接到 Java 中的 Redis。我这样做就像
return new JedisPool(jedisPoolConfig,
redisData.get("address").toString(),
redisData.get("port").asInt(),
2000,
redisData.get("password").toString());
和{"address": "localhost", "port": 6363, "password": "password}
但是,我在尝试连接时遇到此错误:
redis.clients.jedis.exceptions.JedisConnectionException: Failed to create socket.
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:116)
at redis.clients.jedis.Connection.connect(Connection.java:180)
at redis.clients.jedis.Connection.initializeFromClientConfig(Connection.java:338)
at redis.clients.jedis.Connection.<init>(Connection.java:53)
at redis.clients.jedis.Jedis.<init>(Jedis.java:212)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:181)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:571)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:298)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:223)
at redis.clients.jedis.util.Pool.getResource(Pool.java:34)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:364)
at org.myapp.monitors.Monitor$DefaultImpls.setRedis(Monitor.kt:13)
at org.myapp.monitors.IntMonitor.setRedis(IntMonitor.kt:8)
at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:10)
at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:8)
at org.myapp.monitors.MonitorFactory.makeMonitor(MonitorFactory.kt:22)
at org.myapp.utils.ConfigUtils.getMonitors(ConfigUtils.java:62)
at org.myapp.tp.TelPub.<clinit>(TelPub.kt:16)
Caused by: java.net.UnknownHostException: "localhost": Name or service not known
at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932)
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
... 17 more
Caused by: java.net.UnknownHostException: "localhost"
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:800)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
... 17 more
如果我将 "localhost"
替换为 "127.0.0.1"
或 "::1"
.
,也会发生同样的事情
我使用的是 popOS 21.10,这是我的 /etc/host
文件:
127.0.0.1 localhost
::1 localhost
127.0.1.1 pop-os.localdomain pop-os
echo $HOST_NAME
returns "thinkpad-t440p".
我试过了
127.0.0.1 thinkpad-t440p localhost
::1 localhost
127.0.1.1 pop-os.localdomain pop-os
但同样的问题。
ping localhost
给我
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.025 ms
ping thinkpad-t440p
给我
PING thinkpad-t440p (192.168.0.42) 56(84) bytes of data.
64 bytes from thinkpad-t440p (192.168.0.42): icmp_seq=1 ttl=64 time=0.027 ms
确认你的 Redis 服务器在哪个端口运行。
With {"address": "localhost", "port": 6363, "password": "password}
Redis 默认运行在 6379
端口,而不是 6363
。
问题出在使用 toString()
方法,该方法基本上是将一个字符串放在一个字符串中,以便为其添加额外的引号或其他内容。
需要改用asString()
。
我正在尝试使用 Jedis 连接到 Java 中的 Redis。我这样做就像
return new JedisPool(jedisPoolConfig,
redisData.get("address").toString(),
redisData.get("port").asInt(),
2000,
redisData.get("password").toString());
和{"address": "localhost", "port": 6363, "password": "password}
但是,我在尝试连接时遇到此错误:
redis.clients.jedis.exceptions.JedisConnectionException: Failed to create socket.
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:116)
at redis.clients.jedis.Connection.connect(Connection.java:180)
at redis.clients.jedis.Connection.initializeFromClientConfig(Connection.java:338)
at redis.clients.jedis.Connection.<init>(Connection.java:53)
at redis.clients.jedis.Jedis.<init>(Jedis.java:212)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:181)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:571)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:298)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:223)
at redis.clients.jedis.util.Pool.getResource(Pool.java:34)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:364)
at org.myapp.monitors.Monitor$DefaultImpls.setRedis(Monitor.kt:13)
at org.myapp.monitors.IntMonitor.setRedis(IntMonitor.kt:8)
at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:10)
at org.myapp.monitors.IntMonitor.<init>(IntMonitor.kt:8)
at org.myapp.monitors.MonitorFactory.makeMonitor(MonitorFactory.kt:22)
at org.myapp.utils.ConfigUtils.getMonitors(ConfigUtils.java:62)
at org.myapp.tp.TelPub.<clinit>(TelPub.kt:16)
Caused by: java.net.UnknownHostException: "localhost": Name or service not known
at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:932)
at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1517)
at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:851)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
... 17 more
Caused by: java.net.UnknownHostException: "localhost"
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:800)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1507)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1366)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1300)
at redis.clients.jedis.DefaultJedisSocketFactory.connectToFirstSuccessfulHost(DefaultJedisSocketFactory.java:58)
at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:87)
... 17 more
如果我将 "localhost"
替换为 "127.0.0.1"
或 "::1"
.
我使用的是 popOS 21.10,这是我的 /etc/host
文件:
127.0.0.1 localhost
::1 localhost
127.0.1.1 pop-os.localdomain pop-os
echo $HOST_NAME
returns "thinkpad-t440p".
我试过了
127.0.0.1 thinkpad-t440p localhost
::1 localhost
127.0.1.1 pop-os.localdomain pop-os
但同样的问题。
ping localhost
给我
PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.025 ms
ping thinkpad-t440p
给我
PING thinkpad-t440p (192.168.0.42) 56(84) bytes of data.
64 bytes from thinkpad-t440p (192.168.0.42): icmp_seq=1 ttl=64 time=0.027 ms
确认你的 Redis 服务器在哪个端口运行。
With
{"address": "localhost", "port": 6363, "password": "password}
Redis 默认运行在 6379
端口,而不是 6363
。
问题出在使用 toString()
方法,该方法基本上是将一个字符串放在一个字符串中,以便为其添加额外的引号或其他内容。
需要改用asString()
。