【问题标题】:Can't get config files when run ConfigServer and EurekaServer on docker container在 docker 容器上运行 ConfigServer 和 EurekaServer 时无法获取配置文件
【发布时间】:2018-10-14 03:58:21
【问题描述】:

[Spring Boot 微服务]

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

  • ConfigService 已启用 ConfigServer,为微服务保留文件配置
  • DiscoveryService 是 EurekaServer。它将从 ConfigService 中获取配置文件

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

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 个容器(docker)上运行 2 个服务时,ConfigService 运行正常,但是 DiscoveryService 有一些错误(无法连接到 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
  • 配置服务

EplConfigServiceApplication.java

块引用

@SpringBootApplication
@EnableConfigServer
public class EplConfigServiceApplication {

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

bootstrap.yml

server:
  port: 8088

 spring:
  application:
    name: eplconfigserver

  cloud:
    config:
      server:
        git:
          uri: https://github.com/stomer90/epl-config-server.git

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

@SpringBootApplication
@EnableEurekaServer
public class EplDiscoveryServiceApplication {

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

bootstrap.yml

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

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
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

请帮我解决这个问题

【问题讨论】:

    标签: spring-boot docker-compose microservices spring-cloud-netflix


    【解决方案1】:

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

    例如:

     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"
    

    注意配置服务器中的健康检查,它将从注册表服务器调用(条件:服务健康) 您可以将自己的自定义健康检查实现为更复杂的东西,例如

    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

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

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 1970-01-01
      • 2019-06-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-23
      • 2020-07-08
      • 2017-06-25
      • 2018-06-21
      相关资源
      最近更新 更多