[英]Docker Compose Linked Microservices Not Discoverable
When using docker compose I cannot get two services to communicate with one another. 当使用docker compose时,我无法获得两项服务以相互通信。 The idea is getting a Node.JS service to talk to a Java/Spring service.
这个想法是让Node.JS服务与Java / Spring服务对话。
I have tried using 'links', putting them into the same 'network' and then sending requests to http://service_name:port and it returns ERR_NAME_NOT_RESOLVED. 我尝试使用“链接”,将它们放入相同的“网络”,然后将请求发送到http:// service_name:port ,它返回ERR_NAME_NOT_RESOLVED。
Within the Node.JS service I have tried using Axios and the http module, neither work (ERR_NAME_NOT_RESOLVED). 在Node.JS服务中,我尝试使用Axios和http模块,但均无效(ERR_NAME_NOT_RESOLVED)。 I have also tried 'localhost', which doesn't work either (ERR_CONNECTION_REFUSED).
我也尝试过'localhost',它也不起作用(ERR_CONNECTION_REFUSED)。 The Spring service is literally just an exposed REST endpoint (that I know works, as I can access it directly).
实际上,Spring服务只是一个公开的REST端点(据我所知,这是可行的,因为我可以直接访问它)。
I have also tried passing a reference to the service via environment variables eg (inside docker-compose.yml). 我也尝试通过环境变量(例如,在docker-compose.yml内部)传递对服务的引用。
environment:
- SERVICE_HOST=serviceB
The env variable inside the Node.JS project is undefined when calling below 在下面调用时,Node.JS项目内的env变量未定义
process.env.SERVICE_HOST
I am using Windows with Docker toolbox, but have also tried the same project in a Ubuntu VM. 我将Windows与Docker工具箱一起使用,但也在Ubuntu VM中尝试了相同的项目。
serviceA:
build: ./serviceA/
ports:
- "8080:8080"
networks:
- my_network
serviceB:
build: ./serviceB/
ports:
- "9003:9003"
networks:
- my_network
networks:
my_network:
driver: bridge
axios.get("http://serviceB:9003/test")
.then(function(res){
console.log(res);
})
I expected the console.log statement within the Node.JS service to respond with the results from ServiceB's rest call, instead of the error messages. 我希望Node.JS服务中的console.log语句响应ServiceB的rest调用的结果,而不是错误消息。
I am new to using docker-compose, so am expecting I am just missing something obvious here, but I haven't been able to find anything that solves my problem online, nor in similar questions on SO. 我是使用docker-compose的新手,所以我期望这里只是缺少一些明显的东西,但是我无法在网上找到能够解决我的问题或类似问题的任何东西。
EDIT Added entire yaml file and errors for better understanding. 编辑添加了整个yaml文件和错误,以更好地理解。
version: '3'
services:
# Consul
consul:
container_name: consul
image: consul:latest
ports:
- "8500:8500"
- "8300:8300"
hostname: consul
networks:
- front_end
- back_end
# Actor Service
actor_service:
build: ./actor_service/
ports:
- "9001:8080"
depends_on:
- actor_service_db
- consul
networks:
- back_end
actor_service_db:
image: postgres:9.4.5
depends_on:
- consul
networks:
- back_end
environment:
- POSTGRES_DB=actor
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- CONSUL_HTTP_ADDR=https://consul:8500
# Movie Service
movie_service:
build: ./movie_service/
ports:
- "9002:8080"
depends_on:
- movie_service_db
- consul
networks:
- back_end
movie_service_db:
image: postgres:9.4.5
depends_on:
- consul
networks:
- front_end
environment:
- POSTGRES_DB=movie
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- CONSUL_HTTP_ADDR=https://consul:8500
# Movie Aggregate Service
movie_aggregate_service:
container_name: movie_aggregate_service
build:
context: ./movie_aggregate_service/
ports:
- "9003:8080"
depends_on:
- consul
- movie_service
- actor_service
networks:
- front_end
- back_end
hostname: movie_aggregate_service
# Frontend
front_end:
build: ./front_end/
ports:
- "8080:8080"
networks:
- front_end
environment:
- "CONSUL_HOST=consul"
links:
- consul
- movie_aggregate_service
networks:
front_end:
driver: bridge
back_end:
driver: bridge
您的撰写看起来不错-服务发现依赖于容器内的DNS,我尝试将execing( exec
)放入serviceA,并检查/etc/resolv.conf
没发生任何时髦事件。.您应该能够从exec ping serviceB贝壳。
You might want to specify a hostname on each service to ease accessing of endpoints living on them and link them explicitly. 您可能希望在每个服务上指定一个主机名,以简化对驻留在它们上的端点的访问并显式链接它们。 Below is a sample Docker-compose file.
下面是一个示例Docker-compose文件。
version: "3"
services:
ocelot.products:
image: pogs/ocelot-products
container_name: ocelot-products
hostname: ocelot.products
build:
context: ./products
ports:
- "52790:3000"
environment:
"PUBLIC_PORT": "52790"
ocelot.users:
image: pogs/ocelot-users
container_name: ocelot-users
hostname: ocelot.users
build:
context: ./users
ports:
- "52791:3000"
environment:
"PUBLIC_PORT": "52791"
ocelot.transactions:
image: pogs/ocelot-transactions
container_name: ocelot-transactions
hostname: ocelot.transactions
build:
context: ./transactions
ports:
- "52792:3000"
environment:
"PUBLIC_PORT": "52792"
ocelot.gateway:
image: pogs/ocelot-gateway
container_name: ocelot-gateway
build:
context: ./gateway/EShop
ports:
- "52793:80"
links:
- ocelot.products
- ocelot.users
- ocelot.transactions
depends_on:
- ocelot.products
- ocelot.users
- ocelot.transactions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.