从配置服务器读取拆分为多个属性文件的动态配置

问题描述

我想以此模式添加数百个配置文件

/application.yaml
/1000/application.yaml
/2000/application.yaml
/3000/application.yaml
/4000/application.yaml

其中1000、2000、3000是发件人代码。当对我的API进行REST调用时,它将具有一个参数'senderCode',其值类似于1000、2000等

基于此,我想从配置服务器读取对应的application.yaml的配置。

我的配置服务器的应用程序yaml具有:

  cloud:
    config:
      server:
        git:
          uri: http://example.com/my-configurations
          search-paths: 1000,1001,1002,1003

使用上述设置,我可以像这样在客户端应用程序中配置属性

@ConfigurationProperties(prefix = "1000")
public class CodeBasedConfig {
    String senderName;
    String senderSource;
}

但这意味着要像上面的文件那样创建数千个文件。我希望能够将多个文件中的配置加载到这样的映射中:

键:1000
值:1000的配置
密码:2000
值:2000的配置

顺便说一句,如果我可以按需读取给定发件人的配置,而不是预先全部加载它们,那将是一个好处。

解决方法

我稍微改变了文件结构,但是将它们分组在一个名为clients的文件夹下

    ||=== Build: Debug in aa (compiler: GNU GCC Compiler) ===|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|55|undefined reference to `SDL_calloc'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|60|undefined reference to `SDL_wcslen'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|60|undefined reference to `SDL_iconv_string'|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `OutOfMemory':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|28|undefined reference to `SDL_ShowSimpleMessageBox'|
E:\SDL2\SDL2-2.0.12\i686-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|68|undefined reference to `SDL_SetMainReady'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|75|undefined reference to `SDL_free'|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x86\..\src\main\windows\SDL_windows_main.c|77|undefined reference to `SDL_free'|
||error: ld returned 1 exit status|
||=== Build failed: 8 error(s),0 warning(s) (0 minute(s),0 second(s)) ===|

此后,我像这样更新了Spring Cloud Server的searchPaths:

/application.yaml
clients/1000/application.yaml
clients/2000/application.yaml
clients/3000/application.yaml
clients/4000/application.yaml

通过此配置服务器应该读取“ clients”文件夹下的所有application.yaml并将它们合并为统一配置。

仅供参考,我的application.yaml在client文件夹下的内容如下:

spring:
  cloud:
    config:
      server:
        git:
          uri: <my git url>
          username: ${git_username}
          password: ${git_password}
          label: develop
          searchPaths: clients/*

当配置服务器将这些配置提供给客户端时,以上配置显示为:

1000/application.yaml
clients:
  '1000':
     attribute1: value1
     attribute2: value2

2000/application.yaml
clients:
  '2000':
     attribute1: value1
     attribute2: value2

这样,我可以将大型配置分解/重构为更易于维护的配置文件