我正在尝试在我们的开发设置中引入 Spring Boot REST 服务。开发设置使用 docker-compose 和 API 网关来公开同一域(即本地主机)上的各个服务。
当我尝试通过共享 docker-compose 文件中的服务名称从另一个容器内部向我的服务发出 HTTP 请求时,该服务返回 400。
设置
我已经编辑了我们的 docker-compose 文件,所以它看起来像下面介绍 Spring Boot Java 服务。该服务基于 spring-boot-starter-parent (2.0.3.RELEASE) 和 spring-boot-starter-web。我还没有配置任何与 web 服务器相关的东西(除了添加 server.server-header 属性以确保我自己的请求命中我的服务)。
version: '3'
services:
...
hello_java:
build:
context: ../hello-java/
dockerfile: Dockerfile
depends_on:
- postgres
- castle_black
ports:
- "8301:8080"
castle_black:
build: ../castle-black/tyk-gateway
ports:
- "8191:8080"
depends_on:
- redis
行为
如果我从容器外部请求 hello 服务(例如在 localhost:8301 上的浏览器中),它会正确回复。如果我在一个容器内,但是获取了带有我的新服务的容器在 docker 网络中获得的 IP,并使用新服务也能正确响应。
下面我展示了从 API 网关容器内部到 Java 服务的请求,首先使用服务名称,然后使用已解析的 IP。它仅在 IP 案例中以正确的响应进行回复。
# curl -v http://hello_java:8080/hello-java/greet?username=Java
* Hostname was NOT found in DNS cache
* Trying 172.19.0.6...
* Connected to hello_java (172.19.0.6) port 8080 (#0)
> GET /hello-java/greet?username=Java HTTP/1.1
> User-Agent: curl/7.35.0
> Host: hello_java:8080
> Accept: */*
>
< HTTP/1.1 400
< Transfer-Encoding: chunked
< Date: Wed, 01 Aug 2018 11:34:34 GMT
< Connection: close
* Server MySpringBootApp is not blacklisted
< Server: MySpringBootApp
<
* Closing connection 0
# curl -v http://172.19.0.6:8080/hello-java/greet?username=Java
* Hostname was NOT found in DNS cache
* Trying 172.19.0.6...
* Connected to 172.19.0.6 (172.19.0.6) port 8080 (#0)
> GET /hello-java/greet?username=Java HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 172.19.0.6:8080
> Accept: */*
>
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 10
< Date: Wed, 01 Aug 2018 11:34:55 GMT
* Server MySpringBootApp is not blacklisted
< Server: MySpringBootApp
<
* Connection #0 to host 172.19.0.6 left intact
Hello Java
问题
当客户端添加“Host: hello_java:8080”标头时,标准 spring-boot-starter-web 设置中是否有阻止 Web 服务器为请求提供服务的内容?或者为什么 Web 服务器在这两种情况下表现不同?我该怎么办?
炎炎设计
呼如林
相关分类