微服务无法在docker-compose中到达配置服务

时间:2019-06-06 22:05:08

标签: java spring-boot docker docker-compose microservices

我目前有两个微服务:
-服务-端口8080,此微服务尝试从其他微服务获取配置。
- config -端口8888,该微服务应该提供配置。

由于某种原因,我的服务无法从config microservice获取配置。
我的配置微服务应该可以正常工作,因为我可以在收到的机器上卷曲localhost:8888 / service / default:

{"name":"service","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:/shared/service.yml","source":{"server.port":8080,"spring.security.user.password":"admin"}},{"name":"classpath:/shared/service.yaml","source":{"server.port":8080,"spring.security.user.password":"admin"}}]}

收到错误(full)

service | 2019-06-06 21:31:06.721  INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://config:8888   
service | 2019-06-06 21:31:06.894  INFO 1 --- [main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://config:8888. Will be trying the next url if available   
service | 2019-06-06 21:31:06.904 ERROR 1 --- [main] o.s.boot.SpringApplication               : Application run failed

Docker-compose.yaml

version: '3.7'
services:
  config:
    container_name: config
    build: ./config
    ports:
      - 8888:8888

  service:
    container_name: service
    build: ./service
    ports:
      - 8080:8080
    depends_on:
      - config

服务Dockerfile:

FROM openjdk:8-jdk-alpine
ADD target/service.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8080

服务bootstrap.yaml

spring:
  application:
    name: service
  cloud:
    config:
      uri: http://config:8888
      fail-fast: true

service.yaml(具有服务配置)

server:
  port: 8080

spring:
  security:
    user:
      password: admin # doesnt set since no connection

配置Dockerfile

FROM openjdk:8-jdk-alpine
ADD target/config.jar /app.jar
CMD [ "java", "-Xmx200m", "-jar", "/app.jar" ]
EXPOSE 8888

配置application.yaml

spring:
  application:
    name: config
  profiles:
    active: composite
  cloud:
    config:
      server:
        composite:
          - type: native
            search-locations: classpath:/shared

server:
  port: 8888

shared / service.yaml(具有服务配置)

server:
  port: 8080

spring:
  security:
    user:
      password: admin # doesnt set since no connection

有什么想法吗?

我发现了一些类似的问题,尽管他们的URI只是问题,我的设置正确。 Microservice can not reach to Config Server on Docker Compose
Docker - SpringConfig - Connection refused to ConfigServer

1 个答案:

答案 0 :(得分:1)

当一项服务依赖于另一项服务时,必须确保后者已完全启动,然后才能连接到该服务。

对于您而言,很可能config已启动,但在运行service时尚未就绪(上下文已启动)。正如@Ganesh Karewad和@asolanki指出的,解决方案是实现重新连接逻辑。另一个解决方案是在运行config之前确保service已初始化并接受连接。

您可以使用脚本来完成该任务,该脚本要等到config应用启动后才能执行。或者,您可以使用health check命令配置config容器,并在启动后等待其标记为运行状况良好。然后,您可以运行服务容器。

讨论了类似问题herehere

希望有帮助。