spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

no increase in client requests,the server's CPU usage increases day by day,

Open Maplejw opened this issue 9 months ago • 0 comments

version spring bood 3.2.7 spring cloud 2023.0.1 jdk 21 -server -Xmx8192m -Xms8192m -XX:MaxDirectMemorySize=6g -XX:+UseG1GC -XX:MaxGCPauseMillis=200 -XX:InitiatingHeapOccupancyPercent=45 -XX:ParallelGCThreads=7 -XX:ConcGCThreads=7 -Dreactor.netty.pool.leasingStrategy=lifo

Describe the bug QPS from 8K-11K every day no increase in client requests, the CPU usage of the gateway server gets higher day by day. However, when I restart the gateway service, the CPU usage immediately drops. I only added a filter to retrieve the client's IP address and pass it through to the backend. On 4.8, I restart the gateway server.

Image

I checked the GC(G1) and it appears to be normal. I check the direct memory, and it appears to be normal. I print the Netty client pool usage every 5 minutes, and it appears to be normal. I also check the java stack, and do not see block thread.

2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,active connections:1.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,active connections:1.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,active connections:0.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,idle connections:29.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,idle connections:26.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,idle connections:28.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,idle connections:24.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,idle connections:23.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,idle connections:26.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,idle connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,idle connections:25.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,max connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,total connections:26.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,total connections:32.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,total connections:26.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,total connections:29.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,total connections:29.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,total connections:23.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,total connections:26.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,total connections:24.0 
2025-04-08 08:50:00.000  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,max.pending connections:2.147483647E9 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847835500,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847833516,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846933028,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847834508,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846961145,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846902927,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1847832524,pending connections:0.0 
2025-04-08 08:50:00.001  INFO {[]} [            scheduling-1]       i.r.g.s.s.AppInfoSchedular: netty pool id:1846936996,pending connections:0.0 

My code: ClientHeaderFilter

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package igg.report.gateway.shell.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

public class ClientHeaderFilter implements GlobalFilter, Ordered {
    private static final Logger log = LoggerFactory.getLogger(ClientHeaderFilter.class);

    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String clientIp = this.getIpAddr(request);
        ServerHttpRequest modifiedRequest = request.mutate().header("IGG-PROXY-IP", new String[]{clientIp}).build();
        return chain.filter(exchange.mutate().request(modifiedRequest).build());
    }

    private String getIpAddr(ServerHttpRequest request) {
        String ip = request.getHeaders().getFirst("x-forwarded-for");
        if (StringUtils.isEmpty(ip)) {
            ip = request.getHeaders().getFirst("Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip)) {
            ip = request.getHeaders().getFirst("WL-Proxy-Client-IP");
        }

        if (StringUtils.isEmpty(ip)) {
            ip = request.getHeaders().getFirst("HTTP_CLIENT_IP");
        }

        if (StringUtils.isEmpty(ip)) {
            ip = request.getHeaders().getFirst("HTTP_X_FORWARDED_FOR");
        }

        if (StringUtils.isEmpty(ip)) {
            ip = request.getRemoteAddress().getAddress().getHostAddress();
        }

        if (ip != null && ip.indexOf(",") != -1) {
            String[] ipWithMultiProxy = ip.split(",");

            for(int i = 0; i < ipWithMultiProxy.length; ++i) {
                String eachIpSegement = ipWithMultiProxy[i];
                if (!"unknown".equalsIgnoreCase(eachIpSegement)) {
                    ip = eachIpSegement;
                    break;
                }
            }
        }

        return ip;
    }

    public int getOrder() {
        return RouteToRequestUrlFilter.ROUTE_TO_URL_FILTER_ORDER + 2;
    }
}

Maplejw avatar Apr 08 '25 09:04 Maplejw