【问题标题】:Cannot link Consul and Spring Boot app in Docker无法在 Docker 中链接 Consul 和 Spring Boot 应用程序
【发布时间】:2016-11-30 04:26:52
【问题描述】:

我有一个具有以下配置的 Spring Boot 应用程序:

spring:
  thymeleaf:
    cache: false
  cloud:
    consul:
      host: consul
      port: 8500
      discovery:
        prefer-ip-address: true
        instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

我想用 docker-compose(Docker 1.11.2, docker-compose 1.7.1) 运行:

consul:
  image: progrium/consul:latest
  container_name: consul
  hostname: consulhost
  ports:
    - "8400:8400"
    - "8500:8500"
    - "8600:53"
  command: "-server -bootstrap-expect 1 -ui-dir /ui"

collector-server:
  container_name: collector-server
  image: io.thesis/collector-server
  ports:
    - "9090:9090"
  links:
    - consul:consul

不幸的是,这不起作用,我得到:com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection denied。

我完全不知道为什么它不能连接到 Consul,因为我可以连接到其他系统,例如rabbitmq 在其他应用程序中正是如此。

感谢您的任何想法!

【问题讨论】:

    标签: docker spring-boot docker-compose spring-cloud consul


    【解决方案1】:

    如果您尝试在容器启动时立即连接,则 consul 可能尚未准备好接收连接。

    您需要编写一个入口点脚本来等待连接可用,或者甚至只是重试连接几次。请参阅https://docs.docker.com/compose/startup-order/ 了解更多信息。

    【讨论】:

    • 谢谢,我会尝试...但我仍然想知道,因为我从来没有用 rabbitmq、mysql 等做过。Consul 启动非常快,日志显示它已经启动在我的 spring 应用程序上下文加载之前。
    • 它适用于 docker-compose v2-format 和 depends_on。非常感谢!
    猜你喜欢
    • 2016-12-06
    • 2016-12-24
    • 2018-05-13
    • 2020-04-18
    • 2018-10-31
    • 2021-08-12
    • 2019-06-21
    • 1970-01-01
    • 2021-04-22
    相关资源
    最近更新 更多