gpt4 book ai didi

java - Spring Cloud 配置模式匹配不适用于 SVN

转载 作者:行者123 更新时间:2023-12-01 09:51:42 31 4
gpt4 key购买 nike

我在为 Spring Cloud 配置服务器定义多个基于 svn 的配置存储库时遇到问题。我已经设置了三个配置存储库。一个用于开发、单元和生产。我已将默认设置设置为开发(通过设置 spring.cloud.config.server.svn.uri =development repo uri)。但是,每当我向配置服务器的 REST 端点发出 GET 请求时,无论我请求哪个配置文件,我总是会获取开发配置。请参阅下面的示例...

例如:

curl -i -X GET \
-H "Content-Type:application/json" \
'http://localhost:8888/my-service-accounts/unit'

结果:

{
"name":"my-service-accounts",
"profiles":[
"unit"
],
"label":null,
"version":"750",
"propertySources":[
{
"name":"http://PATH_TO_MY_SVN_SERVER/config-repo-development/trunk/my-service-accounts.yml",
"source":{
"server.port":8080,
"hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds":3000
}
}
]
}

但我预计...

{
"name":"my-service-accounts",
"profiles":[
"unit"
],
"label":null,
"version":"750",
"propertySources":[
{
"name":"http://PATH_TO_MY_SVN_SERVER/config-repo-unit/trunk/my-service-accounts.yml",
"source":{
"server.port":7777,
"hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds":3000
}
}
]
}
  • 请注意 propertySources[0].name 值的差异。我希望此配置来自单元存储库,但它仍然来自开发存储库。
<小时/>

我的配置服务器配置:

application.yml

server:
port: 8888
spring:
profiles:
include: subversion
cloud:
config:
server:
svn:
username: configserver
password: ************
uri: http://PATH_TO_MY_SVN_SERVER/config-repo-development
repos:
development:
pattern: ["*/development"]
uri: http://PATH_TO_MY_SVN_SERVER/config-repo-development
unit:
pattern: ["*/unit"]
uri: http://PATH_TO_MY_SVN_SERVER/config-repo-unit
production:
pattern:
- '*/production'
uri: http://PATH_TO_MY_SVN_SERVER/config-repo-production

discovery:
enabled: true
application:
name: my-server-config
  • 注意:我的 IDE (IntelliJ) 警告我它无法解析 spring.cloud.config.server.svn.repos.*.uri 的配置属性...但这就是 Spring Cloud Config 的方式文档展示了如何指定存储库路径。

构建.gradle

buildscript {
ext {
springBootVersion = "1.3.3.RELEASE"
}
repositories {
mavenCentral()
maven {url "https://plugins.gradle.org/m2/"}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.5.RELEASE")
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.1.1"
}
}

apply plugin: "base"
apply plugin: "maven"
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'

jar {
baseName = project.ext.projectName
version = project.ext.projectVersion
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.RC1"
}
}

dependencies {
compile("org.springframework.cloud:spring-cloud-starter-config")
compile("org.springframework.cloud:spring-cloud-config-server")
compile("org.springframework.cloud:spring-cloud-starter-eureka")
compile("org.tmatesoft.svnkit:svnkit")

testCompile("org.springframework.boot:spring-boot-starter-test")
}

eclipse {
classpath {
containers.remove("org.eclipse.jdt.launching.JRE_CONTAINER")
containers "org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"
}
}

task wrapper(type: Wrapper) {
gradleVersion = "2.12"
}

最佳答案

我最终重新组织了 svn 配置存储库的目录布局,并使用更灵活的“searchPaths”属性来实现我想要的功能。

新的 SVN 存储库布局:

  • /svn/config/trunk/dev/service-name.yml
  • /svn/config/trunk/prod/service-name.yml
  • /svn/config/trunk/unit/service-name.yml

基本上,使用这种方法,您可以定义配置存储库 URI,然后定义一个 searchPaths 属性,该属性对传入路径进行模式匹配,以确定在哪个目录中搜索配置。

新的application.yml

server:
port: 8888
spring:
profiles:
include: subversion
cloud:
config:
server:
svn:
username: configserver
password: ************
uri: http://PATH_TO_MY_SVN_SERVER/svn/config
searchPaths: ["{profile}"]
discovery:
enabled: true
application:
name: my-server-config

通过 HTTP 端点访问配置服务器:

curl -i -X GET \
-H "Content-Type:application/json" \
'http://localhost:8888/my-service-name/unit'

默认情况下的配置服务器似乎匹配

svn_server/{application-name}/{pattern_defined_in_searchPaths}

就我而言是:

svn_server/{application-name}/{profile}

关于java - Spring Cloud 配置模式匹配不适用于 SVN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555750/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com