我想为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文件对这两个配置文件正确吗?我有没有错过任何可同时在两个个人资料上使用的东西?
答案 0 :(得分: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/moby/moby/pull/30383#issuecomment-314797629
由于在特定情况下Spring Boot支持多个配置文件,因此我建议使用Spring Boot的“包括”附加配置文件功能。参见: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
基本上,您的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)