简体   繁体   English

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

[英]Spring Boot REST app returns 400 when requested from other docker-compose service by service name

I'm trying to introduce a Spring Boot REST service in our development setup. 我正在尝试在我们的开发设置中引入Spring Boot REST服务。 The development setup is using docker-compose and an API gateway to expose the individual services on the same domain (ie. localhost). 开发设置使用docker-compose和API网关来公开同一域(即localhost)上的各个服务。

When I try to make a HTTP request to my service from inside another container via the service name in the shared docker-compose file, the service returns a 400. 当我尝试通过共享docker-compose文件中的服务名称从另一个容器内部向我的服务发出HTTP请求时,该服务返回400。

The setup 设置

I've edited our docker-compose file, so it looks like the below to introduce the Spring Boot Java service. 我编辑了docker-compose文件,所以它如下所示介绍Spring Boot Java服务。 The service is based on spring-boot-starter-parent (2.0.3.RELEASE) and spring-boot-starter-web. 该服务基于spring-boot-starter-parent(2.0.3.RELEASE)和spring-boot-starter-web。 I haven't configured anything related to the web server (except adding the server.server-header property to ensure myself that the request is hitting my service). 我没有配置任何与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

The behaviour 这种行为

If I request the hello service from outside the containers (eg in my browser on localhost:8301) it replies back correctly. 如果我从容器外部请求hello服务(例如在我的浏览器localhost:8301上),它会正确回复。 If I'm inside a container, but obtain the IP that the container with my new service gets in the docker network and use that the new service also responds correctly back. 如果我在容器内部,但获取具有我的新服务的容器进入docker网络并使用该IP的新IP服务也正确响应的IP。

Below I have shown a request from inside the API gateway container to the Java service, first by using the service name and then afterwards with the IP that was resolved. 下面我展示了从API网关容器内部到Java服务的请求,首先使用服务名称,然后使用已解析的IP。 It only replies with a correct response in the IP case. 它只回复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

The questions 问题

Is there something in the standard spring-boot-starter-web setup that prevents the web server from servicing the request, when the client adds the "Host: hello_java:8080" header? 当客户端添加“Host:hello_java:8080”标头时,标准spring-boot-starter-web设置中是否存在阻止Web服务器为请求提供服务的内容? Or why is the web server behaving differently in the two scenarios? 或者为什么Web服务器在两种情况下表现不同? And what can I do about it? 我能做些什么呢?

After some experimentation it turned out that the it was the underscore in the service name that caused the issue. 经过一些实验后发现它是服务名称中的下划线导致了这个问题。 Changing the service name to not have an underscore solved the problem. 将服务名称更改为没有下划线可以解决问题。

RFC 952 stipulates that "name" (Net, Host, Gateway, or Domain name) is a text string up to 24 characters drawn from the alphabet (AZ), digits (0-9), minus sign (-), and period (.) RFC 952规定“名称”(网络,主机,网关或域名)是一个文本字符串,最多24个字符,从字母表(AZ),数字(0-9),减号( - )和句点( 。)

It seems that the _ is not a valid component for host names. 似乎_不是主机名的有效组件。 It's a bit confusing because I had the same problem and when I ping app_server it's fine but when I wget from app_server I got bad request. 这有点令人困惑,因为我遇到了同样的问题,当我ping app_server时没关系,但是当我从app_server输入时,我收到了错误的请求。

Changing the underscore to minus fixed it for me. 将下划线更改为减去为我修复它。

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

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