ker容器上运行ConfigServer和EurekaServe

ker容器上运行ConfigServer和EurekaServe

本文介绍了在Docker容器上运行ConfigServer和EurekaServer时无法获取配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Spring boot Microservies]

[Spring boot Microservies]

我有一个微服务,其中包括2个服务:ConfigService和DiscoveryService

I have a microservices includes 2 services: ConfigService and DiscoveryService

  • ConfigService 已启用ConfigServer,请为微服务保留文件配置
  • DiscoveryService 是EurekaServer.它将从ConfigService获取配置文件
  • ConfigService is enabled ConfigServer, keep files config for microservice
  • DiscoveryService is EurekaServer. It will get config file from ConfigService

在本地(不是docker)上运行2服务时,一切都很好

When run 2 service on local (not docker), everything is good

Fetching config from server at: http://localhost:8088
Located environment: name=epl-discovery-service, profiles=[default], label=null, version=3f6887b5b355381341e02ad03615f2415d6a566d, state=null
Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/stomer90/epl-config-server.git/epl-discovery-service.yml'}]}
No active profile set, falling back to default profiles: default

但是在2个容器(泊坞窗)上运行2个服务时,ConfigService运行正常,但是DiscoveryService出现一些错误(无法连接到ConfigService)

but when run 2 service on 2 container (docker), ConfigService run normal, but DiscoveryService have some error (can not connect to ConfigService)

Fetching config from server at: http://localhost:8088
Could not locate PropertySource: I/O error on GET request for "http://localhost:8088/epl-discovery-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
No active profile set, falling back to default profiles: default

  • ConfigService
    • ConfigService
    • EplConfigServiceApplication.java

      EplConfigServiceApplication.java

      @SpringBootApplication
      @EnableConfigServer
      public class EplConfigServiceApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(EplConfigServiceApplication.class, args);
          }
      }
      

      bootstrap.yml

      bootstrap.yml

      server:
        port: 8088
      
       spring:
        application:
          name: eplconfigserver
      
        cloud:
          config:
            server:
              git:
                uri: https://github.com/stomer90/epl-config-server.git
      

      Dockerfile

      Dockerfile

      FROM openjdk:8-jdk-alpine
      
      MAINTAINER Phong Nguyen
      
      VOLUME /tmp
      
      # Add Spring Boot app.jar to Container
      ADD ./target/epl-config-service-0.0.1-SNAPSHOT.jar app.jar
      
      RUN sh -c 'touch /app.jar'
      
      ENV JAVA_OPTS=""
      ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar " ]
      

      * DiscoveryService

      EplDiscoveryServiceApplication.java

      EplDiscoveryServiceApplication.java

      @SpringBootApplication
      @EnableEurekaServer
      public class EplDiscoveryServiceApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(EplDiscoveryServiceApplication.class, args);
          }
      }
      

      bootstrap.yml

      bootstrap.yml

      spring:
        application:
          name: epl-discovery-service
        cloud:
          config:
            uri: http://localhost:8088
      

      Dockerfile

      Dockerfile

      FROM openjdk:8-jdk-alpine
      
      MAINTAINER Phong Nguyen
      
      VOLUME /tmp
      
      # Add Spring Boot app.jar to Container
      ADD ./target/epl-discovery-service-0.0.1-SNAPSHOT.jar app.jar
      
      RUN sh -c 'touch /app.jar'
      
      ENV JAVA_OPTS=""
      ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
      

      • Docker-compose.yml
        • Docker-compose.yml
        • version: '3.1'
          
          services:
            epl-config-service:
              build: ./epl-config-service
              ports:
                - "8088:8088"
              restart:
                unless-stopped
          
          
            epl-discovery-service:
              build: ./epl-discovery-service
              ports:
                - "8061:8061"
              environment:
                - REGISTRY_HOST=epl-config-service
              depends_on:
                - epl-config-service
              restart:
                unless-stopped
          

          链接源代码: https://github.com/stomer90/epl-spring -cloud-microservice

          请帮助我解决此问题

          推荐答案

          因此,您已正确指定了容器启动的顺序,但这不能保证先前的容器(在您的情况下,配置服务器是否正常) ),因为您的docker-compose版本是3.1您可以在撰写文件中定义健康检查

          So you have correctly specified the order in which container needs to be started but this doesn't guarantee that previous container (in your case config server is healthy or not) since ur docker-compose version is 3.1you can define healthchecks in your compose file

          例如:

           registry:
              build:
                context: ../service-registry
                dockerfile: Dockerfile
              container_name: registry
              links:
                - configuration-server
              depends_on:
                configuration-server:
                   condition: service_healthy
          

           configuration-server:
          build:
            context: ../configuration-server
            dockerfile: Dockerfile
          image: xyz/configuration-server
          container_name: configuration-server
          environment:
            - SPRING_PROFILES_ACTIVE=dev
            - SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKER=kafka
            - SPRING_CLOUD_STREAM_KAFKA_BINDER_BROKER_PORT=9092
            - SPRING_CLOUD_STREAM_KAFKA_BINDER_ZKNODE=zookeeper
            - SPRING_CLOUD_STREAM_KAFKA_BINDER_ZKPORT=2181
          depends_on:
            kafka:
              condition: service_healthy
            zookeeper:
              condition: service_healthy
          healthcheck:
              test: "exit 0"
          

          请注意将从注册表服务器调用的配置服务器中的运行状况检查(条件:服务运行状况良好)您可以对更复杂的

          notice the healthcheck in config server which would be invoked from registry server (condition:service healthy)you can implement your own custom healthcheck to something more sophesticated like

          healthcheck:
            test: ["CMD", "curl", "-f", "http://localhost"]
            interval: 1m30s
            timeout: 10s
            retries: 3
            start_period: 40s
          

          请参阅: https://docs.docker .com/compose/compose-file/compose-file-v2/#healthcheck

          我想应该足以启动您的容器,让我知道它是否有效

          i guess that should be enough to kickstart your containers, let me know if it works

          这篇关于在Docker容器上运行ConfigServer和EurekaServer时无法获取配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:42