【问题标题】:spring cloud config multiple profilesspring cloud 配置多个配置文件
【发布时间】:2018-11-29 20:26:16
【问题描述】:

我想为我的 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

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

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


    【解决方案1】:

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

    要解决您的问题,您需要为这两种情况启用 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 (or 2) 启用活动配置文件

    【讨论】:

      猜你喜欢
      • 2021-04-18
      • 2019-03-28
      • 2016-12-24
      • 1970-01-01
      • 2016-03-21
      • 2020-09-20
      • 2016-07-12
      • 2022-10-19
      • 2016-07-29
      相关资源
      最近更新 更多