2

下面是 spring-boot 应用程序的 application.properties

spring.application.name=test-service
server.port=8080

management.port=8081
management.context-path=/admin

spring.cloud.config.uri=http://localhost:8888
endpoints.refresh.enabled=true
endpoints.restart.enabled=true

当我启动应用程序时,它会联系配置服务器并按预期加载属性。

我修改配置服务器上的属性并使用触发应用程序刷新

curl -X POST http://localhost:8081/admin/refresh

API 打印已更改的属性的名称。

当我在该物业上进行获取时,我仍然看到旧值

curl -X GET http://localhost:8081/admin/env/{property_name}

我触发了重新启动,它会获取新的属性值

curl -X POST http://localhost:8081/admin/restart 

当我尝试使用更改属性值时看到相同的行为

curl -X POST  http://localhost:8081/admin/env -d property1=123

当我尝试获取属性值时,我仍然看到旧值

curl -X GET http://localhost:8081/admin/env/property1

当我将 management.port 更改为 8080(与 server.port 相同)时,一切都按预期工作。

这种行为是预期的吗?在我看来,它正在修改 2 个不同的环境,一个用于运行在 8080 上的服务器,另一个用于运行在 8081 上的服务器。

4

2 回答 2

1

您能否分享您的 pom.xml 和应用程序主(条目)文件。无论我们可以使用不同的端口。如果您能够在“/refresh”调用中获得更改的属性,那么它也应该在您的应用程序中工作。你确定你在你的bean中使用@RefreshScope吗?

注意:@RefreshScope 不适用于 @Configuration 注释。有关更多详细信息,请遵循文档https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html

于 2016-09-26T14:37:53.927 回答
0

看起来是spring-boot的问题。

当 server.port 和 management.port 不同时,EnvironmentEndpoint 和 EnvironmentMVCEndpoint 被注入了 2 个不同的 Environment。

应用程序环境具有更新的值,但是当我获得特定属性 EnviromentMVCEndpoint 的值时,它并没有反映其 Environment 中的正确值

@GetMapping(value = "/{name:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@HypermediaDisabled
public Object value(@PathVariable String name) {
    if (!getDelegate().isEnabled()) {
        // Shouldn't happen - MVC endpoint shouldn't be registered when delegate's
        // disabled
        return getDisabledResponse();
    }
    return new NamePatternEnvironmentFilter(this.environment).getResults(name);
}

@Override
public void setEnvironment(Environment environment) {
    this.environment = environment;
}
于 2016-09-26T16:23:34.437 回答