前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >new URI(zk_servers_1) 路径包含下划线无法获取host的问题

new URI(zk_servers_1) 路径包含下划线无法获取host的问题

作者头像
全栈程序员站长
发布2021-07-06 14:54:07
发布2021-07-06 14:54:07
60600
代码可运行
举报
运行总次数:0
代码可运行

spring cloud gateway使用zookeeper作为注册中心调用其它服务的时候报了下面这个错误:

代码语言:javascript
代码运行次数:0
复制
ava.lang.NullPointerException: null
    at io.netty.util.NetUtil.isValidIpV4Address(NetUtil.java:648) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
    at io.netty.util.NetUtil.createByteArrayFromIpAddressString(NetUtil.java:368) ~[netty-common-4.1.29.Final.jar:4.1.29.Final]
    at reactor.ipc.netty.options.InetSocketAddressUtil.attemptParsingIpString(InetSocketAddressUtil.java:132) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]
    at reactor.ipc.netty.options.InetSocketAddressUtil.createForIpString(InetSocketAddressUtil.java:80) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]
    at reactor.ipc.netty.options.InetSocketAddressUtil.createInetSocketAddress(InetSocketAddressUtil.java:69) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]
    at reactor.ipc.netty.options.ClientOptions.createInetSocketAddress(ClientOptions.java:253) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]
    at reactor.ipc.netty.http.client.HttpClientOptions.getRemoteAddress(HttpClientOptions.java:87) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]
    at reactor.ipc.netty.http.client.MonoHttpClientResponse.lambda$subscribe$0(MonoHttpClientResponse.java:76) ~[reactor-netty-0.7.10.RELEASE.jar:0.7.10.RELEASE]

调用的地址是http://zks_servers_1:18001,zks_servers_1是服务的hosts配置的名称,空指针异常跟踪发现是下面这个问题导致的:

代码语言:javascript
代码运行次数:0
复制
//类HttpClientOptions
 public final InetSocketAddress getRemoteAddress(URI uri) {
        Objects.requireNonNull(uri, "uri");
        boolean secure = isSecure(uri);
        int port = uri.getPort() != -1 ? uri.getPort() : (secure ? 443 : 80);
        boolean shouldResolveAddress = !this.useProxy(uri.getHost());
        return this.createInetSocketAddress(uri.getHost(), port, shouldResolveAddress);
    }

uri.getHost()返回值是null,也就是说根据上面的调用地址,没有获取到对应的host。 uri的创建方式是:

代码语言:javascript
代码运行次数:0
复制
//类MonoHttpClientResponse
MonoHttpClientResponse(HttpClient parent, String url, HttpMethod method, Function<? super HttpClientRequest, ? extends Publisher<Void>> handler) {
        this.parent = parent;
        boolean isWs = Objects.equals(method, HttpClient.WS);

        try {
            this.startURI = new URI(parent.options.formatSchemeAndHost(url, isWs));
        } catch (URISyntaxException var7) {
            throw Exceptions.bubble(var7);
        }

        this.method = isWs ? HttpMethod.GET : method;
        this.handler = handler;
    }

创建方式是调用URI的new URI(String)方法,知道原因之后在本地测试:

代码语言:javascript
代码运行次数:0
复制
try {
            URI uri = new URI("http://zks_servers_1:18001/test.html");
            String host = uri.getHost();
            System.out.println(host);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

确实获取不到host,查看源码发现:

代码语言:javascript
代码运行次数:0
复制
 /**
     * Returns the host component of this URI.
   
     *
     *   <li><p> A domain name consisting of one or more <i>labels</i>
     *   separated by period characters ({@code '.'}), optionally followed by
     *   a period character.  Each label consists of <i>alphanum</i> characters
     *   as well as hyphen characters ({@code '-'}), though hyphens never
     *   occur as the first or last characters in a label. The rightmost
     *   label of a domain name consisting of two or more labels, begins
     *   with an <i>alpha</i> character. </li>
     
     * </ul>
     *
     * The host component of a URI cannot contain escaped octets, hence this
     * method does not perform any decoding.
     *
     * @return  The host component of this URI,
     *          or {@code null} if the host is undefined
     */
    public String getHost() {
        return host;
    }

谷歌翻译:

代码语言:javascript
代码运行次数:0
复制
由一个或多个标签组成的域名
   由句点字符代码'.'分隔,可选地后跟
   一个英文句号角色。 每个标签由alphanum字符组成
   以及连字符字符代码' - ',虽然连字符永远不会
   作为标签中的第一个或最后一个字符出现。 最右边包含最少长度最少两个并且以英文字符开始的标签

举例如下: www.baidu.com,这个域名包含三个标签www、baidu、com; www.baidu-zhidao.com,这个域名包含三个标签www、baidu-zhidao、com; 这个例子就是说明每个标签都可以使用-连接;

然后看一下,我的报错的服务名称zks_servers_1,这个名称没有以【.】分割,包含了非法字符【_】最后以单个数字结尾也不符合要求。 实际上测试发现zks-servers-1这样也是不正确的,不知道是不是翻译的有问题,这个结果和翻译不太匹配。

总而言之,修改服务器的hosts配置就行了。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101074.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档