在docker容器上运行ConfigServer和EurekaServer时无法获取配置文件

时间:2018-05-03 18:33:41

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

[Spring boot Microservies]

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

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

当在本地(而不是码头工具)上运行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
  • 的ConfigService

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" ]
  • 多克尔-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

请帮我解决此问题

1 个答案:

答案 0 :(得分: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

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