Spring启动应用程序无法链接到docker中的consul

时间:2016-08-01 03:01:19

标签: docker spring-boot consul

我正在尝试使用Consul作为发现服务,另外两个春季启动应用程序向Consul注册;并把它们放入码头工人;

以下是我的代码: 应用程式:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- shouldn't expand -->
<div class="container-fluid" id="content-fluid">
  <div class="container-update">
    <div class="update-group">
      <div class="update-header">
        HEADER
        <div class="update-author">
          by CREATOR
        </div>
      </div>
      <div class="update-body">
        TEST
      </div>
      <div class="updateOptions-hidden">
        <a href="editupdate.php?id="><i class="glyphicon glyphicon-wrench"></i></a>
        <a href="removephp.php?id="><i class="glyphicon glyphicon-remove-sign"></i></a>
      </div>
    </div>
  </div>
</div>

<!-- should expand -->
<div class="container-fluid" id="content-fluid">
  <div class="container-update">
    <div class="update-group">
      <div class="update-header">
        HEADER
        <div class="update-author">
          by CREATOR
        </div>
      </div>
      <div class="update-body">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum sodales massa ac consectetur. Maecenas eu nisl vel erat pellentesque eleifend ut a est. Nullam ac lorem nisi, eu consequat nisi. In euismod porttitor adipiscing. Aenean feugiat,
        turpis nec accumsan posuere, diam tellus pharetra arcu, sit amet ultrices magna felis ac elit. Donec sit amet massa nisi, sed semper turpis. Aliquam fringilla vestibulum urna, in tincidunt dui facilisis condimentum. Integer eget enim lectus, id
        congue ipsum. Sed sit amet ante nec turpis mattis venenatis. In urna quam, cursus vel cursus eu, volutpat eget leo. Praesent in mi diam.
      </div>
      <div class="updateOptions-hidden">
        <a href="editupdate.php?id="><i class="glyphicon glyphicon-wrench"></i></a>
        <a href="removephp.php?id="><i class="glyphicon glyphicon-remove-sign"></i></a>
      </div>
    </div>
  </div>
</div>

2 docker-compose.yml

server:
  port: 3333

spring:
  application:
    name: adder
  cloud:
    consul:
      host: consul
      port: 8500
      discovery:
        preferIpAddress: true
        healthCheckPath: /health
        healthCheckInterval: 15s
        instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}

还有另一个类似的问题Cannot link Consul and Spring Boot app in Docker; 答案表明,应用程序应该等待领事通过使用depends_on完全工作,我试过,但没有工作;

错误信息如下:

 consul1:
  image: "progrium/consul:latest"
  container_name: "consul1"
  hostname: "consul1"
  command: "-server -bootstrap -ui-dir /ui"
 adder:
  image: wsy/adder
  ports:
   - "3333:3333"
 links:
   - consul1
 environment:
   WAIT_FOR_HOSTS: consul1:8500

除了spring boot application.yml和docker-compose.yml之外,以下是App的Dockerfile

adder_1    | com.ecwid.consul.transport.TransportException: java.net.ConnectException: Connection refused
adder_1    |    at com.ecwid.consul.transport.AbstractHttpTransport.executeRequest(AbstractHttpTransport.java:80) ~[consul-api-1.1.8.jar!/:na]
adder_1    |    at com.ecwid.consul.transport.AbstractHttpTransport.makeGetRequest(AbstractHttpTransport.java:39) ~[consul-api-1.1.8.jar!/:na]

和start.sh

FROM java:8

VOLUME /tmp
ADD adder-0.0.1-SNAPSHOT.jar app.jar
RUN bash -c 'touch /app.jar'
ADD start.sh start.sh
RUN bash -c 'chmod +x /start.sh'
EXPOSE 3333
ENTRYPOINT ["/start.sh", " java -Djava.security.egd=file:/dev/./urandom    -jar /app.jar"]

3 个答案:

答案 0 :(得分:0)

您的问题是depends_on仅控制您服务的启动顺序。在开始使用spring应用程序之前,您必须等到领事服务器启动并运行。您可以使用此脚本执行此操作:

#!/bin/bash

set -e

default_host="database"
default_port="3306"

host="${2:-$default_host}"
port="${3:-$default_port}"

echo "waiting for TCP connection to $host:$port..."

while ! (echo >/dev/tcp/$host/$port) &>/dev/null
do
  sleep 1
done

echo "TCP connection ready. Executing command [$1] now..."

exec $1

您在docker文件中的用法:

COPY wait.sh /wait.sh
RUN chmod +x /wait.sh
CMD ["/wait.sh", "java -jar yourApp-jar" , "consulURL" , "ConsulPort" ]

答案 1 :(得分:0)

我只想澄清一点,最后我还是没有解决方案,也无法理解这里的情况;我尝试了Ohmen的建议,在APP容器中,我能ping通consul1;但APP仍无法连接领事;

如果我只按以下命令启动领事:

docker-compose -f docker-compose-discovery.yml up discovery

然后我可以直接运行APP(通过Main),它可以连接spring.cloud.consul.host:discovery;

但是,如果我尝试在docker容器中运行APP,如下所示:

docker run --name adder --link discovery-consul:discovery wsy/adder

连接被拒绝后再次失败;

我对docker&amp; amp;搬运工-撰写;我认为这将是一个很好的例子,但对我来说似乎并不容易;

答案 2 :(得分:0)

我可以推断您的 Consul 配置位于application.yml而不是bootstrap.yml,这就是问题。

根据this answerbootstrap.ymlapplication.yml之前加载,而Consul客户端必须在应用程序本身之前检查其配置,因此请查看bootstrap.yml

工作 bootstrap.yml

的示例
spring:
  cloud:
    consul:
      host: consul
      port: 8500
      discovery:
        prefer-ip-address: true
  1. 运行Consul服务器,不要忘记 name 选项以匹配您的配置:

    docker run -d -p 8500:8500 --name=consul progrium/consul -server -bootstrap

  2. Consul服务器现在正在运行,运行您的应用程序映像(先前使用您的工件构建)并将其链接到Consul容器:

    docker run -d -name=my-consul-client-app --link consul:consul acme/spring-app