You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker化Spring Cloud Gateway请求头过大问题求助

问题描述

本地运行应用时一切正常,但通过Docker Compose启动后,发送POST或GET请求(示例地址:GET http://localhost:8181/api/product)时,Postman返回431 Request Header Fields Too Large错误。已知需要修改请求头大小限制,但不清楚如何在Docker Compose容器中配置。

提供的Docker Compose配置

---
version: '3.7'
services:
  ## MySQL Docker Compose Config
  postgres-order:
    container_name: postgres-order
    image: postgres
    environment:
      POSTGRES_DB: order-service
      POSTGRES_USER: ptechie
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - ./postgres-order:/data/postgres
    expose:
      - "5431"
    ports:
      - "5431:5431"
    command: -p 5431
    restart: always

  postgres-inventory:
    container_name: postgres-inventory
    image: postgres
    environment:
      POSTGRES_DB: inventory-service
      POSTGRES_USER: ptechie
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - ./postgres-inventory:/data/postgres
    ports:
      - "5432:5432"
    restart: always

  ## Mongo Docker Compose Config
  mongo:
    container_name: mongo
    image: mongo:4.4.14-rc0-focal
    restart: always
    ports:
      - "27017:27017"
    expose:
      - "27017"
    volumes:
      - ./mongo-data:/data/db


  zookeeper:
    image: confluentinc/cp-zookeeper:7.0.1
    container_name: zookeeper
    ports:
      - "2181:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000

  broker:
    image: confluentinc/cp-kafka:7.0.1
    container_name: broker
    ports:
      - "9092:9092"
    depends_on:
      - zookeeper
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
      KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1

#  ## Zipkin
#  zipkin:
#    image: openzipkin/zipkin
#    container_name: zipkin
#    ports:
#      - "9411:9411"
#
  ## Eureka Server
  discovery-server:
    image: danicazdravkovic/discovery-server:latest
    container_name: discovery-server
    ports:
      - "8761:8761"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
#    depends_on:
#      - zipkin

  api-gateway:
    image: danicazdravkovic/api-gateway:latest
    container_name: api-gateway
    ports:
      - "8181:8080"
    expose:
      - "8181"
    environment:
      - SPRING_PROFILES_ACTIVE=docker
#      - LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_SECURITY= TRACE
    depends_on:
#      - zipkin
      - discovery-server


  ## Product-Service Docker Compose Config
  product-service:
    container_name: product-service
    image: danicazdravkovic/product-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    depends_on:
      - mongo
      - discovery-server
      - api-gateway

  ## Order-Service Docker Compose Config
  order-service:
    container_name: order-service
    image: danicazdravkovic/order-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-order:5431/order-service
    depends_on:
      - postgres-order
      - broker
#      - zipkin
      - discovery-server
      - api-gateway

  ## Inventory-Service Docker Compose Config
  inventory-service:
    container_name: inventory-service
    image: danicazdravkovic/inventory-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
      - SPRING_DATASOURCE_URL=jdbc:postgresql://postgres-inventory:5432/inventory-service
    depends_on:
      - postgres-inventory
      - discovery-server
      - api-gateway

  ## Notification-Service Docker Compose Config
  notification-service:
    container_name: notification-service
    image: danicazdravkovic/notification-service:latest
    environment:
      - SPRING_PROFILES_ACTIVE=docker
    depends_on:
#      - zipkin
      - broker
      - discovery-server
      - api-gateway

相关日志信息

2023-08-28 14:47:10 2023-08-28T12:47:10.695Z DEBUG 1 --- [or-http-epoll-3] .f.h.o.ObservedResponseHttpHeadersFilter : Will instrument the response
2023-08-28 14:47:10 2023-08-28T12:47:10.696Z DEBUG 1 --- [or-http-epoll-3] .f.h.o.ObservedResponseHttpHeadersFilter : The response was handled for observation {name=http.client.requests(null), error=null, context=name='http.client.requests', contextualName='null', error='null', lowCardinalityKeyValues=[http.method='GET', http.status_code='UNKNOWN', spring.cloud.gateway.route.id='product-service', spring.cloud.gateway.route.uri='lb://product-service'], highCardinalityKeyValues=[http.uri='http://localhost:8181/api/product'], map=[class io.micrometer.core.instrument.Timer$Sample='io.micrometer.core.instrument.Timer$Sample@745b2658', class io.micrometer.core.instrument.LongTaskTimer$Sample='SampleImpl{duration(seconds)=1.090919261, duration(nanos)=1.090919261E9, startTimeNanos=7682403671438}'], parentObservation={name=http.server.requests(null), error=null, context=name='http.server.requests', contextualName='null', error='null', lowCardinalityKeyValues=[exception='none', method='GET', outcome='SUCCESS', status='200', uri='UNKNOWN'], highCardinalityKeyValues=[http.url='/api/product'], map=[class io.micrometer.core.instrument.Timer$Sample='io.micrometer.core.instrument.Timer$Sample@159ad82d', class io.micrometer.core.instrument.LongTaskTimer$Sample='SampleImpl{duration(seconds)=1.097201581, duration(nanos)=1.097201581E9, startTimeNanos=7682397843526}'], parentObservation=null}}
2023-08-28 14:47:10 2023-08-28T12:47:10.697Z TRACE 1 --- [or-http-epoll-3] o.s.c.g.filter.NettyWriteResponseFilter  : NettyWriteResponseFilter start inbound: 2f267dd5, outbound: [690e1732-641] 
2023-08-28 14:47:10 2023-08-28T12:47:10.699Z TRACE 1 --- [or-http-epoll-3] o.s.c.g.filter.GatewayMetricsFilter      : spring.cloud.gateway.requests tags: [tag(httpMethod=GET),tag(httpStatusCode=431),tag(outcome=CLIENT_ERROR),tag(routeId=product-service),tag(routeUri=lb://product-service),tag(status=REQUEST_HEADER_FIELDS_TOO_LARGE)]

解决方案

从日志中的spring.cloud.gateway标识可以判断,问题出在API网关层。Spring Cloud Gateway基于Netty,默认的请求头大小限制较小,Docker环境下可能因服务发现或代理相关的额外请求头导致总大小超出限制。

只需在Docker Compose的api-gateway服务中添加环境变量,调整Netty的请求头大小限制即可:

修改api-gateway的environment部分,增加以下配置:

environment:
  - SPRING_PROFILES_ACTIVE=docker
  # 增大最大请求头大小(单位:字节,示例值16384可根据实际需求调整)
  - SPRING_CLOUD_GATEWAY_SERVER_NETTY_MAX_HTTP_HEADER_SIZE=16384
  # 可选:增大初始请求行的最大大小
  - SPRING_CLOUD_GATEWAY_SERVER_NETTY_INITIAL_LINE_MAX_SIZE=4096

修改后的完整api-gateway服务配置如下:

api-gateway:
  image: danicazdravkovic/api-gateway:latest
  container_name: api-gateway
  ports:
    - "8181:8080"
  expose:
    - "8181"
  environment:
    - SPRING_PROFILES_ACTIVE=docker
    - SPRING_CLOUD_GATEWAY_SERVER_NETTY_MAX_HTTP_HEADER_SIZE=16384
    - SPRING_CLOUD_GATEWAY_SERVER_NETTY_INITIAL_LINE_MAX_SIZE=4096
  depends_on:
    - discovery-server

配置完成后,重启Docker Compose服务:

docker-compose down && docker-compose up -d

内容的提问来源于stack exchange,提问作者Danica Zdravkovic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:55:56