Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当其他docker compose服务按服务名称请求时,Spring Boot REST应用程序返回400_Java_Spring Boot_Docker Compose_Bad Request - Fatal编程技术网

Java 当其他docker compose服务按服务名称请求时,Spring Boot REST应用程序返回400

Java 当其他docker compose服务按服务名称请求时,Spring Boot REST应用程序返回400,java,spring-boot,docker-compose,bad-request,Java,Spring Boot,Docker Compose,Bad Request,我正试图在我们的开发设置中引入SpringBootREST服务。开发设置使用docker compose和API网关公开同一域(即localhost)上的各个服务 当我试图通过共享docker compose文件中的服务名称从另一个容器中向我的服务发出HTTP请求时,服务返回400 设置 我已经编辑了docker compose文件,因此下面介绍Spring Boot Java服务。该服务基于SpringBootStarter父级(2.0.3.RELEASE)和SpringBootStarter

我正试图在我们的开发设置中引入SpringBootREST服务。开发设置使用docker compose和API网关公开同一域(即localhost)上的各个服务

当我试图通过共享docker compose文件中的服务名称从另一个容器中向我的服务发出HTTP请求时,服务返回400

设置 我已经编辑了docker compose文件,因此下面介绍Spring Boot Java服务。该服务基于SpringBootStarter父级(2.0.3.RELEASE)和SpringBootStarter web。我没有配置任何与web服务器相关的内容(除了添加server.server-header属性以确保请求命中我的服务)

行为 如果我从容器外部请求hello服务(例如,在localhost:8301上的浏览器中),它会正确回复。如果我在一个容器中,但获得了容器和我的新服务在docker网络中获得的IP,并使用该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
#curl-vhttp://hello_java:8080/hello-java/greet?username=java
*在DNS缓存中找不到主机名
*正在尝试172.19.0.6。。。
*已连接到hello_java(172.19.0.6)端口8080(#0)
>GET/hello java/greet?username=java HTTP/1.1
>用户代理:curl/7.35.0
>主持人:你好,java:8080
>接受:*/*
> 
GET/hello java/greet?username=java HTTP/1.1
>用户代理:curl/7.35.0
>主持人:172.19.0.6:8080
>接受:*/*
> 
问题
当客户端添加“Host:hello\u java:8080”头时,标准spring boot starter web设置中是否存在阻止web服务器为请求提供服务的内容?或者,为什么web服务器在这两种情况下表现不同?我能做些什么呢?

经过一些实验,发现是服务名称中的下划线导致了问题。将服务名称更改为不带下划线解决了此问题。

RFC 952规定“名称”(网络、主机、网关或域名)是一个文本字符串,最多24个字符,从字母表(a-Z)、数字(0-9)、减号(-)和句点(.)

似乎u不是主机名的有效组件。这有点让人困惑,因为我也遇到了同样的问题,当我ping app_服务器时,情况很好,但当我从app_服务器上下载时,我收到了错误的请求


将下划线改为负号对我来说是固定的。

你提到了一个API网关(Zuul,Spring Cloud gateway,…?)-你的网关本身是一个服务吗(也是docker容器的一部分)?如果不是,则网关(外部)和服务(容器内部)具有不同的“localhost”,网关无法设置代理。API网关是tyk.io,它正在compose文件的第二个容器中运行。当我在API网关容器中运行bash时,我可以访问hello_java容器中的web服务器。如果我向hello_java发出HTTP请求,web服务器将以400状态代码响应,而如果我向容器中hello_java解析到的IP发出请求,它将给出正确的响应(200 OK)。很抱歉,您的断言不正确。RFC952已由其他RFC更新(例如,允许数字作为第一个符号),未被废弃。从“updated by”关系的传递闭包开始,我没有时间查看所有RFC,但我假设主机名使用下划线的限制仍然有效。如果没有,请指向适当的RFC,该RFC放宽了限制。在任何情况下,从名称中删除下划线都可以解决我的问题。经过无数个小时的调试,这里提供的解决方案也对我有所帮助。虽然我仍然有名称中包含一个下划线的服务,并且我的nginx反向代理没有向它们发送路由请求的问题,但当我引入一个包含两个下划线的服务时,它会拒绝这样做。我现在完全重新命名了这个有问题的服务(仅仅是一个单词),事情开始起作用了。所以,感谢OP与我们分享!我从来没有对找到解决办法这么生气过。非常感谢你发布这篇文章,我从来没有想到这就是问题所在
# 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