使用Spring Boot的Docker和Eureka无法注册客户端 [英] Docker and Eureka with Spring Boot failing to register clients

查看:418
本文介绍了使用Spring Boot的Docker和Eureka无法注册客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring Boot + Docker Compose + Eureka的非常简单的演示。



我的服务器在端口8671上运行,具有以下应用程序属性:

 服务器:
端口:8761
eureka:
实例:
preferred-ip-address:true
客户端:
registerWithEureka:false
fetchRegistry:否
服务器:
waitTimeInMsWhenSyncEmpty:0

我的Eureka客户端运行在具有以下应用程序属性的9000端口:

 服务器:
端口:9000
春季:
应用程序:
名称:用户注册
尤里卡:
客户端:
registerWithEureka:真
fetchRegistry:真
serviceUrl:
defaultZone: http:// localhost:8761 / eureka /
实例:
preferred-ip-address:true

当我在父Maven项目中启动docker.compose文件时,这是我的docker-compose文件的内容:

  eureka服务器:
图片:rosentha l / eureka服务器
端口:
- 8761:8761
用户注册:
图像:rosenthal /用户注册
端口:
- 9000:9000
链接:
-尤里卡服务器

何时我先启动eureka服务器,然后再通过

  mvn spring-boot来运行我的应用程序:运行

服务器成功注册了我的客户端(我称为用户注册)。



当我通过docker-compose运行应用程序时,客户端无法通过以下输出进行注册:

  DiscoveryClient_USER -REGISTRATION / 0fd640cbc3ba:用户注册:9000:
注册服务...
user-registration_1 | 2017-06-21 04:36:05.120错误1 --- [nfoReplicator-0]
c.n.d.s.t.d.RedirectingEurekaHttpClient:请求执行错误
user-registration_1 |
user-registration_1 | com.sun.jersey.api.client.ClientHandlerException:
java.net.ConnectException:连接被拒绝(连接被拒绝)

我的第一个假设是,在等待服务器启动时运行docker-compose进入了竞争状态,但是我的eureka客户端似乎心跳不断,试图调用配置了该服务器的服务器。这意味着它只是找不到我注册的Eureka服务器(并且正在运行,我可以在localhost:8671上导航到它)。



我在这里缺少什么?通过本地启动的Spring-boot,并使用自己的嵌入式tomcat容器启动,一切运行正常。一旦我开始使用docker-compose进行操作,它就无法正常工作。



EDIT



<相信,我意识到了我的问题。因此docker不在本地主机上运行,​​而是在我启动docker时分配的公共IP上运行。导航到该ip +端口显示我的服务正在为Eureka Server运行。客户端仍然没有注册。



因此,我为我的eureka客户端更改了application.yml文件,以:

  serviceUrl:
defaultZone:http://192.168.59.103:8761/eureka/

该IP是我的docker守护进程运行的IP。现在,当我执行docker-compose时,它会丢失第一次注册,但是第二个心跳会接我的客户端。



如何确保客户端等待服务器完全启动?我在摘要文件中使用了适当的docker链接字段,但没有按预期运行。此外,如何查看defaultZone文件是我的DOCKER_HOST IP?



最终结果



生成的泊坞窗组成文件的文件对我有用:

  eureka服务器:
图片:thorrism / eureka-server
端口:
- 8761:8761
用户注册:
图片:thorrism /用户注册
端口:
- 9000:9000
链接:
-eureka服务器
环境:
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE:http:// eureka-server:8761 / eureka


解决方案

如果对您有用,那将以某种方式为我的生产环境配置它(docker-compose文件版本2):

 版本:'2'
服务:
尤里卡服务器:
图像:rosenthal / eureka服务器
暴露:
- 8761
用户注册:
图像:rosenthal /用户注册
container_name:用户注册
端口:
- 9000:8080
环境:
server.port:8080
eureka.client.enabled:'true'
eureka.host: eureka服务器
eureka.instance.preferIp地址:'true'

来自文档就是暴露确实:


在不将端口发布到主机的情况下公开它们-只有链接的服务才能访问它们。只能指定内部端口。


由于您在同一网络中拥有所有内容,因此容器可以看到彼此而没有链接



侧面注意



请记住,使用此配置端口9000将在主机上公开访问,并映射到用户注册容器的8080端口。


I have a very simple demo of using Spring Boot + Docker Compose + Eureka.

My server runs on port 8671 with the following application properties:

server:
  port: 8761
eureka:
  instance:
    prefer-ip-address: true
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

My Eureka client runs on port 9000 with the following application properties:

server:
  port: 9000
spring:
  application:
    name: user-registration
eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

When I start up my docker.compose file in the parent maven project, this is the contents of my docker-compose file:

eureka-server:
  image: rosenthal/eureka-server
ports:
   - "8761:8761"
user-registration:
  image: rosenthal/user-registration
  ports:
   - "9000:9000"
  links:
   - eureka-server

When I run my application by first starting the eureka server, following by the client via

mvn spring-boot:run 

The server successfully registers my client (I call it user-registration).

When I run my application through docker-compose, the client fails to register with the following output:

 DiscoveryClient_USER-REGISTRATION/0fd640cbc3ba:user-registration:9000: 
 registering service...
 user-registration_1  | 2017-06-21 04:36:05.120 ERROR 1 --- [nfoReplicator-0]        
 c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error
 user-registration_1  | 
 user-registration_1  | com.sun.jersey.api.client.ClientHandlerException: 
 java.net.ConnectException: Connection refused (Connection refused)

My first assumption was that running docker-compose ran into a race condition on waiting for the server to start, but my eureka client seems to have a heartbeat trying to call home to the server it's configured with. This means it's just not able to find the Eureka server I have registered (and is running, I can navigate to it on localhost:8671).

What am I missing here? Everything runs fine running locally with spring-boot starting up with it's own embedded tomcat containers. As soon as I start to do it with docker-compose, it doesn't want to work.

EDIT

I realized my problem, I believe. So docker doesn't run on localhost, it runs on the public IP it is assigned when I start up docker. Navigating to this ip + port shows my service running for Eureka Server. The client still doesn't register.

SO, I made changes to the application.yml file for my eureka client to:

serviceUrl:
  defaultZone: http://192.168.59.103:8761/eureka/

That IP is the one my docker daemon is running under. Now, it misses the first registration when I do docker-compose, but the second heartbeat picks up my client.

How can I ensure the client waits until the server is FULLY up? I used the proper docker "links" field in my docket compose file, but it didn't work as I hoped. Additionally, how can I see the defaultZone file to be my DOCKER_HOST IP?

Final result

The resulting docker-compose file that got everything working for me was:

eureka-server:
  image: thorrism/eureka-server
  ports:
   - "8761:8761"
user-registration:
  image: thorrism/user-registration
  ports:
   - "9000:9000"
  links:
   - eureka-server
  environment:
    EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://eureka-server:8761/eureka

解决方案

If useful for you, that's in some way how I configure it for my production environment (docker-compose file version 2):

version: '2'
services:
  eureka-server:
    image: rosenthal/eureka-server
    expose:
    - "8761"
  user-registration:
    image: rosenthal/user-registration
    container_name: user-registration
    ports:
    - "9000:8080"
    environment:
      server.port: 8080
      eureka.client.enabled: 'true'
      eureka.host: eureka-server
      eureka.instance.preferIpAddress: 'true'

From the docs that's what expose does:

Expose ports without publishing them to the host machine - they’ll only be accessible to linked services. Only the internal port can be specified.

As you've got everything in the same network, containers can see each other with no links between them.

SIDE NOTE

Keep in mind that with this configuration port 9000 will be publicly accessible at the host machine, and mapped to the 8080 port of the user-registration container.

这篇关于使用Spring Boot的Docker和Eureka无法注册客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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