基于netty实现对springboot下容器的扩展

实现对springboot下容器的扩展

自定义容器的自动装配,可以在spring.factories指定。根据springboot-web自动装配默认是tomcat,可以排除掉tomcat加载自己的。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.kaddddd.gateway.netty.bootstrap.EmbeddedNettyAutoConfiguration

@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)

@Configuration

@ConditionalOnWebApplication // 在Web环境下才会起作用

public class EmbeddedNettyAutoConfiguration {

@Configuration

@ConditionalOnClass({Bootstrap.class}) // Netty的Bootstrap类必须在classloader中存在,才能启动Netty容器

@ConditionalOnMissingBean(value = {AbstractServletWebServerFactory.class, ServletWebServerFactory.class}, search = SearchStrategy.CURRENT) //当前Spring容器中不存在AbstractServletWebServerFactory接口的实例

public static class EmbeddedNetty {

//上述条件注解成立的话就会构造EmbeddedNettyFactory这个EmbeddedServletContainerFactory

@Bean

public NettyServletWebServerFactory embeddedNettyFactory() {

return new NettyServletWebServerFactory();

}

}

}

对webserver工厂的实现AbstractServletWebServerFactory,重写getWebServer(ServletContextInitializer... initializers),返回自定义的netty容器NettyHttpServer。并初始化NettyServletContext implements ServletContext。

实现ServletRegistration.Dynamic接口,将servletMapping添加到NettyServletContext中。

org.springframework.boot.SpringApplication =>run

public ConfigurableApplicationContext run(String... args) {

...

try {

...

prepareContext(context, environment, listeners, applicationArguments, printedBanner);

refreshContext(context);

afterRefresh(context, applicationArguments);

} catch (Throwable ex) {

handleRunFailure(context, ex, exceptionReporters, listeners);

throw new IllegalStateException(ex);

}

return context;

}

org.springframework.boot.SpringApplication =>refreshContext

private void refreshContext(ConfigurableApplicationContext context) {

if (this.registerShutdownHook) {

try {

context.registerShutdownHook();

}

catch (AccessControlException ex) {

// Not allowed in some environments.

}

}

refresh((ApplicationContext) context);

}

调用spring核心refresh方法中的onRefresh();

public void refresh() throws BeansException, IllegalStateException {

synchronized (this.startupShutdownMonitor) {

// Prepare this context for refreshing.

prepareRefresh();

// Tell the subclass to refresh the internal bean factory.

ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

// Prepare the bean factory for use in this context.

prepareBeanFactory(beanFactory);

try {

// Allows post-processing of the bean factory in context subclasses.

postProcessBeanFactory(beanFactory);

// Invoke factory processors registered as beans in the context.

invokeBeanFactoryPostProcessors(beanFactory);

// Register bean processors that intercept bean creation.

registerBeanPostProcessors(beanFactory);

// Initialize message source for this context.

initMessageSource();

// Initialize event multicaster for this context.

initApplicationEventMulticaster();

// Initialize other special beans in specific context subclasses.

onRefresh();

// Check for listener beans and register them.

registerListeners();

// Instantiate all remaining (non-lazy-init) singletons.

finishBeanFactoryInitialization(beanFactory);

// Last step: publish corresponding event.

finishRefresh();

}

catch (BeansException ex) {

if (logger.isWarnEnabled()) {

logger.warn("Exception encountered during context initialization - " +

"cancelling refresh attempt: " + ex);

}

// Destroy already created singletons to avoid dangling resources.

destroyBeans();

// Reset 'active' flag.

cancelRefresh(ex);

// Propagate exception to caller.

throw ex;

}

finally {

// Reset common introspection caches in Spring's core, since we

// might not ever need metadata for singleton beans anymore...

resetCommonCaches();

}

}

}

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext=>onRefresh()

@Override

protected void onRefresh() {

super.onRefresh();

try {

createWebServer();

}

catch (Throwable ex) {

throw new ApplicationContextException("Unable to start web server", ex);

}

}

org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext=>createWebServer()

private void createWebServer() {

WebServer webServer = this.webServer;

ServletContext servletContext = getServletContext();

if (webServer == null && servletContext == null) {

ServletWebServerFactory factory = getWebServerFactory();

this.webServer = factory.getWebServer(getSelfInitializer());

...

}

else if (servletContext != null) {

try {

getSelfInitializer().onStartup(servletContext);

}

catch (ServletException ex) {

throw new ApplicationContextException("Cannot initialize servlet context", ex);

}

}

initPropertySources();

}

ServletWebServerFactory factory = getWebServerFactory();

this.webServer = factory.getWebServer(getSelfInitializer());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值