基于配置文件的春季云配置服务器客户端具有可选的本地开发人员属性

发布于 2025-02-10 03:23:30 字数 3910 浏览 3 评论 0 原文

我的目标是:

让Spring Cloud Config Server导入活动,并为开发人员提供了一种在计算机上使用可选属性文件的方法。

我的配置服务器使用 org.springframework.cloud:spring-cloud-config-server:3.1.3 @enableconfigserver 在主班上。 HTTP向混凝土端点的请求产生预期的结果。该服务器应为其本地设置的开发人员提供重要的环境配置。

$ curl http://localhost:8888/test-application/dev
{"name":"test-application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cfg/test-application/application-dev.yml","source":{"server.port":1111}},{"name":"classpath:/cfg/test-application/application.yml","source":{"server.port":8000}}]}

其中 localhost:8888 是我的配置服务器, test-application ist客户端应用程序的名称(通过 spring.application.name.name )。提供的 dev 是客户端上的当前活动配置文件(DEV配置文件表示本地运行的软件,没有Dev环境)。

客户端配置:

application.yml

spring:
  application:
    name: test-application
  config:
    import:
      - configserver:http://localhost:8888                  # <- pull the configuration from the configserver
      - optional:file:/absolute/path/to/the/project/root/   # <- if there are any additional configuration files, use them

客户端使用以下依赖项:

  • org.springframework.boot:spring-boot-starter-web:2.7.0
  • org.springframework.cloud.cloud:spring-cloud-cloud---------启动器-bootstrap:3.1.3
  • org.springframework.cloud:spring-cloud-starter:3.1.3
  • org.springframework.cloud.cloud:spring-cloud-starter-starter-starter-starter-config: 3.1.3

如上所示,“基本” application.yml 已配置的 server.port = 8000 其中配置文件特定于 server。端口= 1111 。阅读文档此行为是正确的。但是我本地的开发人员配置包含 server.port = 2222 。这被忽略了。

问题出现了:

启动客户端应用程序时,我可以看到以下日志语句:

Fetching config from server at : http://localhost:8888
Located environment: name=test-application, profiles=[default], label=null, version=null, state=null
Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application-dev.yml'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application.yml'}]
The following 1 profile is active: "dev"
Tomcat initialized with port(s): 1111 (http)
Initializing Spring embedded WebApplicationContext
Tomcat started on port(s): 1111 (http) with context path ''
Started TestApplicationKt in 2.234 seconds (JVM running for 2.762)

Spring的配置评估结果是选择端口 1111 ,而不是通缉 2222 application-dev.yml 位于项目root(开发人员配置)中。


包装:

三个配置文件:

  1. config Server application.yml (端口:8000)
  2. 配置服务器 application-dev.yml (端口:1111)
  3. 项目root Developer config application-dev.yml (端口:2222)&lt; - 我希望此文件比其他两个具有优先权。

运行调试器时,我看到这些注入环境 bean中找到的属性源。所需的开发人员文件在此列表中,属性源内容正确( server.port = 2222 )。

有人想知道如何解决此问题?

我创建了一个重现这种确切行为的项目。 链接到github

提前致谢!

My Goal:

Having the Spring Cloud Config Server import active and provide a way for developers to have an optional property file on their machine.

wanted cfg prio diagram

My Config Server is up and running using org.springframework.cloud:spring-cloud-config-server:3.1.3 and @EnableConfigServer on the main class. Http requests to the concrete endpoint yield the expected result. This server should provide important environment configurations for a developer for his/her local setup.

$ curl http://localhost:8888/test-application/dev
{"name":"test-application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/cfg/test-application/application-dev.yml","source":{"server.port":1111}},{"name":"classpath:/cfg/test-application/application.yml","source":{"server.port":8000}}]}

Where localhost:8888 is my Config Server and test-application ist the name of the client application (defined via spring.application.name). The provided dev is the currently active profile on the client (The dev profile indicates a locally running software, there is no dev environment).

Clients configuration:

application.yml

spring:
  application:
    name: test-application
  config:
    import:
      - configserver:http://localhost:8888                  # <- pull the configuration from the configserver
      - optional:file:/absolute/path/to/the/project/root/   # <- if there are any additional configuration files, use them

The client uses the following dependencies:

  • org.springframework.boot:spring-boot-starter-web:2.7.0
  • org.springframework.cloud:spring-cloud-starter-bootstrap:3.1.3
  • org.springframework.cloud:spring-cloud-starter:3.1.3
  • org.springframework.cloud:spring-cloud-starter-config:3.1.3

As shown above, the "base" application.yml configured server.port=8000 where the profile specific is set to server.port=1111. When reading the documentation this behaviour is correct. But my local developer configuration contains server.port=2222. This was ignored.

Here comes the problem:

When starting the client application, i can see the following log statements:

Fetching config from server at : http://localhost:8888
Located environment: name=test-application, profiles=[default], label=null, version=null, state=null
Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application-dev.yml'}, BootstrapPropertySource {name='bootstrapProperties-classpath:/cfg/test-application/application.yml'}]
The following 1 profile is active: "dev"
Tomcat initialized with port(s): 1111 (http)
Initializing Spring embedded WebApplicationContext
Tomcat started on port(s): 1111 (http) with context path ''
Started TestApplicationKt in 2.234 seconds (JVM running for 2.762)

The configuration evaluation result from spring was to choose port 1111 instead of the wanted 2222 within the application-dev.yml located in the project root (developer config).


Wrapped up:

Three config files:

  1. config server application.yml (port: 8000)
  2. config server application-dev.yml (port: 1111)
  3. project root developer config application-dev.yml (port: 2222) <- I want this file to have precedence over the other two.

When running the debugger, i see these found property sources within the injected Environment bean. The wanted developer file is within this list and the property source content is correct (server.port=2222).

Environment runtime snapshot

Does anyone have an idea how to solve this problem?

I have created a project that reproduces this exact behaviour. Link to GitHub.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅人 2025-02-17 03:23:30

当应用程序在开发环境中运行时, dev 配置文件是活动的吗?还是在本地运行时也有效?
理想情况下,您应该有2个不同的DEV和本地配置文件,并且只有在 dev 配置文件处于活动状态时才能启用Config Server。
您可以将本地属性文件重命名为 application-local.yml ,并在profile local 处于活动状态时禁用Spring Cloud Config。
中删除配置服务器配置。

---
spring:
  application:
    name: test-application
  config:
    activate:
      on-profile: dev
    import:
      - configserver:http://localhost:8888                  # <- pull the configuration from the configserver
      - optional:file:/absolute/path/to/the/project/root/   # <- if there are any additional configuration files, use them
---
spring:
  application:
    name: test-application
  config:
    activate:
      on-profile: local
  cloud:
    config:
      enabled: false

您可以在 bootstrap.yml 中放置以下代码(在 application.yml )中创建新文件,然后从 application.yml 编辑:实际修复

感谢您共享可重复的示例。
您需要做的就是从客户端 application.yml 中删除本地 application-dev.yml 位置参考显示在Belolow屏幕截图中。

/客户端/src/main/resources/application.yml

,然后将其放入configserver application.yml 结束时。订单很重要,因此请确保将压倒位置放在最后。

/ configserver /src/main/resources/application.yml

构建并启动两个应用程序,您将看到客户端应用程序服务器在端口2222上启动

Is the dev profile active when application is running in dev environment or it's also active when running locally?
Ideally you should have 2 different profiles for dev and local and you can enable config server only when dev profile is active.
You can rename your local property file as application-local.yml and disable spring cloud config when profile local is active.
You can put below code in bootstrap.yml(create new file beside application.yml) and remove config server configurations from application.yml

---
spring:
  application:
    name: test-application
  config:
    activate:
      on-profile: dev
    import:
      - configserver:http://localhost:8888                  # <- pull the configuration from the configserver
      - optional:file:/absolute/path/to/the/project/root/   # <- if there are any additional configuration files, use them
---
spring:
  application:
    name: test-application
  config:
    activate:
      on-profile: local
  cloud:
    config:
      enabled: false

EDIT: Actual fix

Thanks for sharing reproducible example.
All you need to do is remove local application-dev.yml location reference from client application.yml and add it to configServer application.yml as shown in belolow screenshots.

/client/src/main/resources/application.yml
enter image description here

And put it in configServer application.yml at the end of search-locations. Order matters so please make sure you put overriding location/s at the end.

/configServer/src/main/resources/application.yml
enter image description here

Build and start both the applications and you will see client application server started on port 2222
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文