使用配置文件禁用 Spring 云服务器配置?

Disable Spring Cloud Server Config using profile?

我想实现以下目标:

因此我的 class 当前 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

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

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

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

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

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

感谢任何帮助。

P.S。使用的版本:

<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。

首先,如果您从客户端应用程序模块启动客户端 class,一切正常(嵌入式模式已开启 => 请参阅 bootstrap.yml 中的开发配置文件)

然后,启动配置服务器 (ConfigServer class)。然后在客户端应用程序中将 'spring.profiles.active' 更改为 'prod'(或其他)并重新启动它:属性 注入不再起作用(并且 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}];

因此无法连接到配置服务器模块。如果您评论 Maven 依赖项:来自 client-app 模块的 spring-cloud-config-server,它会再次运行。

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

根据 spring 云配置服务器中的 this class:配置服务器明确禁用配置客户端,除非您从系统 属性 设置 spring.cloud.config.enabled=true-D ) 或通过 SpringApplicationBuilder。这是目前让它发挥作用的唯一方法。欢迎您提交增强请求,解释您的用例。