Cannot link Consul and Spring Boot app in Docker

痴心易碎 提交于 2019-12-11 04:41:55

问题


I have a Spring Boot app with the following config:

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

that I want to run with 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

Unfortunately that does not work, I get: com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused.

I have absolutely no idea why it can't connect to Consul, because I can connect to other systems , e.g. rabbitmq in other applications exactly this way.

Thank you for any ideas!


回答1:


If you're trying to connect immediately when the container starts it's possible that consul is not ready to receive connections yet.

You need to write an entrypoint script to wait for the connection to be available, or even just retry the connection a few times. See https://docs.docker.com/compose/startup-order/ for more information.



来源:https://stackoverflow.com/questions/38591295/cannot-link-consul-and-spring-boot-app-in-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!