当其他docker-compose服务通过服务名称请求时,Spring Boot REST应用返回400 [英] Spring Boot REST app returns 400 when requested from other docker-compose service by service name

查看:98
本文介绍了当其他docker-compose服务通过服务名称请求时,Spring Boot REST应用返回400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我们的开发设置中引入Spring Boot REST服务.开发设置使用docker-compose和API网关来公开同一域(即localhost)上的各个服务.

I'm trying to introduce a Spring Boot REST service in our development setup. The development setup is using docker-compose and an API gateway to expose the individual services on the same domain (ie. localhost).

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

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文件,因此下面介绍了Spring Boot Java服务.该服务基于spring-boot-starter-parent(2.0.3.RELEASE)和spring-boot-starter-web.我没有配置任何与Web服务器相关的内容(除了添加server.server-header属性以确保自己正在访问我的服务之外).

I've edited our docker-compose file, so it looks like the below to introduce the Spring Boot Java service. The service is based on spring-boot-starter-parent (2.0.3.RELEASE) and 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).

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,并使用该新服务也可以正确响应.

The behaviour

If I request the hello service from outside the containers (e.g. in my browser on localhost:8301) it replies back correctly. 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.

下面,我展示了从API网关容器内部到Java服务的请求,首先通过使用服务名称,然后再使用已解析的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. It only replies with a correct response in the IP case.

# 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服务器的行为都不同?那我该怎么办?

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? Or why is the web server behaving differently in the two scenarios? 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.

这篇关于当其他docker-compose服务通过服务名称请求时,Spring Boot REST应用返回400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆