首页 > 解决方案 > Spring Cloud Config Server:如何覆盖应用程序特定文件中的共享属性

问题描述

我想按照下面的建议在多个应用程序中共享一些属性,并且我希望能够覆盖该值,因此如果我在 application-dev.properties 中有 x=1,我应该能够在我的应用程序特定文件中覆盖 x即在我的情况下 test_app-dev.properties 包含 x=2。所以当我打电话给http://local host:8888/test_app/dev x=1 胜过一切。文件在 git 中。它不应该返回 x=2 还是我误解了共享属性的意图?

https://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html中,“2.1.5 与所有应用程序共享配置”段落。它说:

使用基于文件的(即 git、svn 和本机)存储库,在 application* 中具有文件名的资源在所有客户端应用程序之间共享(如 application.properties、application.yml、application-*.properties 等)。您可以使用具有这些文件名的资源来配置全局默认值,并根据需要让它们被特定于应用程序的文件覆盖。

标签: spring-bootspring-cloud-config

解决方案


最初我自己也面临这个问题。在查看了@user1549440 的答案后,对如何使用常用属性有了一个想法。将可共享配置文件夹安排为第一个可搜索路径后,它按预期工作。刚刚添加了方便解决方案的工作示例。

下面是中的示例配置configuration-server

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: file://app/configurations
          default-label: master
          search-paths:
            - 'common*'
            - 'petstore*'
            - 'inventory*'
  info:
     app:
       name: Configuration Server
       description: Spring Cloud Configuration Server
       version: 1.0.0
  security: 
    user: 
      name: myname
      password: mypass

以下是应用程序的示例文件夹结构。

├── app
│   └── configurations
│       ├── common
│       │   ├── application-dev.yml
│       │   ├── application-test.yml
│       │   ├── application-stg.yml
│       │   └── application-prod.yml
│       ├── inventory
│       │   ├── petstore-dev.yml
│       │   ├── petstore-test.yml
│       │   ├── petstore-stg.yml
│       │   └── petstore-prod.yml
│       └── petstore
│           ├── inventory-dev.yml
│           ├── inventory-test.yml
│           ├── inventory-stg.yml
│           └── inventory-prod.yml

推荐阅读