java - Spring Cloud Config Server 不适用于 Docker 构建
问题描述
您好,感谢您的关注!我在尝试使用 Git 对 Spring Cloud Config Server 进行 Dockerize 来管理我的微服务时遇到了问题。当它只是 Spring Cloud 微服务上下文时,我的应用程序工作正常,但是当我为此配置服务器创建 Dockerfile 并构建它时,我得到了下一个错误:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' 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.HealthContributorRegistry]: Factory method 'healthContributorRegistry' 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.
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' 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.
Caused by: 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.
Caused by: 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.
Caused by: 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.
Caused by: java.lang.IllegalStateException: You need to configure a uri for the git repository.
但是我的 bootstrap.yml 中有一个 gitRepo,当我在没有 Dockerfile 的情况下使用它时它可以工作:
spring:
application:
name: CONFIG-SERVER
profiles:
active:
- git
cloud:
config:
server:
git:
default-label: main
uri: https://github.com/hideyourname/cloud-config-server.git
Soo ...我认为我创建 Dockerfile 的方式存在问题,因为即使存在它也可以找到我的 git
FROM openjdk:11 as BUILDER
LABEL maintainer = "petrea config server"
#EXPOSE 8080 SA VEDE MDACA MERGE
COPY /target/boys-config.jar boys-config.jar
RUN mkdir -p target/dependency && (cd target/dependency; jar -xf /boys-config.jar)
#Stage 2
FROM openjdk:11-slim
VOLUME /tmp
ARG DEPENDENCY=/target/dependency
COPY --from=BUILDER ${DEPENDENCY}/BOOT-INF/lib /boys-config/lib
COPY --from=BUILDER ${DEPENDENCY}/META-INF /boys-config/META-INF
COPY --from=BUILDER ${DEPENDENCY}/BOOT-INF/classes /boys-config
#execute the application
ENTRYPOINT ["java", "-cp", "boys-config:boys-config/lib/*", "com.petrea.boys-config-server.BoysConfigServerApplication"]
我在 POM 中有 Cloud Config Server 依赖项,在我的主类中有 @EnableConfigServer,但是由于某种原因,当我使用 Dockerfile 构建这个图像时,它失败了......请问有什么想法吗?
解决方案
如果您遵循上面引用的书中的示例,我建议您将以下代码段从 bootsrap.yml 移动到 applications.yml。
spring:
application:
name: CONFIG-SERVER
profiles:
active:
- git
cloud:
config:
server:
git:
default-label: main
uri: https://github.com/hideyourname/cloud-config-server.git
这就是我所做的,它对我有用。
推荐阅读
- java - 使用 MS SQL 数据库 IntegratedSecurity=true 的 Spring Boot Maven 项目配置
- dijkstra - 当我尝试在 Dijkstra 算法中添加父数组时如何解决这个错误的访问问题
- proxy - Istio 上的 Jgroups
- mysql - MySQL 8.0.23.0 更改类型和网络
- sql - 需要帮助求和/分组值
- svn - HPUX Subversion 客户端不再连接到远程存储库
- chef-infra - 试图配置 ruby 环境,但我发出命令 echo 'evaluation "$(chef shell-init bash)"' >> ~/.bash_profile 它什么也没做
- postgresql - Spring Data JPA with Postgres 动态修改列问题
- html - 是否可以使用 jinja2 在模态中注入 html 文件?
- typescript - 如何在 Vue.js 中获取当前活动路由?