昨天在调试代码时,因为升级了Nacos的版本,应用一直获取不到Nacos的配置,查看了下Nacos的日志,原因是未授权,于是将nacos依赖升级到最新的0.2.7

application.yml配置如下:

image-20200411180751520

启动程序后,还是无法获取,测试了下,发现能连接上Nacos,但是获取不到配置项

image-20200411181207507

于是乎,使用Fiddler抓下包,看看返回内容是咋回事,先设置下代理

1
2
3
4
System.setProperty("http.proxyHost", "localhost");
System.setProperty("http.proxyPort", "8888");
System.setProperty("https.proxyHost", "localhost");
System.setProperty("https.proxyPort", "8888");

查看到请求路径多了个"/",但是授权接口是正常的.

image-20200411181524593

又看了下nacos-client拼接url的源码

1
2
3
 private String getUrl(String serverAddr, String relativePath) {
        return serverAddr + "/" + serverListMgr.getContentPath() + relativePath;
    }

把nacos.contextPath改成nacos试试

image-20200411182132981

我😐……

之后看了下nacos-client最新的源码:

1
2
3
4
5
private String getUrl(String serverAddr, String relativePath) {
        String contextPath = serverListMgr.getContentPath().startsWith("/") ?
                serverListMgr.getContentPath() : "/" + serverListMgr.getContentPath();
        return serverAddr + contextPath + relativePath;
}

在github上维护者也回复了,nacos-client需要替换到对应的版本,改下pom.xml的依赖.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 <!--AliBaba NacOs依赖-->
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>${nacos-config-spring-boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.2.1</version>
        </dependency>

至此,大功告成.