spring 云配置多个配置文件
spring cloud config multiple profiles
我想为我的 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 云配置服务器的默认行为是 git
配置文件的逻辑。所以它试图找到你没有的 属性 spring.cloud.config.server.git.uri
。
要解决您的问题,您需要为这两种情况启用 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);
}
}
......
由于 Spring Boot 在您的特定情况下支持多个配置文件,我建议使用 Spring Boot 的 "including" 附加配置文件功能。参见: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)
即可启用活动配置文件
我想为我的 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 云配置服务器的默认行为是 git
配置文件的逻辑。所以它试图找到你没有的 属性 spring.cloud.config.server.git.uri
。
要解决您的问题,您需要为这两种情况启用 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);
}
}
......
由于 Spring Boot 在您的特定情况下支持多个配置文件,我建议使用 Spring Boot 的 "including" 附加配置文件功能。参见: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)