首页 > 解决方案 > 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 构建这个图像时,它失败了......请问有什么想法吗?

标签: javagitdockerdockerfilespring-cloud

解决方案


如果您遵循上面引用的书中的示例,我建议您将以下代码段从 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

这就是我所做的,它对我有用。


推荐阅读