当前位置: 首页 > news >正文

wordpress 媒体库空白上海排名优化seo

wordpress 媒体库空白,上海排名优化seo,做网站的目的是啥,网站开发小组通过深入分析SpringBoot中WebServer的启动流程,插入自定义的Loading页面展示逻辑,优化软件使用时的用户体验。 背景 Java本身的特点,再加上开发人员能力差,软件开发工程化程度低等等问题,经过一段时间的迭代之后&…

通过深入分析SpringBoot中WebServer的启动流程,插入自定义的Loading页面展示逻辑,优化软件使用时的用户体验。

背景

Java本身的特点,再加上开发人员能力差,软件开发工程化程度低等等问题,经过一段时间的迭代之后,经常会出现的一个问题就是应用启动越来越慢。

过往我也是收集了不少相关资料,例如 云音乐服务端应用启动时间下降40%实践分享,7min到40s: SpringBoot 启动优化实践!等等。

本文跳出与上述文章类似的技术角度,借鉴Jenkins的启动流程 —— Jenkins也是Java开发的,而且其启动速度虽然经过不少优化,但依然有着非常明显的延迟,但Jenkins通过加入Loading页面的方式,极大提升了用户在等待系统就绪期间的用户体验 —— 启动之后马上呈现给用户一个Loading页面,待后台服务能够正常提供服务之后冲向到常规Index页面。

实现

Jenkins默认适用的是winstone 容器,所以我在一开始所设想的"拿来主义"被掐死在了摇篮里,最后经过一番摸索,基于SpringBoot + Undertow实现了类似的效果。

思路:

  1. SpringBoot项目中,单单一个WebServer的启动是很快的,只是SpringBoot的启动流程里,其会在WebServer启动之前初始化所有的Bean,而这是整个启动流程里最耗时的。
  2. 所以,我们将在SpringBoot容器中的Bean实例化之前启动我们自定义的"minimal Undertow Server"来向用户提供对于 loading页面的响应。
  3. 待Spring容器准备进行WebServer的启动时,停止我们的"minimal Undertow Server",以避免端口占用。
  4. 前端loading页面将定期轮询后端服务启动情况,待其正常响应时跳转到相应的主体服务页面。

直接上代码:

// ====================== 1. 应用启动入口类// 应用启动入口public static void main(String[] args) {final SpringApplication springApplication = new SpringApplication(SpringBootTestApplication.class);// 读取用户配置, 决定启动方式final Boolean humanableStart = Convert.toBool(CommonUtil.getProperty("START_HUMANABLE", "false"));if (humanableStart) {springApplication.setApplicationContextClass(AnnotationConfigServletWebServerApplicationContextEx.class); // 自定义}// 启动Spring容器springApplication.run(args);}// ======================== 2. SpringBoot不同版本实现方式不同
// 这里以 SpringBoot 2.3.3为例, 更高版本需要实现 ApplicationContextFactory 接口
class AnnotationConfigServletWebServerApplicationContextWithHumanableStart extends AnnotationConfigServletWebServerApplicationContext {Undertow minimalUndertowserver;@Overrideprotected void prepareRefresh() {// 尽量提前"minimal Undertow Server"的创建, 优化用户体验.String property = this.getEnvironment().getProperty("server.port");minimalUndertowserver = minimalUndertowserver(Convert.toInt(property));super.prepareRefresh();}@Overrideprotected void onRefresh() {// ServletWebServerApplicationContext正是通过覆写本方法来实现 WebServer 创建的// 同时会向容器中注入webServerStartStop Bean,借助Spring的生命周期回调接口SmartLifecycle来负责将webServer的开启和关闭;super.onRefresh();}@Overrideprotected void finishRefresh() {// 关键流程:  AbstractApplicationContext.refresh()// super.finishRefresh()中将触发 WebServerStartStopLifecycle.start() 以启动webserver, 所以我们得在它之前将我们的轻量级webserver关闭掉.minimalUndertowserver.stop();super.finishRefresh();}static Undertow minimalUndertowserver(int port) {// 这个loading.html是从jenkins里扒过来了, 也算是实现了部分"拿来主义"final String loadingHtml = ResourceUtil.readStr("static/loading.html", CharsetUtil.CHARSET_UTF_8);// Start the minimal Undertow serverUndertow undertow = Undertow.builder().addHttpListener(port, "0.0.0.0").setHandler(new HttpHandler() {@Overridepublic void handleRequest(HttpServerExchange exchange) throws Exception {exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");exchange.getResponseSender().send(loadingHtml);}}).build();undertow.start();return undertow;}
}

原理分析

Spring容器之所以启动慢,主要原因肯定就是各类Bean的实例化耗时叠加

// AbstractApplicationContext.java
// Spring核心启动流程
@Override
public void refresh() throws BeansException, IllegalStateException {synchronized (this.startupShutdownMonitor) {// Prepare this context for refreshing.prepareRefresh();  // 我们在这里启动 minimal Undertow Server// 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();  // 在这里关闭 minimal Undertow Server.} 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();}}
}

相关

  1. Jenkins源码 - loading页面
  2. Jenkins源码 - loading后端代码
  3. GitHub - jenkinsci-winstone
http://www.ds6.com.cn/news/68122.html

相关文章:

  • 天猫店的网站怎么做的sem是什么显微镜
  • 医院网站建设方案计划书google play应用商店
  • 电子商务网站的建设报告市场营销推广策划方案
  • 杭州高端网站建设搜索引擎营销的案例
  • wordpress注册登录问题网站seo快速排名优化的软件
  • 怎么做网站首页关键词深圳百度推广竞价托管
  • 网站后台的关键词福州关键词排名软件
  • 网站建设与设计实验报告市场调研报告范文模板
  • 网站网页能自己做吗网站排名seo教程
  • app开发定制外包22宁阳网站seo推广
  • 网站读取错误时怎样做网络营销策略优化
  • 哪个网站教做饭做的好交换友情链接的意义是什么
  • 网站申请免费培训心得模板
  • 微信网站建设热线免费公司网站建站
  • 做网站的成本在哪广州营销课程培训班
  • 国内ui做的好的网站有哪些如何做好网络销售技巧
  • 有没有教做黄色网站厦门seo推广优化
  • 亚运会110周年庆典在杭州举行长沙网站优化方法
  • 苏州企业网站建设专家站点查询
  • 网站代付系统怎么做放心网站推广优化咨询
  • 北京免费网站建设模板大数据培训班出来能就业吗
  • 水友做的yyf网站全网热度指数
  • 旧域名新网站品牌推广的方式
  • 做前端的女生压力大吗seo黑帽培训骗局
  • 网站h1标签的应用好的竞价托管公司
  • 免费建网站系统西安百度网站排名优化
  • 郑州网站建设包括哪些品牌推广是做什么的
  • 河北网站建设报价韶山seo快速排名
  • 河北省住房与建设厅网站首页自助建站模板
  • 市桥做网站的公司做网络推广