首页 > 解决方案 > Spring could config server 目录组织与 searchPaths 不起作用

问题描述

我工作的公司决定将 SpringBoot 项目的所有配置属性集中在一个地方。为此,我们配置了spring-cloud-config-server指向 Github 存储库的指向。

在 github 上,我们为我们拥有的每个应用程序创建了一个文件夹。例子:

-https://github.com/myusername/my-spring-cloud-config-repo (Github's base folder)
    --my-project-1 (folder)
        ---my-project-1.properties (file)
        ---application-dev.properties (file)
        ---application-prod.properties (file)
    --my-project-2 (folder)
        ---my-project-2.properties (file)
        ---application-dev.properties (file)
        ---application-prod.properties (file)
        ---application-test.properties (file)

最初,application.properties我们spring-cloud-config-server有以下配置:

server.port=9090
spring.cloud.config.server.git.uri=https://github.com/myusername/my-spring-cloud-config-repo.git
spring.cloud.config.server.git.username=myusername
spring.cloud.config.server.git.password=mypassword
spring.cloud.config.server.git.skip-ssl-validation=true
spring.cloud.config.server.git.clone-on-start=true
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.searchPaths=*/**

因此,一旦我们启动服务器,我们就可以发送 HTTP 请求:

http://localhost:9090/my-project-2/test

它会成功返回文件夹内外的所有配置my-project-2.properties,这是所需的行为。 当我们发送时,问题就开始了:application-test.propertiesmy-project-2

http://localhost:9090/my-project-2/dev  

因为它会返回文件夹内外的所有配置my-project-2.properties,也会返回文件夹中的文件。发生这种情况是因为强制配置服务器在存储库的所有文件夹上搜索“dev”文件,并且每次客户端启动时最终都会导致冲突。application-dev.propertiesmy-project-2application-dev.propertiesmy-project-1searchPaths=*/**

为了解决这个问题,根据HereHere,我可以wildcardssearchPaths配置中使用:

spring.cloud.config.server.git.searchPaths={application}/{profile}

但它没有用......我也尝试了很多组合:

searchPaths={application}/{profile}.properties
searchPaths={application}/**
searchPaths=/{application}/{profile}
searchPaths='{application}/{profile}'
searchPaths='{application}/{profile}.properties'
searchPaths='{application}'/'{profile}'

(我根据spring cloud config searchPaths的答案尝试了单引号的想法)

但他们都没有为我工作。我应该如何配置该searchPaths属性以使其用于我的目的?提前致谢。

标签: javaspringspring-bootspring-cloudspring-cloud-config

解决方案


推荐阅读