【问题标题】:Improve GoogleAppEngine Spring web application startup time改进 GoogleAppEngine Spring Web 应用程序启动时间
【发布时间】:2016-01-11 03:48:48
【问题描述】:

我有一个在 GAE 中运行的 Java Web 应用程序。我使用 Spring 进行 servlet 调度。我使用它是为了可以使用注解来定义我的 servlet 中的调用,并让它完成所有的参数解析和结果转换。 GAE 中 Web 应用程序的加载时间约为 10 秒,我想知道是否有办法缩短它。谢谢。

这是我的 web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">


<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
    <servlet-name>publisher-servlet</servlet-name>
    <servlet-class>
                com.example.webapp.PublisherServlet
            </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>publisher-servlet</servlet-name>
    <url-pattern>/publisher</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<context-param>
    <param-name>contextClass</param-name>
    <param-value>com.example.webapp.CustomXmlWebApplicationContext</param-value>
</context-param>

还有我的 mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven />

<bean class="com.y2apps.quoteformessenger.webapp.ClientController">
</bean>

这是启动服务器的第一次调用的服务器日志

I 2015-10-13 14:19:54.937  200 119.69 KB 10.98 s I 14:19:59.222 I 14:20:05.916 /getallcategorylists?typeId=2
             84.229.82.245 - - [13/Oct/2015:04:19:54 -0700] "GET /getallcategorylists?typeId=2 HTTP/1.1" 200 122562 - "okhttp/2.2.0" "iron-core-93812.appspot.com" ms=10978 cpu_ms=13017 cpm_usd=0.01369737 instance=00c61b117c057f572d9967e34ef8e65bb7cbfdcd app_engine_release=1.9.27 trace_id=-
I 14:19:59.222 javax.servlet.ServletContext log: Initializing Spring FrameworkServlet 'mvc-dispatcher'
I 14:20:05.916 This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.

【问题讨论】:

  • 你考虑过Warmup Request吗?它不会缩短加载时间,而是将其隐藏:“在任何实时请求到达该实例之前,预热请求将应用程序代码加载到新实例中。”
  • 假设你也有一个 COntextLoaderListener 停止加载你的 bean 两次。
  • 启动应用引擎需要一段时间,这取决于运气/负载,而且 spring 本身也不是最快的,特别是如果你让它做很多自动的事情,比如类路径扫描。你做的动态越少,它应该越快。您可以在本地进行测试。
  • 谢谢安迪。我在下面回复了 Patrice,为什么这不能解决我的问题。
  • 谢谢 M. Deinum。你是什​​么意思?我如何加载 bean 两次?

标签: java xml spring google-app-engine spring-mvc


【解决方案1】:

我尝试了所有方法,但无法缩短 10 秒的启动时间。我认为它对我来说太重要了,所以我从服务器中删除了 spring,并将启动时间从 10 秒缩短到 4-5 秒。

【讨论】:

    【解决方案2】:

    正如Andy Turner 所提到的,跳过漫长的启动时间的最佳选择是Warmup Requests,这将...嗯,“预热”您的实例,并预加载某些需要加载的内容,这意味着当第一个请求进来时,您的实例将“准备好开始运行”。

    其他建议(不要两次加载您的 bean 并尽量减少类路径扫描)也肯定会有所帮助。除了这三个建议,您无能为力。

    【讨论】:

    • 谢谢帕特里斯,问题是我的网络应用程序服务于一个移动应用程序,它发送的第一个请求需要 10 秒。问题是我事先不知道用户会打开应用程序。我可以一直发送这些请求,但这只是意味着我会让服务器一直运行,这会花钱(而且我可以在没有虚假请求的情况下做到这一点)。
    • @yaronbic 那么也许改变你的缩放以保持最小空闲实例 1 可以工作吗?或者....我知道python(和go)与java相比几乎没有加载时间......可能对你不利,因为你不想改变你的整个应用程序,但取决于你走了多远,可能是一个解决方案
    • 谢谢帕特里斯。是的,我现在不会从头开始构建它,但是我对 python 不了解,也许我会在下一个中使用它。谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-02-27
    • 2016-03-12
    • 2020-08-16
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多