简体   繁体   English

Redis - JUnit - 引起:java.net.UnknownHostException:本地主机

[英]Redis - JUnit - Caused by: java.net.UnknownHostException: localhost

I have a SpringBoot application where I connect to Redis and it works on cloud environment:我有一个 SpringBoot 应用程序,我连接到 Redis 并且它适用于云环境:

 RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
 redisStandaloneConfiguration.setHostName("localhost");
 redisStandaloneConfiguration.setPort(6379);
 redisStandaloneConfiguration.setPassword(REDIS_PASSWORD);
 LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
 lettuceConnectionFactory.setValidateConnection(true);

Now i started a local Redis instance on localhost and port 6379;现在我在 localhost 和端口 6379 上启动了一个本地 Redis 实例; I tried to write a Junit test that uses Redis and I got the following exception我尝试编写使用 Redis 的 Junit 测试,但出现以下异常

Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to  localhost:6379
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)
    at io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:56)
    at io.lettuce.core.AbstractRedisClient.getConnection(AbstractRedisClient.java:320)
    at io.lettuce.core.RedisClient.connect(RedisClient.java:211)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.lambda$getConnection$1(StandaloneConnectionProvider.java:115)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.springframework.data.redis.connection.lettuce.StandaloneConnectionProvider.getConnection(StandaloneConnectionProvider.java:115)
    at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1459)
    ... 17 common frames omitted
Caused by: java.net.UnknownHostException:  localhost
    at java.net.InetAddress.getAllByName0(InetAddress.java:1281)
    at java.net.InetAddress.getAllByName(InetAddress.java:1193)
    at java.net.InetAddress.getAllByName(InetAddress.java:1127)
    at java.net.InetAddress.getByName(InetAddress.java:1077)

Any hints?有什么提示吗?

I tried:我试过了:

  • localhost本地主机
  • 127.0.0.1 127.0.0.1
  • put localhost 127.0.0.1 in hosts file将 localhost 127.0.0.1 放入 hosts 文件中

But got the same exception但得到了同样的例外

After some hours I found the problem:几个小时后,我发现了问题:

redis.cluster.nodes:${REDIS_CLUSTER_NODES: localhost:6379;}

If I put如果我把

redis.cluster.nodes: localhost:6379;

it works fine它工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM