简体   繁体   English

Docker构成跨容器通信

[英]Docker compose cross containers communication

I'm a very beginner in docker world, and I'm not able to make two containers communicate using docker compose. 我是docker世界中的一个初学者,我无法使用docker compose使两个容器通信。 I have two containers: 我有两个容器:

  • Service Registry: a simple spring boot application using Netflix Eureka to implement a service registry feature. 服务注册表:使用Netflix Eureka实施服务注册表功能的简单春季启动应用程序。
  • API Gateway: a simple spring boot application using Netflix Zuul. API网关:使用Netflix Zuul的简单Spring Boot应用程序。 This application will try periodically to be registered into the Service Registry application by connecting to a given URL 该应用程序将尝试通过连接到给定的URL定期尝试注册到Service Registry应用程序中

All work fine without docker !! 没有泊坞窗,一切正常! And now with docker-compose, the gateway is not able to find the Eureka server URL. 现在使用docker-compose,网关无法找到Eureka服务器URL。

docker-compose.yml file: docker-compose.yml文件:

version: '3.5'
services:

gateway:
 build:
  context: ../robots-store-gateway
  dockerfile: Dockerfile
 image: robots-store-gateway
 ports:
  - 8000:8000
 networks:
  - robots-net

serviceregistry:
 build:
  context: ../robots-sotre-serviceregistry
 image: robots-sotre-serviceregistry
 ports:
  - 8761:8761
 networks:
  - robots-net

networks:
 robots-net:
  name: custom_network
  driver: bridge

The application.yml file of the gateway is: 网关的application.yml文件为:

eureka:
  client:
    service-url:
      default-zone: http://serviceregistry:8761/eureka/

I receive this exception: 我收到此异常:

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused) com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝(连接被拒绝)

I tried different ways to configure the Eureka client but no way !! 我尝试了其他方法来配置Eureka客户端,但是没有办法! it doesn't work. 它不起作用。 Thanks in advance. 提前致谢。

I don't exactly why !!! 我也不是为什么! But finally, this works for me: 但最后,这对我有用:

version: '3.5'
services:

 gateway:
   container_name: gateway
   build:
    context: ../robots-store-gateway
    dockerfile: Dockerfile
   image: robots-store-gateway
   ports:
    - 8000:8000
   hostname: gateway
   environment:
     eureka.client.serviceUrl.defaultZone: http://serviceregistry:8761/eureka/

 serviceregistry:
   container_name: serviceregistry
   build:
    context: ../robots-sotre-serviceregistry
   image: robots-sotre-serviceregistry
   ports:
    - 8761:8761
   hostname: serviceregistry
   environment:
    eureka.client.serviceUrl.defaultZone: http://serviceregistry:8761/eureka/

You didn't set the container name. 您没有设置容器名称。 Link 链接

And the url or ip should replace to container name. 并将url或ip替换为容器名称。

Example: mongodb://{container_name}:27017 范例:mongodb:// {{container_name}:27017

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM