2015-10-13 53 views
2

我在GAE上運行的Java Web應用程序。我使用Spring進行servlet調度。我使用它,以便我可以使用批註來定義我的servlet中的調用,以便它執行所有參數分析和結果轉換。網絡應用程序在GAE中的加載時間約爲10秒,我想知道是否有辦法縮短這個時間。謝謝。提高GoogleAppEngine Spring的web應用程序的啓動時間

這裏是我的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-調度-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. 
+0

你有沒有考慮一個[預熱請求(https://cloud.google.com/appengine/docs/java/config/appconfig?hl=en#Java_Warmup_requests)?它不會縮短加載時間,但會隱藏它:「在任何活動請求到達該實例之前,熱身請求會將應用程序代碼加載到新實例中。」 –

+0

假設你也有一個'COntextLoaderListener'停止兩次加載你的bean。 –

+0

啓動在App Engine上需要依賴於運氣/負載和彈簧一會兒本身不是最快的兩種,特別是如果你把它做很多類似的類路徑掃描AUTOMAGIC事情。動力越差,它應該得到的越快。你可以在本地測試。 – zapl

回答

0

正如Andy Turner所提到的那樣,跳過這麼長啓動時間的最佳選擇是Warmup Requests,這將會......「熱身」您的實例,並預加載某些需要加載的內容,這意味着您的當第一個請求進來實例將「準備旗開得勝」。

其他建議(不加載你的bean兩次,並儘量減少類路徑掃描)肯定會有所幫助。除了這三個建議之外,你沒有什麼可以做的。

+0

謝謝Patrice,問題是我的web應用程序服務於一個移動應用程序,並且它發送的第一個請求需要10秒。事情是,我不知道用戶將打開應用程序。我可以一直髮送這些請求,但這只是意味着我會讓服務器始終運行,這將花費金錢(並且我可以在沒有虛假請求的情況下執行此操作)。 – yaronbic

+0

@yaronbic然後可能改變你的縮放比例以保持1分鐘的空閒實例可以工作?或....我知道python(和去)幾乎沒有加載時間相比,java ....可能不適合你,因爲你不想改變你的整個應用程序,但取決於你有多遠,可能是一個解決方案 – Patrice

+0

謝謝帕特里斯。是的,我現在不會從頭開始構建它,但我不知道python的相關知識,也許我會在下一個中使用它。謝謝 – yaronbic

1

我什麼都試過,無法縮短10秒啓動。我認爲這對我來說太重要了,所以我從服務器上卸下了彈簧,並將啓動時間從10秒縮短到4-5秒。