【问题标题】:Microservice can't reach config service in docker-compose微服务无法访问 docker-compose 中的配置服务
【发布时间】:2019-10-22 10:47:13
【问题描述】:

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

由于某种原因,我的服务无法从配置微服务中获取配置。
我的配置微服务应该可以工作,因为我可以在收到的机器上 curl 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

配置应用程序.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

【问题讨论】:

  • 你的 2 个微服务之间似乎有很强的依赖关系,这背后的原理是什么?
  • 你到底是什么意思?我打算创建更多服务,配置服务将为它们提供配置。
  • 我的意思是微服务应该是独立的,如果有依赖关系,应该在另一层解决
  • 您提供了一个有效点。我想我并没有考虑太多。
  • 配置服务可能会出现初始延迟,到那时依赖服务已经尝试连接到它。您可以在容器启动后通过 ssh 进入服务容器并 ping 配置容器并在可能的情况下卷曲端点来调试它。如果这可行,那么这是一个时间问题。如果不是,那就是配置问题。您可以在撰写文件中创建一个环境变量并将值设置为配置服务并使用它来连接到配置服务。

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


【解决方案1】:

当一项服务依赖于另一项服务时,您必须确保后者在连接之前已完全启动。

在您的情况下,config 很可能在运行 service 时已启动但尚未准备好(上下文已启动)。正如@Ganesh Karewad 和@asolanki 指出的那样,解决方案是实现重新连接逻辑。另一种解决方案是在运行service 之前确保config 已初始化并接受连接。

您可以通过等待config 应用程序启动的脚本来实现这一点。或者,您可以使用health check 命令配置config 容器,并在启动它后等待它被标记为健康。然后就可以运行服务容器了。

here 和 here 讨论的类似问题

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2021-04-10
    • 2022-07-16
    • 1970-01-01
    • 2019-11-22
    • 2016-02-02
    • 2021-10-26
    • 1970-01-01
    • 2020-09-12
    • 2021-06-14
    相关资源
    最近更新 更多