Spring Cloud Config Server 不适用于 Docker 构建

Posted

技术标签:

【中文标题】Spring Cloud Config Server 不适用于 Docker 构建【英文标题】:Spring Cloud Config Server doens't work with Docker build 【发布时间】:2021-05-23 08:22:06 【问题描述】:

您好,感谢您的关注!我在尝试使用 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 构建此映像时,它失败了……请问有什么想法吗?

【问题讨论】:

你用的是什么版本? 您好,感谢您的回答!我的 Docker 版本是 19.03.1,构建 74b1e89e8a,我使用的是 Windows 8.1 Pro,Spring Cloud 版本:Hoxton.SR1,我的项目应用程序中的 java 11 编辑:我关注这本书/github中的项目...github.com/ihuaylupo/manning-smia/tree/master/chapter5/docker @spencergibb 这是 Spring Boot 版本 :) 就为了这个。我在 STS 上使用默认提供的 2.4.4 快照,并查看以前的项目,我正在使用 2.3.8,用 2.3.8 替换 2.4.4,现在它正在工作!哇哇哇哇! 【参考方案1】:

如果您按照上面引用的书中的示例进行操作,我建议您将下面的 sn-p 从 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

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

【讨论】:

以上是关于Spring Cloud Config Server 不适用于 Docker 构建的主要内容,如果未能解决你的问题,请参考以下文章

spring-cloud-starter-eureka-server 和 spring-cloud-starter-netflix-eureka-server的区别

spring-cloud-config:spring-cloud-config Maven配置错误

spring-cloud-config-server

无法通过 spring.cloud.config.enabled:false 禁用 Spring Cloud Config

0701-spring cloud config-简介Config Server开发Config Client开发

问题 spring-cloud-config 和 spring-cloud-bus