【问题标题】:Disable Spring Cloud Server Config using profile?使用配置文件禁用 Spring Cloud Server Config?
【发布时间】:2015-08-25 09:43:28
【问题描述】:

我想实现以下目标:

  • 当处于“开发”模式时,将 Spring Cloud Config 嵌入到当前 webapp 中
  • 处于'dev'模式时,连接到已经运行的Spring Cloud Config服务器实例

因此,我当前 webapp 的类路径包含对配置服务器 客户端的依赖项:

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency> 

在开发模式下,并且在bootstrap.yml中具有以下属性,就可以了(嵌入式配置服务器已配置并启动)

spring:
  application:
    name: app1
  profiles:
    active: dev
  cloud:
    config:
      uri: ${SPRING_CONFIG_URI:http://localhost:8888}

---

spring:
  profiles: dev
  cloud:
    config:
      server:
        bootstrap: true
        git:
          uri: https://github.com/nocquidant/my-config-repo.git
          basedir: target/config

当不处于“开发”模式时(例如 spring.profiles.active = prod),当前的 webapp 不会启动:它无法自动装配我的属性(我猜嵌入式服务器是在错误的配置下启动的)

如果我在 POM 中注释 spring-cloud-config-server 依赖项,那么它可以工作。

我认为我可以使用以下方法实现我想要的,但没有(实际上是否使用 EnableConfigServer 似乎并没有改变任何东西):

@Configuration
@Profile("dev")
@EnableConfigServer
public static class EmbeddedConfigServer { }

我该怎么做(不使用 Maven 配置文件)? 有没有一种简单的方法来禁用 spring 云配置服务器? 有没有更好的方法来实现我的目标?

任何帮助表示赞赏。

附:使用的版本:

<parent>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-parent</artifactId>
  <version>1.0.3.BUILD-SNAPSHOT</version>
</parent>

// 尼克

// --- 编辑#1

请看github上的一个简单项目:https://github.com/nocquidant/exchange/tree/master/poc-spring-cloud/

它包含 2 个简单的模块:config-server 和 client-app。

首先,如果您从 client-app 模块启动 Client 类,一切正常(嵌入式模式已开启 => 请参阅 bootstrap.yml 中的开发配置文件)

然后,启动 config-server(ConfigServer 类)。然后在客户端应用程序中为“prod”(或其他)更改“spring.profiles.active”并重新启动它:属性注入不再起作用(并且 webapp 拒绝启动)。

Caused by: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'server' on field 'port': rejected value [${taap.bak.config.port}];

因此它无法连接到 config-server 模块。如果你注释 maven 依赖项:来自 client-app 模块的 spring-cloud-config-server,它会再次工作。

希望现在更清楚了。 谢谢,

【问题讨论】:

  • 我原以为@Profile("dev") 会起作用。您看到的错误是什么?
  • 我刚刚编辑了我的帖子以使其更清晰。谢谢。

标签: spring-cloud


【解决方案1】:

根据spring cloud config server中的this class:除非您从系统属性(-D)或通过SpringApplicationBuilder设置spring.cloud.config.enabled=true,否则配置服务器会显式禁用配置客户端。这是目前使其正常工作的唯一方法。欢迎您提交增强请求,解释您的用例。

【讨论】:

  • 我确实看到并尝试在我的 bootstrap.yml(用于配置文件!dev)中设置此属性,但没有成功。我刚刚尝试从系统属性 (-D) 设置它,它按预期工作。谢谢!附言使用SpringApplicationBuilder 似乎不起作用...
猜你喜欢
  • 2017-08-23
  • 2016-02-27
  • 2020-04-21
  • 2017-06-23
  • 1970-01-01
  • 2016-12-22
  • 2022-12-04
  • 2016-07-06
  • 2020-07-11
相关资源
最近更新 更多