Spring 使用 Docker-compose 部署的引导服务 - 内存问题:资源耗尽事件

Spring boot services deploying using Docker-compose - memory issue: Resource exhaustion event

我正在使用 spring-boot 微服务构建应用程序并部署到 docker 容器中。我正在使用 docker-compose 机制将服务部署到容器中。

我正在使用:

当我打算使用 docker-compose up 命令将应用程序部署到容器中时,部署时其中一项服务出现以下错误。

Resource exhaustion event: the JVM was unable to allocate memory from the heap.

并打印一些数据。

我已经修改了 WSL2 的 docker 设置,并为其分配了更多内存,从 4GB 到 8GB,但仍然失败。

在分析日志时我发现了一件事:

部署的服务没有占用 docker-compose 文件中提到的内存。以下是日志和文件:

日志:

springboot-naming-server_1       | Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx86677K -XX:MaxMetaspaceSize=118122K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 700M, Thread Count: 250, Loaded Class Count: 18441, Headroom: 0%)
springboot-api-gateway_1         | Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx290293K -XX:MaxMetaspaceSize=119306K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 700M, Thread Count: 50, Loaded Class Count: 18650, Headroom: 0%)
role-service_1                   | Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx69418K -XX:MaxMetaspaceSize=135381K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 700M, Thread Count: 250, Loaded Class Count: 21488, Headroom: 0%)
user-service_1                   | Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx48733K -XX:MaxMetaspaceSize=156066K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 700M, Thread Count: 250, Loaded Class Count: 25140, Headroom: 0%)

-Xmx值不同。容器内存已经分配。

Docker-编写文件:

version: '3.8'

services:
  rabbitmq:
    image: rabbitmq:3.8.14-management
    mem_limit: 300m
    ports:
      - "5672:5672"
      - "15672:15672"
    networks:
      - test-network

  zipkin-server:
    image: openzipkin/zipkin:2.23
    ports:
      - "9411:9411"
    deploy:
      resources:
        limits:
          cpus: 0.15
          memory: 300m
    networks:
      - test-network
    depends_on:
      - rabbitmq
    restart: unless-stopped
    environment:
      - RABBIT_URI=amqp://guest:guest@rabbitmq:5672
    
  redis-cache-server:
    image: redis
    command: ['redis-server', '--requirepass', 'aaaxxxcccvvv']
    deploy:
      resources:
        limits:
          memory: 356m
    ports:
      - "6379:6379"
    networks:
      - test-network

  springboot-naming-server:
    image: myapp/test-app-naming-server:1.0
    deploy:
      resources:
        limits:
          cpus: 0.15
          memory: 700m
    ports:
      - "8761:8761"
    networks:
      - test-network
      
  springboot-api-gateway:
    image: myapp/test-app-api-gateway:1.0
    deploy:
      resources:
        limits:
          cpus: 0.15
          memory: 700m
    ports:
      - "8765:8765"
    networks:
      - test-network
    depends_on: 
      - springboot-naming-server
      - rabbitmq
    environment:
      - EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://springboot-naming-server:8761/eureka
      - RABBIT_URI=amqp://guest:guest@rabbitmq:5672
      - SPRING_RABBITMQ_HOST=rabbitmq
      - SPRING_ZIPKIN_SENDER_TYPE=rabbit

  role-service:
    image: myapp/test-roleservice:1.0
    deploy:
      resources:
        limits:
          memory: 700m
    ports:
      - "8083:8083"
    networks:
      - test-network
    depends_on: 
      - springboot-naming-server
      - springboot-api-gateway
      - rabbitmq
    environment:
      - EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://springboot-naming-server:8761/eureka
      - SPRING.ZIPKIN.BASE-URL=http://zipkin-server:9411
      - RABBIT_URI=amqp://guest:guest@rabbitmq:5672
      - SPRING_RABBITMQ_HOST=rabbitmq
      - SPRING_ZIPKIN_SENDER_TYPE=rabbit
            
  user-service:
    image: myapp/test-userservice:1.0
    deploy:
      resources:
        limits:
          memory: 700m
    ports:
      - "8084:8084"
    networks:
      - test-network
    depends_on: 
      - springboot-naming-server
      - springboot-api-gateway
      - redis-cache-server
      - rabbitmq   
    environment:
      - EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://springboot-naming-server:8761/eureka
      - SPRING.ZIPKIN.BASE-URL=http://zipkin-server:9411
      - RABBIT_URI=amqp://guest:guest@rabbitmq:5672
      - SPRING_RABBITMQ_HOST=rabbitmq
      - SPRING_ZIPKIN_SENDER_TYPE=rabbit

  web-app:
    build:
      context: .
      dockerfile: Dockerfile.Web
    ports: 
      - "8081:8080"
    networks:
      - test-network
    depends_on: 
      - springboot-naming-server
      - springboot-api-gateway
      - redis-cache-server
      - rabbitmq      
    command: ['catalina.sh', 'run']
    environment:
      - EUREKA.CLIENT.SERVICEURL.DEFAULTZONE=http://springboot-naming-server:8761/eureka
      - SPRING.REDIS.HOST=redis-cache-server
      - SPRING.ZIPKIN.BASE-URL=http://zipkin-server:9411
      - RABBIT_URI=amqp://guest:guest@rabbitmq:5672
      - SPRING_RABBITMQ_HOST=rabbitmq
      - SPRING_ZIPKIN_SENDER_TYPE=rabbit
    
networks:
  test-network:

我不明白为什么每个服务不能平等地获得内存。因此,我的最后一个服务“userservice”总是失败,并出现与内存相关的错误(上面已经提到)。

谁能给我一些指示,我需要检查与 docker 或 spring-boot 等相关的任何配置或设置等

谢谢,

阿图尔

我能解决

问题就像我在问题中提到的那样。虽然在启动时每个服务都占用大量内存(请参阅问题部分中的日志),因此其余服务没有剩余内存,并且由于其余服务在启动时失败并显示与内存相关的消息.

我对 docker-compse.yml 文件所做的更改是:

environment:
      - JAVA_TOOL_OPTIONS=-Xmx128000K
deploy:
  resources:
    limits:
      memory: 800m

我做了这 2 项更改,然后根据要求分配了内存,现在每个服务都启动了。

我发现(可能是错误的观察)docker compose 启动服务,它只查找该服务并分配内存。当其他服务寻找内存时,他们无法获得内存,因为早期的服务几乎占用了大量内存。

谢谢,

阿图尔