【发布时间】:2022-01-06 20:10:54
【问题描述】:
我有一个 Hello 服务和 World 服务。 Hello 服务有一个休息点 /hello/hw,它在内部调用 World 服务中的休息端点以返回字符串“Hello World”。 如果系统未 dockerized,则应用程序可以正常工作。
但是问题是当我想对系统进行 dockerize 时。 我使用 docker-compose 来容器化这两个服务。我在 docker compose 文件中定义了一个自定义网络(命名为 custom_net)。
rest端点/hello/hw可达,但Hello服务无法调用World服务中的rest端点。
Hello服务中的application.properties中World服务的url为http://localhost:8082/world
当我将 url 设置为 http://custom_net:8082/world 或 http://custom_net:8092/world 时,我得到 UnknownHostException
当我不更改 application.properties 中的 url 时,我收到 Connection denied 异常。 日志消息指出“失败:连接被拒绝:localhost/127.0.0.1:8082;” 我不确定桥接这 2 个服务需要什么配置
命令 docker network list 将自定义网络的名称显示为“tempfolder_custom_net”。 命令 docker inspect 显示 hello 和 world 服务都注册到了这个网络
version: '3'
services:
hello_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: hello_service
volumes:
- ./deploy:/root
networks:
- custom_net
depends_on:
- world_service
command: sh -c "java -jar -Dspring.config.location=file:///root/hello/application.yml /root/hello/hello-0.0.1-SNAPSHOT.jar "
ports:
- 8091:8081
world_service:
image: 'openjdk:8-jdk-alpine'
restart: always
container_name: world_service
volumes:
- ./deploy:/root
command: sh -c "java -jar -Dspring.config.location=file:///root/world/application.yml /root/world/world-0.0.1-SNAPSHOT.jar "
ports:
- 8092:8082
networks:
- custom_net
networks:
custom_net:
driver: bridge
Hello 服务的Application.yml ...
server:
port: 8081
services:
world:
url: http://localhost:8082/world
【问题讨论】:
-
尝试拨打
http://world_service:8092/world -
"http://world_service:8092/world" 给出连接被拒绝的异常。 "http://world_service:8082/world" 给出错误的请求 400 错误
-
你是如何从 hello service 调用 word service 的?如何在代码中配置 URL 调用?顺便说一句检查
http://172.17.0.1:8092/world -
在 Hello 服务中我使用的是 WebClient。 webClient 从 application.properties 获取基本 url。我不想硬编码IP。生产环境硬编码ip地址是标准做法吗?
-
能否分享 application.properties 文件中的 URL,并检查
http://172.17.0.1:8092/world?
标签: spring-boot docker docker-compose dockerfile