Jetty使用心得筆記 [转]

本文分享了作者使用 Jetty 的心得,并详细介绍了 Jetty 的目录结构、启动方式、配置参数等内容。同时,还提供了限制 Jetty 记忆体大小的方法及 JConsole 远程监控 Jetty 的两种方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jetty使用心得筆記

標籤: Jetty, 程式語言

儿個月前試用了一陣子Jetty,當時對Jetty印象蠻好的。如果需要的是一個非常輕量或是可模組化設定的APServer,可以考慮這東東。不過...我自己用一陣子後還是換回Tomcat就是了...XD...這不是說Jetty不好,而是因為剛好Tomcat上有一些現成WebbaseAPServer monitor工具,但是Jetty沒有。我又蠻需要這些工具,所以就還是換回Tomcat了。有時...好維護比高效來的重要呀...<(T_T)>

癈言結束,Jetty筆記(7.4.5版本為基準)正文開始~~~

================================================================== 

快速上手簡介
請參考JavaWorld@TW這篇文章
================================================================== 

使用的好處,我個人覺的有這儿點
1.
速度快
2.
功能模組化,可以只啟用想要用的功能(e.g.讓它只支援Servlet,不支援JSP)
3.
還有很多,請上官網查詢
================================================================== 

Jetty比較重要的子目錄與相關說明如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

Z:\JETTY-7.4.5

├─bin --> 存放Jetty的啟動批次檔(只有linux版的bash script)。此目錄等同於tomcat的/bin

├─contexts --> 存放hot deploy相關設定檔(e.g. 如果有一個app叫test.war,可在這裡寫個test.xml設定檔,檔內可設定ContextPath相關設定。此目錄等同於tomcat的/conf

├─contexts-available

├─etc --> 存放Jetty各功能模組的設定檔(e.g. jetty.xml、jetty-ssl.xml),其中最重要的一個是jetty.xml,Jetty在啟動時一定要載入此設定檔。

├─lib

│  ├─annotations

│  ├─ext

│  ├─jndi

│  ├─jsp

│  ├─jta

│  └─policy

├─logs

├─overlays

│  ├─instances

│  ├─nodes

│  ├─templates

│  └─webapps

├─resources

└─webapps --> 存放各個webapp(war檔格式)的地方。此目錄等同於tomcat的/webapps

================================================================== 
Jetty啟動方式
1.Standalone
:在Console下指令啟動
2.Embedded
:寫JavaCode呼叫Jetty相關class來啟動(可參考這裡的文件)

Standalone
方式啟動與關閉Jetty 

?

1

2

啟動指令:java -jar start.jar

關閉指令:直接按[Ctrl + C]

也可以用指定StopPort的方式來啟動與關閉Jetty,範例如下 

?

1

2

3

4

啟動指令:java -DSTOP.PORT=8888 -DSTOP.KEY=jetty-password -jar start.jar

啟動指令:java -DSTOP.PORT=8888 -DSTOP.KEY=jetty-password -jar start.jar --stop

 

註:"STOP.PORT"用來指定要停止Jetty時所用的port,"STOP.KEY"用來指定要停止Jetty時,所用的密碼


輸入"java-jar start.jar --help",可以得到JettyConsole指令下的所有用法與參數說明。其中除了上述啟動與關閉Jetty的指令外,還有儿項比較有用的指令,分述如下。 

?

1

2

"java -jar start.jar --list-options"指令:列出Jetty的"OPTIONS"參數可以設定的設定值清單

"java -jar start.jar --dry-run"指令:Print the command line that the start.jar generates,then exit. This may be used to generate command lines(不啟動Jetty,只列出啟動Jetty時,jvm實際上被呼叫的Consle指令)

================================================================== 
"java -jar start.jar"指令的啟動流程如下
step1.
載入/JettyInstallFolder/start.ini設定檔
step2.
依據start.ini內容戴入各項Jetty設定
step3.
載入/JettyInstallFolder/etc/webdefault.xml的設定供"所有"webapp使用
step4.
載入各個webappweb.xml設定

start.ini
的內容其實只是指定Jetty要戴入哪些功能模組(e.g."etc/jetty.xml""etc/jetty-ssl.xml"),如果針對這些模組有客制化的需求,必需到各別模組設定檔中調整。

webdefault.xml
的設定值會在所有webapp啟動前被載入,如果有各別webappweb.xml裡都一致的設定,可以將它移至此設定檔。

jetty.xml
裡比較重要的參數說明如下

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<Call name="addConnector">

  <Arg>

   <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">

  <!-- 指定http connection port -->

  <Set name="port"><Property name="jetty.port" default="8080"/></Set>

   

  <!-- 指定connection time out的等待時間,單位為millisec -->

  <Set name="maxIdleTime">43200000</Set>

   

  <!-- 指定同一時間可以處理多少個request,同一時間要處理多個client的需求時,此值要加大。此值的設定跟CPU有關,CPU不夠力時,這個值不能設太大 -->

  <Set name="Acceptors">2</Set>

   

  <!-- 指定https connection port -->

  <Set name="confidentialPort">8443</Set>

   </New>

  </Arg>

</Call>


webdefault.xml
裡比較重要的參數說明如下 

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

acceptRanges      If true, range requests and responses are

                  supported

 

dirAllowed        If true, directory listings are returned if no

                  welcome file is found. Else 403 Forbidden.

 

welcomeServlets   If true, attempt to dispatch to welcome files

                  that are servlets, but only after no matching static

                  resources could be found. If false, then a welcome

                  file must exist on disk. If "exact", then exact

                  servlet matches are supported without an existing file.

                  Default is true.

 

                  This must be false if you want directory listings,

                  but have index.jsp in your welcome file list.

 

redirectWelcome   If true, welcome files are redirected rather than

                  forwarded to.

 

gzip              If set to true, then static content will be served as

                  gzip content encoded if a matching resource is

                  found ending with ".gz"

 

resourceBase      Set to replace the context resource base

 

resourceCache     If set, this is a context attribute name, which the servlet

                  will use to look for a shared ResourceCache instance.

                       

relativeResourceBase

                  Set with a pathname relative to the base of the

                  servlet context root. Useful for only serving static content out

                  of only specific subdirectories.

 

aliases           If True, aliases of resources are allowed (eg. symbolic

                  links and caps variations). May bypass security constraints.

 

maxCacheSize      The maximum total size of the cache or 0 for no cache.

maxCachedFileSize The maximum size of a file to cache

maxCachedFiles    The maximum number of files to cache

 

useFileMappedBuffer

                  If set to true, it will use mapped file buffer to serve static content

                  when using NIO connector. Setting this value to false means that

                  a direct buffer will be used instead of a mapped file buffer.

                  By default, this is set to true.

 

cacheControl      If set, all static content will have this value set as the cache-control

                  header.


如果希望Jetty啟動時不要載入start.ini裡的設定,可以先刪除start.ini設定檔,並在啟動時指定明確的載入參數(e.g."java -jar start.jar OPTIONS=Server,jsp,jmx etc/jetty.xml/etc/jetty-jmx.xml"),這樣就不會載入start.ini裡的設定。不過在start.ini設定檔中設好各項設定是比較方便的作法。 
==================================================================
Webapp
若要deployJetty,先將該webapp打包成war後,丟到webapps目錄裡即完成deploy動作(還有其它的方式,但是這是最簡單的方式)

如果要把某個webapp當成JettyRootApp(代表"http://localhost:8080/"url指到該webapp),可將該webapp更名成root.war放至"webapp"目錄,並刪除"contexts"目錄裡的所有的檔案與子目錄。接著再重啟Jettyserver,就會把root.war當成是RootApp
==================================================================
Jetty
eclipse上有官方的plugin可供安裝(參考這份文件)。以前聽人說官方的效能很差,整合不好,很容易就當掉,多數人比較推"run-jetty-run"這個plugin;不過我自已試了官方最新版倒是沒有這些問題,覺的可以推薦給大家試試。
================================================================== 

要限制Jetty使用的記憶體大小可以用下面的指令達成
java -Xms64m -Xmx128m -jar start.jar
==================================================================
使用JConsolereomote monitorjetty有二種方式,分述如下

<
方式1>:使用JVM本身提供的JMX功能,步驟如下

step1.
利用文字編輯器撰寫一個jmxremote.password 設定檔(參考下面範例內容),並設定檔案的讀寫權限(windows上執行的話,要確定硬碟的資料格式一定要是NTFSFAT32格式是無法設定檔案權限的。此外,Windows上改檔案權限方式比較麻煩(可參考這份文件),在Linux上則是用chmod指令去改)

?

1

user1 password1


step2.
利用文字編輯器撰寫一個jmxremote.access 設定檔(參考下面範例內容)

?

1

user1 readonly


step3.Jetty
在啟動時,利用下述的指令啟動JMX功能

?

1

2

3

4

5

6

7

8

9

java ^

-Dcom.sun.management.jmxremote ^

-Djava.rmi.server.hostname=127.0.0.1 ^

-Dcom.sun.management.jmxremote.port=1099 ^

-Dcom.sun.management.jmxremote.ssl=false ^

-Dcom.sun.management.jmxremote.authenticate=true ^

-Dcom.sun.management.jmxremote.access.file=jmxremote.access ^

-Dcom.sun.management.jmxremote.password.file=jmxremote.password ^

-jar start.jar


step4.Jetty
啟動後,可利用JConsle連進Jetty觀察系統狀況(Jconsole的連線設定範例如下)

?

1

2

3

url:127.0.0.1:1099

username:user1

password:password1


大濕大師有寫一篇<方式1>的相關文章,建議大家去看一看,了解其中的一些細節

<
方式2>:使用Jetty提供的JMX功能,步驟如下

step1.
start.ini內把jetty-jmx.xml設定檔啟用。在start.ini設定檔裡"Configurationfiles"設定區塊的第一行加進"etc/jetty-jmx.xml"(一定要放在這個區塊的第一行,不然JMX功能會失效)

step2.
jetty-jmx.xml設定檔內把所有mark起來的功能unmark,啟用RMI功能。

Jetty
本身提供的功能無法設定以帳號、密碼方式來進行登入驗証,只要連上線就直接登入系統,使用上要小心。
==================================================================
 Jetty
TestRealm功能是用來設定使用者群組與權限,相關說明可參考這裡
==================================================================
如果想對Jetty服務器架構及性能進行優化,可以參考之前我找到的這份Slide
================================================================== 

Jetty官網: http://www.eclipse.org/jetty/
Jetty
官方文件: http://wiki.eclipse.org/Jetty

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值