Spring Cloud Gateway下的GC停顿排查之旅
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class GcPauseEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Map<String, Object> gcProperties = new HashMap<>();
gcProperties.put("spring.cloud.gateway.filter.RemoveSingleUseFilterGatewayFilterFactory.remove-filter-1.args[0]", "Dubbo");
gcProperties.put("spring.cloud.gateway.filter.RemoveSingleUseFilterGatewayFilterFactory.remove-filter-1.args[1]", "reference.enabled");
gcProperties.put("spring.cloud.gateway.filter.RemoveSingleUseFilterGatewayFilterFactory.remove-filter-1.args[2]", "false");
gcProperties.put("spring.cloud.gateway.filter.RemoveSingleUseFilterGatewayFilterFactory.remove-filter-1.args[3]", "true");
// 添加GC参数,用于模拟GC停顿排查
gcProperties.put("JAVA_OPTS", "-Xms256m -Xmx256m -XX:+UseG1GC -XX:MaxGCPauseMillis=100");
environment.getPropertySources().addLast(new MapPropertySource("gcPauseSimulation", Collections.unmodifiableMap(gcProperties)));
}
public static void main(String[] args) {
SpringApplication.run(GcPauseEnvironmentPostProcessor.class, args);
}
}
这段代码定义了一个EnvironmentPostProcessor
,用于在Spring Boot应用启动时修改环境配置。在这个例子中,我们模拟了GC参数的设置,使得JVM使用G1垃圾收集器,并尝试将最大GC暂停时间控制在100毫秒左右。这有助于开发者在生产环境之前模拟和识别潜在的GC问题。
评论已关闭