【问题标题】:Spring Cloud - Configuration client caching propertiesSpring Cloud - 配置客户端缓存属性
【发布时间】:2019-02-15 04:03:18
【问题描述】:

当我从我的属性存储库中更改一个值并重新启动 Spring Cloud Config Server 时,这些更改不会反映在它的使用者身上。

my-microservice/application.properties:

spring.application.name=my-service
spring.cloud.config.uri=http://localhost:8888

MyServiceController.java

@RestController
public class MyServiceController {

    @Autowired
    private Configuration configuration;

    @GetMapping("/my-service")
    public MyServiceBean retrieveMyServiceProperties() {
        // show propertie's values
        return new MyServiceBean(configuration.getPropertie1(), configuration.getPropertie2());
    }

}

spring-cloud-config-server/application.properties

server.port=8888
spring.application.name=spring-cloud-config-server

spring.cloud.config.server.git.uri=file://path

Git 存储库

my-service.properties

my-service.propertie1=1
my-service.propertie2=2

当我向 localhost:8080/my-service 发送 GET 请求时,我得到的结果是:

{  
   "propertie1":1,
   "propertie2":2
}

好的,没关系! 但是,如果我更改 my-service.properties 并重新启动我的 Spring Cloud Config Server,则所做的更改不会反映 MyServiceController。我确实需要重新启动 my-microservice 应用程序,以使更改生效。 这是正常行为吗?我的意思是,如果这是远程的,那么,它应该配置是否缓存。

【问题讨论】:

  • 你不应该重启配置服务器,而是在客户端调用/refresh actuator 端点
  • 成功了!谢谢。

标签: java spring spring-boot spring-cloud spring-cloud-config


【解决方案1】:

为了更新配置,我向localhost:8080/actuator/refresh发送了POST请求。

默认情况下,/refresh 不会暴露在执行器端点中。

我确实在 application.properties 中暴露了以下行:

management.endpoints.web.exposure.include=*

然后,向上面的端点发送一个带有 no 正文的POST 请求。

【讨论】:

  • 为什么当我们点击 /actuator/refresh 端点时它会导致客户端应用程序重新启动
【解决方案2】:

要更新您的客户端应用程序,最好使用 RabbitMQ 或 Apache Kafka 等消息代理。 这个过程分为三个层次:

  1. 客户端应用程序和配置服务器订阅消息代理中的特定主题 (/refresh)。

  2. 配置服务器在更新后立即向该主题 (/refresh) 发送 refresh 事件。 (例如 application.properties 文件在 git 中更新)。

  3. 所有客户端应用程序都在监听 refresh 事件,当它们收到刷新消息时,它们将被更新

简而言之,我们可以使用 pub-sub 模型来更新我们的客户端应用程序。

Config Server using Spring Cloud Config and Apache Kafka

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 2016-04-15
    • 2018-07-31
    • 1970-01-01
    • 2020-07-10
    • 2017-12-10
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多