无法访问Docker容器中的Spring Cloud Config [英] Spring Cloud Config in Docker container is not accessible

查看:151
本文介绍了无法访问Docker容器中的Spring Cloud Config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相对简单的设置.Spring Could Config服务器和Spring Cloud Eureka服务器.(我将Eureka服务器称为在我的配置中无处不在...").该配置服务器使用Git存储库来查找适当的配置.

I have a relatively simple setup. A Spring Could Config server, and a Spring Cloud Eureka server. (I refer Eureka server as Discover everywhere in my config...) The config server uses a Git repository for finding the appropriate config.

一切都在Docker容器中.我使用 docker-compose 构建我的服务.网关服务器应等待配置服务器启动.我将在更下方提供文件的来源.

Everything is in Docker containers. I use docker-compose to build my services. The Gateway server should wait for config server to come up. I'll provide the source of the file further down.

因此,当我构建和启动容器时,发现(Eureka)服务器不想启动.它异常退出.它真的很长,所以我在这里添加最重要的部分:

So, when I build and start the containers, the discovery (Eureka) server doesn't want to come up. It exits with an exception. It is really long, so I just add the most important part here:

019-01-27 23:52:17,494 [INFO  ] o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment [main] – Fetching config from server at : http://tao-elszamolas-config:9001
2019-01-27 23:52:17,898 [INFO  ] o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment [main] – Connect Timeout Exception on Url - http://tao-elszamolas-config:9001. Will be trying the next url if available
2019-01-27 23:52:17,902 [ERROR ] o.s.b.SpringApplication.reportFailure [main] – Application run failed
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
... Something...Something
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://tao-elszamolas-config:9001/discovery-server/prod": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:218) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:96) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
    ... 15 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:?]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[?:?]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[?:?]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[?:?]
    at java.net.Socket.connect(Socket.java:591) ~[?:?]
    at java.net.Socket.connect(Socket.java:540) ~[?:?]
    at sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[?:?]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[?:?]
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[?:?]
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[?:?]
    at sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[?:?]
    at sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[?:?]
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242) ~[?:?]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181) ~[?:?]
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075) ~[?:?]
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009) ~[?:?]
    at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:734) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:218) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:96) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
    ... 15 more

我在Config服务器日志中发现的唯一例外是一个抱怨与Discovery(Eureka)服务器连接的错误.是的,我尝试在Eureka中注册我的配置服务,以便在启动其他微服务时查找它.

The only exception what I found in the Config server log is the one complaining about the connection to Discovery (Eureka) server. Yes, I try to register my config service inEureka for looking it up at startup of further microservices.

连接被拒绝,这意味着在Eureka服务器尝试获取其配置时,配置服务器未运行,或者未在给定的端口9001上侦听.

The connection is refused which means, the config server is not running at the time when Eureka server is trying to get its config or it is not listening on the given port, 9001.

怎么可能?我在撰写文件中使用了 depends_on ,因此它应该等待.

How is it possible? I used the depends_on in my compose file, so it should wait for that.

还是为什么它能完成工作?如果我在没有docker的本地计算机上执行相同的操作,则它将一直尝试连接到Config,直到config服务器启动.我想使用Docker实现相同的行为.

Or why it finishes working? If I do the same thing on local without docker then it keeps trying to connect to Config, until the config server starts up. I'd like achieve the same behaviour using Docker.

任何建议将不胜感激!

最后是我的配置文件:

docker-compose.yml :

 version: "3"
   services:           
     tao-elszamolas-config:
       build: ./tao-elszamolas-config
       container_name: tao-elszamolas-config
       ports:
         - "9001:9001"
       volumes:
         - "/tao-elszamolas/logs:/var/log/tao-elszamolas"
       networks:
         - taonetwork

       tao-elszamolas-discovery:
         build: ./tao-elszamolas-discovery
         container_name: tao-elszamolas-discovery
         depends_on:
           - tao-elszamolas-config
         ports:
           - "9002:9002"
         volumes:
           - "/tao-elszamolas/logs:/var/log/tao-elszamolas"
         networks:
           - taonetwork

   networks:
     taonetwork:
       driver: bridge

配置服务器的

Dockerfile :

 FROM openjdk:11-jdk-slim
 MAINTAINER BeszterceKK
 COPY ./tao-elszamolas-config.jar /usr/src/taoelszamolas/tao-elszamolas-config.jar
 WORKDIR /var/log/tao-elszamolas
 WORKDIR /usr/src/taoelszamolas
 ENV SPRING_PROFILES_ACTIVE prod
 EXPOSE 9001
 ENTRYPOINT ["java", "-DlogFileLocation=/var/log/tao-elszamolas", "-jar", "tao-elszamolas-config.jar"]

application.yml (用于配置服务器):

application.yml for Config server:

server:
  port: 9001

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/something/something
          username: something
          password: something
          skip-ssl-validation: true
  security:
    user:
      name: something
      password: something

eureka:  
  client:
    serviceUrl:
      defaultZone: http://something:something@tao-elszamolas-discovery:9002/eureka/


info:
  artifact: tao-elszamolas-config
  name: TAO Elszamolas Config Application
  description: Tao elszamolas microservice elosztott szolgaltatas konfiguracio
  version: 1.0.0

Discovery(Eureka)服务器的

Dockerfile :

Dockerfile of Discovery (Eureka) Server:

FROM openjdk:11-jdk-slim
MAINTAINER BeszterceKK
COPY ./tao-elszamolas-discovery.jar /usr/src/taoelszamolas/tao-elszamolas-discovery.jar
WORKDIR /var/log/tao-elszamolas
WORKDIR /usr/src/taoelszamolas
ENV SPRING_PROFILES_ACTIVE prod
EXPOSE 9002
ENTRYPOINT ["java", "-DlogFileLocation=/var/log/tao-elszamolas", "-jar", "tao-elszamolas-discovery.jar"]

bootstrap.yml (用于发现(Eureka)服务器):

bootstrap.yml for Discovery (Eureka) server:

spring:
  application:
    name: discovery-server
  cloud:
    config:
      fail-fast: true
      name: discovery-server
      uri: http://tao-elszamolas-config:9001
      username: something
      password: something

application.yml (用于发现(Eureka)服务器):

application.yml for Discovery (Eureka) server:

server:
  port: 9002

eureka:
  instance:
    hostname: tao-elszamolas-discovery
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://tao-elszamolas-discovery:9002/eureka

info:
  artifact: tao-elszamolas-discovery
  name: TAO Elszamolas Eureka Szolgaltatas monitor
  description: Tao elszamolas automatikus szolgaltatas regisztracio
  version: 1.0.0

最后,这就是我启动容器的方式.很简单:

And finally, this is how I start my containers. It is simply:

docker-compose up -d

整个应用程序堆栈在没有Docker的情况下可在本地运行.所以我想问题出在我的Docker配置上.

The whole application stack works on local without Docker. So I guess the problem is with my Docker configuration.

还有一件事要提到,通常,我不想发布要托管的端口,而只是在我的容器之间的自定义网络上公开它们.这就是为什么我尝试将 PORTS 替换为 EXPOSE 的原因,但这并没有真正的帮助.

One more thing to mention, generally, I don't want to publish the ports to host, only expose them on my custom network between the containers. This is why I tried to replace the PORTS with EXPOSE, but it didn't really help.

推荐答案

对于启动而言,Compose不会等到容器就绪"(对于您的特定应用程序而言意味着什么)–直到容器运行为止

for startup Compose does not wait until a container is "ready" (whatever that means for your particular application) - only until it’s running

因此,您应该参考: https://docs.docker.com/compose/启动订单/

等待,直到首先准备好配置服务器容器(已经在端口9001上侦听),然后再启动Eureka服务器容器.

for wait until for config server container is ready first (already listened on port 9001), then starting Eureka server container.

这篇关于无法访问Docker容器中的Spring Cloud Config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆