spring-cloud-config - spring cloud 配置多个配置文件

标签 spring-cloud-config

我想为我的 spring_cloud_config_server 设置多个配置文件。

这是我的 yml 文件:

server:
  port: "2000"

spring:
  profiles:
    active: native
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: file:///opt/app/configuration

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8260/eureka

logging:
  level:
    org:
      springframework: INFO
---
spring:
  profiles: docker
  application:
    name: config-server
  cloud:
    config:
      server:
        native:
          search-locations: file:/opt/app/configuration

server:
  port: "2000"

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8260/eureka

logging:
  level:
    org:
      springframework: INFO

当我使用以下命令使用“ native ”配置文件运行应用程序时

java -jar app.jar

java -jar -Dspring.profiles.active=native app.jar

应用运行良好。 当我使用以下命令使用“docker”配置文件运行应用程序时

java -jar -Dspring.profiles.active=docker app.jar

应用程序退出并出现异常:

ERROR o.s.b.w.e.tomcat.TomcatStarter - Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.cloud.config.server.config.CompositeConfiguration': Unsatisfied dependency expressed through method 'setEnvironmentRepos' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: You need to configure a uri for the git repository.

根本原因是:

Exception: You need to configure a uri for the git repository.

我的 yml 文件对于这两个配置文件是否正确?我是否遗漏了任何使它适用于两种配置文件的东西?

最佳答案

Spring Cloud Config Server 的默认行为是 git 配置文件的逻辑。所以它试图找到你没有的属性 spring.cloud.config.server.git.uri

要解决您的问题,您需要为这两种情况启用native 配置文件。 native 配置仅在“ native ”配置文件处于事件状态时才开始工作:

public class EnvironmentRepositoryConfiguration { 
......
@Configuration
@ConditionalOnMissingBean(EnvironmentRepository.class)
@Profile("native")
class NativeRepositoryConfiguration {

  @Bean
  public NativeEnvironmentRepository 
     nativeEnvironmentRepository(NativeEnvironmentRepositoryFactory factory,
        NativeEnvironmentProperties environmentProperties) {
    return factory.build(environmentProperties);
  }
}
......

在这里查看更多: https://github.com/spring-cloud/spring-cloud-config/blob/master/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java#L215

由于 Spring Boot 在您的特定情况下支持多个配置文件,因此我建议使用 Spring Boot 的“包括”附加配置文件功能。请参阅:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-adding-active-profiles

基本上您的 application.yml 配置将如下所示:

spring.profiles: some-profile-1
spring.profiles.include:
  - native
# specific configuration for 'some-profile-1'

---

spring.profiles: some-profile-2
spring.profiles.include:
  - native
# specific configuration for 'some-profile-2'

您只需传递 -Dspring.profiles.active=some-profile-1(或 2)

即可启用事件配置文件

关于spring-cloud-config - spring cloud 配置多个配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53547033/

相关文章:

cloud-foundry - Spring Cloud Config Server 加密问题

java - Bootstrap.properties中Spring.application.name的意义

java - 使用 ApplicationContextInitializer 进行 Spring 配置测试 - 如何解密?

spring-boot - 来自 docker 的 Consul 共享网络

Spring Cloud Config 客户端不从服务器获取值

spring-boot - 多个微服务的通用应用程序属性文件

spring - 使用保管库保护 Spring Cloud 配置凭据

java - spring boot,cloud spring配置错误,为什么这不起作用

当 spring-cloud-config 服务器未准备好时,spring-cloud-config 客户端无法启动

spring-boot - 如何使用 env 特定配置创建 Spring Cloud Config Client?