A Problem about Spring Cloud Config Auto-refresh

498 阅读1分钟

问题

在学习Spring cloud config模块时,一般都是跟着官网的Centralized Configuration | quick-start做一遍.

但在下面这步的时候,出现问题了:

configuration-client/src/main/resources/bootstrap.properties

spring.application.name=a-bootiful-client
# N.B. this is the default:
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false

最后一句出现了红线,也就是这个无效了。

如果你无视这个问题,继续往下做:

大致翻译下: 改变git仓库中的a-bootiful-client.properties文件中的message值(比如改为Hello Spring),记得commit一下,你可以通过访问http://localhost:8888/a-bootiful-client/default来验证config服务器已经知道了改变,你需要调用refresh方法来强制客户端去获取新的值.......你可以通过使用post方法去请求http://localhost:8888/refresh来调用refresh方法,然后访问http://localhost:8080/message来验证是否生效。

然而当你使用post方法去请求http://localhost:8888/refresh时,返回的404。

原因

docs.spring.io | 50.2 Exposing Endpoints这个文档讲到了:

由于endpoints中会包含很多敏感信息,你需要小心的暴露它们,下面这张表展示了默认暴露的接口。这也是management.security.enabled=false被删除的原因

这里就不贴表的截图了,可以点击上面的连接详细查看,会发现除了health和info两个支持web访问外,其他的默认不支持web访问。

解决方法

在bootstrap.properties文件中添加下面的代码 management.endpoints.web.exposure.include=*

暴露全部endpoints,你也可以指定需要暴露的endpoints。