美文网首页
There was an unexpected error (t

There was an unexpected error (t

作者: 秋元_92a3 | 来源:发表于2020-04-30 23:26 被阅读0次

使用nacos java-sdk 的client连接nacos服务端的时候报错,具体的错误信息如下“

ErrCode:403, ErrMsg:<html><body><h1>Whitelabel Error Page</h1><p>This application has no explicit mapping for /error, so you are seeing this as a fallback.</p><div id='created'>Thu Apr 30 15:00:25 GMT+08:00 2020</div><div>There was an unexpected error (type=Forbidden, status=403).</div><div>unknown user!</div></body></html>
    at com.alibaba.nacos.client.config.impl.ClientWorker.getServerConfig(ClientWorker.java:260)
    at com.alibaba.nacos.client.config.NacosConfigService.getConfigInner(NacosConfigService.java:173)
    at com.alibaba.nacos.client.config.NacosConfigService.getConfig(NacosConfigService.java:122)
    at com.czb.chargepile.core.NacosConfigLoad.main(NacosConfigLoad.java:31)

我是使用nacos提供给java的sdk进行连接的,连接代码与参照的是的官方代码,但是由于我的nacos的服务段设置的是验证权限,所以在登陆参数中又有些许添加的元素。我的连接的代码如下:

public static void load() {
        properties = new Properties();
        try {
            properties.put("serverAddr", "127.0.0.1:5566");
            properties.put("username", "test");
            properties.put("password", "123456");
            properties.put("contextPath", "nacos");
            properties.put("namespace", "nacos");
            properties.put("fileExtension", "yaml");
            final ConfigService configService = NacosFactory.createConfigService(properties);
            log.info("nacosTimeOut:{}", 3000);
            final String config = configService.getConfig("application.yml", "nacosTest", 3000);
            log.info("nacos config load:{}", config);
            ByteArrayInputStream in = new ByteArrayInputStream(config.getBytes());
            InputStreamReader input = new InputStreamReader(in, "utf-8");
            properties.load(input);
        } catch (Exception e) {
            log.error("file io exception", e);
        }
    }

通过查询,下面遇到的问题对我有所启发:

https://github.com/alibaba/nacos/issues/2462
我将nacos-server升级到1.2.0版本后,使用nacos-client 1.2.0版本进行连接,基于 spring-cloud-alibaba。
当我开启nacos-server的权限验证后,在bootstrap.properties中写入username和password两个参数,server-addr为ip:port,不填写context-path。
在项目启动时,检测到这username和password参数,nacos-client会进行登录操作,但请求的地址是http://ip:8848/v1/auth/users/login,并没有加上/nacos,且请求结果为HTTP Status 404 – Not Found,而经测试正确的地址应该是http://ip:8848/nacos/v1/auth/users/login
经过debug发现在nacos-client:1.2.0的com.alibaba.nacos.client.security.SecurityProxy的第86行,
contextPath = properties.getProperty(PropertyKeyConst.CONTEXT_PATH, "/nacos");方法并没有获取正确的contextPath,应该获取为"/nacos"但实际获取的是""。
最后,我通过将一个域名:80解析到ip:8848/nacos来解决了这个问题。并在bootstrap.properties配置server-addr=域名:80和context-path=/
我想知道,这是不是nacos-client:1.2.0存在的问题,还是由于我自身的错误设置导致的。

于是我抱着试试看的态度,也开始怀疑是不是的我的版本问题,于是,发现我引用的版本是

<dependency>
       <groupId>com.alibaba.nacos</groupId>
       <artifactId>nacos-client</artifactId>
       <version>1.1.4</version>
</dependency>

于是,查看当前nacos-client的更新版本情况


image.png

发现最新版本是1.2.1,于是进行修改,然后运行程序,发送不再报403,已经能够从nacos正常拉取到配置。

附上官方的通过java client 拉取nacos配置的地址:
https://nacos.io/zh-cn/blog/nacos%201.2.0%20guide.html

相关文章

网友评论

      本文标题:There was an unexpected error (t

      本文链接:https://www.haomeiwen.com/subject/bhgzwhtx.html