Spring Cloud Gateway 启动流程源码分析
以下分析以 spring-cloud-starter-gateway 4.1.0 源码为分析样本。
配置和启动类
如果我们要使用 Spring Cloud Gateway,需要在pom里引入如下依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> <version>4.1.0</version> </dependency>
spring-cloud-starter-gateway里的依赖如下:
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gateway-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> <optional>true</optional> </dependency> </dependencies>
看上面引入了 spring-boot-starter-webflux,为后面分析做铺垫;
除了引入依赖,我们还需要有一个启动类,如下:
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@Slf4j
@SpringBootApplication
public class XXGatewayApp {
public static void main(String[] args) {
SpringApplication.run(XXGatewayApp.class, args);
log.info("XX网关启动成功!");
}
}启动 nettyserver
在主类启动后,是如何启动一个nettyserver的,我们来分析一下;跟踪启动类代码来到如下方法:
org.springframework.boot.SpringApplication#run(java.lang.String…)
public ConfigurableApplicationContext run(String... args) {
Startup startup = SpringApplication.Startup.create();
if (this.registerShutdownHook) {
shutdownHook.enableShutdownHookAddition();
}
... 省略代码...
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
Banner printedBanner = this.printBanner(environment);
// @A1 创建 ConfigurableApplicationContext
context = this.createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
// @A2 触发创建server
this.refreshContext(context);
... 省略代码...
} catch (Throwable ex) {
if (ex instanceof AbandonedRunException) {
throw ex;
}
this.handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
if (context.isRunning()) {
listeners.ready(context, startup.ready());
}
return context;
} catch (Throwable ex) {
if (ex instanceof AbandonedRunException) {
throw ex;
} else {
this.handleRunFailure(context, ex, (SpringApplicationRunListeners)null);
throw new IllegalStateException(ex);
}
}
}@A1:这个方法会执行以下逻辑
org.springframework.boot.WebApplicationType#deduceFromClasspath
计算web容器类型,而最上面的pom依赖引入了 spring-boot-starter-webflux
static WebApplicationType deduceFromClasspath() {
if (ClassUtils.isPresent("org.springframework.web.reactive.DispatcherHandler", (ClassLoader)null) && !ClassUtils.isPresent("org.springframework.web.servlet.DispatcherServlet", (ClassLoader)null) && !ClassUtils.isPresent("org.glassfish.jersey.servlet.ServletContainer", (ClassLoader)null)) {
return REACTIVE;
} else {
for(String className : SERVLET_INDICATOR_CLASSES) {
if (!ClassUtils.isPresent(className, (ClassLoader)null)) {
return NONE;
}
}
return SERVLET;
}
}上面代码根据类路径中加入的依赖,返回REACTIVE,最终返回 org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext 对象
创建websever
接上面 @A2方法,会触发AnnotationConfigReactiveWebServerApplicationContext如下调用:
注:AnnotationConfigReactiveWebServerApplicationContext父类是
ReactiveWebServerApplicationContext#createWebServer
private void createWebServer() {
WebServerManager serverManager = this.serverManager;
if (serverManager == null) {
StartupStep createWebServer = this.getApplicationStartup().start("spring.boot.webserver.create");
String webServerFactoryBeanName = this.getWebServerFactoryBeanName();
//@B1 创建ReactiveWebServerFactory ,创建server的核心点
ReactiveWebServerFactory webServerFactory = this.getWebServerFactory(webServerFactoryBeanName);
createWebServer.tag("factory", webServerFactory.getClass().toString());
boolean lazyInit = this.getBeanFactory().getBeanDefinition(webServerFactoryBeanName).isLazyInit();
//@B2 WebServerManager构造方法创建server
this.serverManager = new WebServerManager(this, webServerFactory, this::getHttpHandler, lazyInit);
this.getBeanFactory().registerSingleton("webServerGracefulShutdown", new WebServerGracefulShutdownLifecycle(this.serverManager.getWebServer()));
//@B3 很绝的方法
this.getBeanFactory().registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this.serverManager));
createWebServer.end();
}
this.initPropertySources();
}@B1 方法很绕,会从ReactiveWebServerFactoryConfiguration里去获得NettyReactiveWebServerFactory的bean定义,而这个bean定义依赖 ReactorResourceFactory ,代码如下:
@Bean
NettyReactiveWebServerFactory nettyReactiveWebServerFactory(ReactorResourceFactory resourceFactory, ObjectProvider<NettyRouteProvider> routes, ObjectProvider<NettyServerCustomizer> serverCustomizers) {
NettyReactiveWebServerFactory serverFactory = new NettyReactiveWebServerFactory();
serverFactory.setResourceFactory(resourceFactory);
Stream var10000 = routes.orderedStream();
Objects.requireNonNull(serverFactory);
var10000.forEach((xva$0) -> serverFactory.addRouteProviders(new NettyRouteProvider[]{xva$0}));
serverFactory.getServerCustomizers().addAll(serverCustomizers.orderedStream().toList());
return serverFactory;
}也就是说在容器返回NettyReactiveWebServerFactory 对象前会把ReactorResourceFactory 对象初始化完毕;ReactorResourceFactory 这个类实现了InitializingBean,我们看看afterPropertiesSet方法初始化内容:
public void start() {
synchronized(this.lifecycleMonitor) {
if (!this.isRunning()) {
if (!this.useGlobalResources) {
if (this.loopResources == null) {
this.manageLoopResources = true;
this.loopResources = (LoopResources)this.loopResourcesSupplier.get();
}
if (this.connectionProvider == null) {
this.manageConnectionProvider = true;
this.connectionProvider = (ConnectionProvider)this.connectionProviderSupplier.get();
}
} else {
Assert.isTrue(this.loopResources == null && this.connectionProvider == null, "'useGlobalResources' is mutually exclusive with explicitly configured resources");
// @C1
HttpResources httpResources = HttpResources.get();
if (this.globalResourcesConsumer != null) {
this.globalResourcesConsumer.accept(httpResources);
}
this.connectionProvider = httpResources;
this.loopResources = httpResources;
}
this.running = true;
}
}
}@C1方法最终执行的是 reactor.netty.resources.LoopResources#create(java.lang.String)
static LoopResources create(String prefix) {
if (((String)Objects.requireNonNull(prefix, "prefix")).isEmpty()) {
throw new IllegalArgumentException("Cannot use empty prefix");
} else {
return new DefaultLoopResources(prefix, DEFAULT_IO_SELECT_COUNT, DEFAULT_IO_WORKER_COUNT, true);
}
}是不是很熟悉了,是创建的reactor.netty.resources.DefaultLoopResources 对象
todo~~
@B2 方法会调用到如下方法:
org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory#getWebServer
public WebServer getWebServer(HttpHandler httpHandler) {
// @D1 创建 HttpServer
HttpServer httpServer = this.createHttpServer();
ReactorHttpHandlerAdapter handlerAdapter = new ReactorHttpHandlerAdapter(httpHandler);
NettyWebServer webServer = this.createNettyWebServer(httpServer, handlerAdapter, this.lifecycleTimeout, this.getShutdown());
webServer.setRouteProviders(this.routeProviders);
return webServer;
}@D1 方法会执行到如下方法: org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory#createHttpServer
private HttpServer createHttpServer() {
HttpServer server = HttpServer.create().bindAddress(this::getListenAddress);
if (Ssl.isEnabled(this.getSsl())) {
server = this.customizeSslConfiguration(server);
}
if (this.getCompression() != null && this.getCompression().getEnabled()) {
CompressionCustomizer compressionCustomizer = new CompressionCustomizer(this.getCompression());
server = compressionCustomizer.apply(server);
}
server = server.protocol(this.listProtocols()).forwarded(this.useForwardHeaders);
return this.applyCustomizers(server);
}继续分析上面:@B3
这个地方太绝了,向容器中注入了一个 WebServerStartStopLifecycle,这种类型的类会被框架中触发start方法,触发一些列的start方法,最终执行的是 reactor.netty.http.server.HttpServerBind父类 ----> HttpServer 父类 -----> reactor.netty.transport.ServerTransport的 bindNow方法 ----> bind方法。
重要方法 reactor.netty.transport.ServerTransport#bind
public Mono<? extends DisposableServer> bind() {
CONF config = (CONF)(this.configuration());
Objects.requireNonNull(config.bindAddress(), "bindAddress");
Mono<? extends DisposableServer> mono = Mono.create((sink) -> {
SocketAddress local = (SocketAddress)Objects.requireNonNull((SocketAddress)config.bindAddress().get(), "Bind Address supplier returned null");
if (local instanceof InetSocketAddress) {
InetSocketAddress localInet = (InetSocketAddress)local;
if (localInet.isUnresolved()) {
local = AddressUtils.createResolved(localInet.getHostName(), localInet.getPort());
}
}
boolean isDomainSocket = false;
DisposableBind disposableServer;
if (local instanceof DomainSocketAddress) {
isDomainSocket = true;
disposableServer = new UdsDisposableBind(sink, config, local);
} else {
disposableServer = new InetDisposableBind(sink, config, local);
}
ConnectionObserver childObs = new ChildObserver(config.defaultChildObserver().then(config.childObserver()));
// @E1
Acceptor acceptor = new Acceptor(config.childEventLoopGroup(), config.channelInitializer(childObs, (SocketAddress)null, true), config.childOptions, config.childAttrs, isDomainSocket);
// @E2
TransportConnector.bind(config, new AcceptorInitializer(acceptor), local, isDomainSocket).subscribe(disposableServer);
});
if (config.doOnBind() != null) {
mono = mono.doOnSubscribe((s) -> config.doOnBind().accept(config));
}
return mono;
}@E1:最终调用 reactor.netty.resources.DefaultLoopResources#cacheNioServerLoops 获得work线程池
@E2:最终调用 reactor.netty.resources.DefaultLoopResources#cacheNioSelectLoops 获得boss线程池
问题来了:看reactor.netty.resources.LoopResources源码,如果系统参数里没有配置reactor.netty.ioSelectCount,则boss线程会和work线程池的AtomicReference在cacheNioSelectLoops 方法中返回同一对象;这是不是会造成线程池共用?
可以参看 https://blog.csdn.net/qq_42651904/article/details/134561804 这篇文章理解;
那么你们生产环境会单独设置 reactor.netty.ioSelectCount 参数吗?
jvisualvm监控验证
- 如果不设置reactor.netty.ioSelectCount 这个启动参数,通过监控网关启动后线程名称是 reactor-http-nio-xx

- 如果在启动的时候设置这个参数为1,启动的线程不一样 reactor-http-select-xx
public static void main(String[] args) {
System.setProperty("reactor.netty.ioSelectCount", "1");
SpringApplication.run(XXXGatewayApp.class, args);
log.info("XXX网关启动成功!");
}

设想一下,如果代码写得不好在GlobalFilter有阻塞写法,比如数据查询、redis查询,加上高并发请求,那么是不是会影响boss线程“接客”?
后面再通过压测的方式论证一下。
到此这篇关于Spring Cloud Gateway 启动流程源码分析的文章就介绍到这了,更多相关Spring Cloud Gateway 启动流程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
MyBatis 探秘之#{} 与 ${} 参传差异解码(数据库连接池筑牢数据交互
本文详细介绍了MyBatis中的`#{}`和`${}`的区别与使用场景,包括预编译SQL和即时SQL的区别、安全性问题,以及如何正确使用数据库连接池来提高性能,感兴趣的朋友一起看看吧2024-12-12
Java(Springboot)项目调用第三方WebService接口实现代码
这篇文章主要介绍了如何使用Java调用WebService接口,传递XML参数,获取XML响应,并将其解析为JSON格式,文中详细描述了WSDL文档的使用、HttpClientBuilder和Apache Axis两种调用方式的具体实现步骤,需要的朋友可以参考下2025-02-02
Java的MyBatis框架中对数据库进行动态SQL查询的教程
这篇文章主要介绍了Java的MyBatis框架中对数据库进行动态SQL查询的教程,讲解了MyBatis中一些控制查询流程的常用语句,需要的朋友可以参考下2016-04-04


最新评论