当前位置: 首页 > 知识库问答 >
问题:

获取java.net.unknownHostException:hostname:使用spring-data-redis-starter时名称或服务未知

狄冠宇
2023-03-14

我试图通过使用spring-boot-starter-data-redis和lettuce库以集群模式连接到Redis,

        2019-08-21 00:55:42.695  WARN 75 --- [ioEventLoop-6-1] i.l.c.c.topology.ClusterTopologyRefresh  : Unable to connect to myhostname.service:6379

java.util.concurrent.CompletionException: java.net.UnknownHostException: myhostname.service: Name or service not known
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292) ~[na:1.8.0_181]
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308) ~[na:1.8.0_181]
    at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593) ~[na:1.8.0_181]
    at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577) ~[na:1.8.0_181]
    at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474) ~[na:1.8.0_181]
    at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977) ~[na:1.8.0_181]
    at io.lettuce.core.AbstractRedisClient.lambda$initializeChannelAsync$1(AbstractRedisClient.java:275) ~[lettuce-core-5.0.4.RELEASE.jar!/:na]
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:485) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.setFailure(DefaultPromise.java:112) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.DefaultChannelPromise.setFailure(DefaultChannelPromise.java:89) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.bootstrap.Bootstrap.doResolveAndConnect0(Bootstrap.java:216) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.bootstrap.Bootstrap.access$000(Bootstrap.java:49) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.bootstrap.Bootstrap$1.operationComplete(Bootstrap.java:188) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.bootstrap.Bootstrap$1.operationComplete(Bootstrap.java:174) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:511) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:485) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:424) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.DefaultPromise.trySuccess(DefaultPromise.java:103) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.DefaultChannelPromise.trySuccess(DefaultChannelPromise.java:84) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetSuccess(AbstractChannel.java:978) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.register0(AbstractChannel.java:512) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe.access$200(AbstractChannel.java:423) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.AbstractChannel$AbstractUnsafe$1.run(AbstractChannel.java:482) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:465) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_181]
Caused by: java.net.UnknownHostException: master1.event-store-service-V-70125f6-2-1566348843-redis.service: Name or service not known
    at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_181]
    at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_181]
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName0(InetAddress.java:1276) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName(InetAddress.java:1192) ~[na:1.8.0_181]
    at java.net.InetAddress.getAllByName(InetAddress.java:1126) ~[na:1.8.0_181]
    at java.net.InetAddress.getByName(InetAddress.java:1076) ~[na:1.8.0_181]
    at io.netty.util.internal.SocketUtils$8.run(SocketUtils.java:146) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.util.internal.SocketUtils$8.run(SocketUtils.java:143) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_181]
    at io.netty.util.internal.SocketUtils.addressByName(SocketUtils.java:143) ~[netty-common-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.DefaultNameResolver.doResolve(DefaultNameResolver.java:43) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.SimpleNameResolver.resolve(SimpleNameResolver.java:63) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.SimpleNameResolver.resolve(SimpleNameResolver.java:55) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.InetSocketAddressResolver.doResolve(InetSocketAddressResolver.java:57) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.InetSocketAddressResolver.doResolve(InetSocketAddressResolver.java:32) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.resolver.AbstractAddressResolver.resolve(AbstractAddressResolver.java:108) ~[netty-resolver-4.1.25.Final.jar!/:4.1.25.Final]
    at io.netty.bootstrap.Bootstrap.doResolveAndConnect0(Bootstrap.java:208) ~[netty-transport-4.1.25.Final.jar!/:4.1.25.Final]
    ... 18 common frames omitted

注意:“myhostname.service”是由于我正在使用consulg,我可以ping myhostname.service也可以使用“redis-cli-c-h myhostname.service-p6379”命令连接到它。

@Configuration
@ConfigurationProperties(prefix = "redis")
public class LettuceCacheConfig {

    private static Logger logger = LoggerFactory.getLogger(LettuceCacheConfig.class);
    private static Long topologyRefreshDuration = 10L;
    private static int maxRedirects = 3;

    private String servers;
    private String profileName; 
    private String password;

    private PoolConfig poolConfig = new PoolConfig();

    class PoolConfig {
        int maxTotal;
        int maxIdle;
        int minIdle;

        public PoolConfig() {
        }
    }

    public LettuceClientConfiguration getLettuceClientConfig() {
        final ClusterTopologyRefreshOptions options = ClusterTopologyRefreshOptions.builder()
                .enablePeriodicRefresh(Duration.of(topologyRefreshDuration, ChronoUnit.MINUTES))
                .enableAllAdaptiveRefreshTriggers().dynamicRefreshSources(true).build();

        final LettuceClientConfiguration lettuceClientConfig = LettuceClientConfiguration.builder()
                .clientOptions(ClusterClientOptions.builder().topologyRefreshOptions(options)
                        .validateClusterNodeMembership(false).build())
                .build();

        return lettuceClientConfig;
    }


    private GenericObjectPoolConfig getPoolingConfiguration() {
        final GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setMaxIdle(this.poolConfig.getMaxIdle());
        config.setMaxTotal(this.poolConfig.getMaxTotal());
        config.setMinIdle(this.poolConfig.getMinIdle());
        return config;
    }

    public RedisClusterConfiguration getClusterConfiguration() {
        final RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration();//new RedisClusterConfiguration(getRedisServers());
        String hostPort = getRedisServers().get(0);
        String tokens[] = hostPort.split(":");
        logger.info("REDIS HOST IS {}", tokens[0]);
        logger.info("REDIS PORT IS {}", tokens[1]);
        clusterConfiguration.clusterNode(tokens[0], Integer.parseInt(tokens[1]));
        clusterConfiguration.setMaxRedirects(maxRedirects);
        logger.info("PASSWORD is {}",password);
        clusterConfiguration.setPassword(RedisPassword.of(password));
        return clusterConfiguration;
    }



    private List<String> getRedisServers() {
        String tokens[] = servers.split(",");
        logger.debug("Redis Servers are {}", Arrays.toString(tokens));
        return Arrays.asList(tokens);
    }

    @Bean
    public RedisConnectionFactory getLettuceConnectionFactory() {
        return new LettuceConnectionFactory(getClusterConfiguration(),
         getLettuceClientConfig());
    }

}

因为我在java.net.inetaddress.getByName(“hostName”)中遇到异常,

>

  • 从我的应用程序容器运行一个redis-cli来连接到redis集群容器,它是成功的。(如上所述)

    因为我在java.net.inetaddress.getByName(“hostname”)中遇到异常,所以我在应用程序容器中使用我的redis主机名尝试了相同的方法。它成功了,没有引发UnknownHostException。

  • 共有1个答案

    强阳曜
    2023-03-14

    我不能说这样就完全回答了这个问题,但得到了问题的原因。问题是当应用程序容器启动时,redis容器/服务还没有在领事处注册,所以查找名称为“master1.redis.service”的redis容器返回unknownHostException。但是由于我运行了redis-cli和ping,过了一段时间就没有连接问题了。我通过重新启动应用程序容器,同时保持Redis容器正常运行来验证这一点,然后没有未知的HostException。我不知道的原因是,最初如果有UnknowwhostException,为什么ClusterTopologyRefreshOptions不能工作?根据我的想法,它应该在一段时间后再次查找主机名,并从领事那里获得决议/IP。但这并没有发生

     类似资料:
    • 问题内容: 我正在尝试使用httplib将信用卡信息发送到authorize.net。当我尝试发布请求时,得到以下回溯: 我这样建立我的请求: 要添加另一层,它正在我们的开发服务器上运行,该服务器具有httplib 2.6,并且在httplib.HTTPSConnection中没有source_address参数。 任何帮助是极大的赞赏。 =============================

    • 我们有一个私人空间,并提供了一个内部API的访问权限,这样我们就可以从私人空间应用程序访问它。因此,我们开发了一个演示Java应用程序来测试它。当我们尝试用HTTP连接服务时,堆栈跟踪会像这样结束。 当我们ping私有空间中的相应IP时,我们可以看到它已启动。但是当我们试图从Java应用程序中访问服务时,它就像上面那样结束了。 Java 代码是这样的: 有没有人有任何想法来解决这个问题? 谢谢你。

    • 问题可能是什么,如何解决?请帮帮我

    • 问题内容: 我正在满足以下要求的应用程序(Spring): 从Redis Server1读取数据 从Redis Server2读取数据 从Redis Server3读取数据 和 将信息保存到MySQL。 有人可以给我们一个思路,使用Spring Data Redis连接到不同的Redis服务器。 获得了链接:http : //forum.spring.io/forum/spring- projec

    • 我正在尝试运行一个电子邮件收割机,当我在没有循环的情况下手动输入url时,我没有任何连接错误。 然而,当我从文件中加载url时,我得到以下错误“名称或服务错误” 回溯(最近一次调用):文件“/usr/lib/python3/dist-packages/urllib3/connection.py”,第141行,在新连接(self.host,self.port),self.timeout,**extr

    • 我正在尝试使用Apache Http组件4.5.1使用Instagram进行oauth登录,但未能成功获取访问令牌。 我很确定这是库本身的问题,因为如果我卷曲,我会得到我想要的结果。 所以,我尝试了几种不同的发布调用方式,但它们都给我相同的结果,所以我将发布我发现的最优雅的方式是使用Fluent-hc lib: 我得到的结果是: Java语言网未知后异常:api。instagram。com:名称或