Eclipse Jetty を起動させてみた

Eclipse Jetty

ダウンロード

  • Jetty本体をダウンロード
  • 11.0.6 を .tgz 形式でダウンロードした
  • jetty-home-11.0.6.tar.gz というファイルがダウンロードされる
  • /opt に解凍する
$ sudo tar -xvf jetty-home-11.0.6.tar.gz -C /opt
export JETTY_HOME="/opt/jetty-home-11.0.6"

アーキテクチャ

メインコンセプト

Jetty Modules

  • Jetty Moduleは協調し一つ以上の機能を提供するJavaコンポーネント
  • Jetty ModuleはJavaAPIを使って構成できる(Javaプログラムということ?)が、設定ファイルを使って宣言的にもできる(?)
  • Jetty Moduleは依存関係がある
    • httpモジュールはserverモジュールに依存しており、serverモジュールはtheadpool,loggingモジュールに依存している
  • Jetty Moduleを有効・無効にすることでJettyの動作を変更できる
    • 例えばhttpモジュールを有効にするとHTTPリクエストは処理するが、httpsモジュールが無効であればHTTPSリクエストは処理しない
  • カスタムモジュールを作成し、動作を変更できる
  • これらの仕組みによって、必要に応じJettyのサイズを小さくできる
    • 無効にされたモジュールはロードされずメモリを使わないから

$JETTY_HOME and $JETTY_BASE

  • 複数のJettyディストリビューションを複数の場所で管理する代わりに、$JETTY_HOME によってスタンドアローンのJettyのバイナリを分離して管理できる(?)
  • 特定の環境にカスタマイズされたものを $JETTY_BASE とする(?)
  • Jetty本体の場所 -> $JETTY_HOME
  • Jettyの設定の場所 -> $JETTY_BASE
  • $JETTY_HOME は各バージョンごとに一つだが、 $JETTY_BASE はそれぞれのバージョンに対し複数あってもOK
  • 例えばデフォルトで8080でリッスンするJettyの設定を6060にしたい場合は $JETTY_HOME ではなく、 $JETTY_BASE で行う
    • こうすればJettyのバージョンを変えても設定は維持される
  • $JETTY_HOME の場所をシステム管理者とすることでファイルパーミッションの効力(機能?)を許可できる
    • この時 $JETTY_BASE は一般ユーザとできる
    • ただ、強い権限を持つユーザでサーバーを起動するのはセキュリティ上あまりよろしいことではないため、システム管理者は割り当てないほうがいいだろう

Start Mechanism

  • Jettyをスタートするメカニズムは2つの特徴がある
    • 必要なモジュールを有効にする $JETTY_BASE を設定し、構成を表示する
    • $JETTY_BASE を読み取るJVMをスタートしJettyを起動する(?)
  • $JETTY_HOME/start.jar$JETTY_BASE から起動させることでJettyがスタートする
> mkdir jetty-base
> cd jetty-base/
> java -jar $JETTY_HOME/start.jar --help
Usage:

$ java -jar $JETTY_HOME/start.jar [command] [options...]

Commands can be of two types: report commands or configuration commands.
Commands execute and then exit the JVM.
Options can be specified with or without commands.
When no command is specified, Jetty is started with the given options.
〜以下略〜
  • モジュールをリスト表示することができる
> java -jar $JETTY_HOME/start.jar --list-modules=demo

Modules [demo]:
===============

demo modules:
-------------
               demo - A meta module to enable all demo modules.
    demo-async-rest - Demo Async Rest webapp
          demo-jaas - Demo Spec webapp
         demo-jetty - Demo Jetty Webapp
          demo-jndi - Demo JNDI Resources Webapp
           demo-jsp - Demo Simple JSP Webapp
demo-mock-resources - Download and install some Demo Mock Resources
 demo-moved-context - Demonstrate a Moved Context Handler.
         demo-proxy - Demo Proxy Webapp
         demo-realm - Configure a demo authentication realm.
       demo-rewrite - Demonstrate the rewrite module.
          demo-root - Demo root webapp.
        demo-simple - Demo Simple Webapp
          demo-spec - Download and deploy the Test Spec webapp demo.
      test-keystore - Test keystore with self-signed SSL Certificate.
  • httpモジュールを追加してみる
> java -jar $JETTY_HOME/start.jar --list-modules=http

Modules [http]:
===============

connector modules:
------------------
           http - Enables a clear-text HTTP connector.
          http2 - Enables the support for the secure HTTP/2 protocol.
         http2c - Enables the support for the clear-text HTTP/2 protocol.
          https - Adds HTTPS protocol support to the TLS(SSL) Connector.
unixsocket-http - Adds an HTTP protocol support to the Unix Domain Socket connector.
> java -jar $JETTY_HOME/start.jar --add-module=http
INFO  : mkdir ${jetty.base}/start.d
INFO  : server          transitively enabled, ini template available with --add-module=server
INFO  : logging-jetty   transitively enabled
INFO  : http            initialized in ${jetty.base}/start.d/http.ini
INFO  : resources       transitively enabled
INFO  : threadpool      transitively enabled, ini template available with --add-module=threadpool
INFO  : logging/slf4j   dynamic dependency of logging-jetty
INFO  : bytebufferpool  transitively enabled, ini template available with --add-module=bytebufferpool
INFO  : mkdir ${jetty.base}/resources
INFO  : copy ${jetty.home}/modules/logging/jetty/resources/jetty-logging.properties to ${jetty.base}/resources/jetty-logging.properties
INFO  : Base directory was modified
> tree .
.
├── resources
│   └── jetty-logging.properties
└── start.d
    └── http.ini

2 directories, 2 files
  • デモウェブアプリをモジュールとして設定できるようなのでやってみる
> java -jar $JETTY_HOME/start.jar --add-module=demo-simple
INFO  : webapp          transitively enabled, ini template available with --add-module=webapp
INFO  : security        transitively enabled
INFO  : servlet         transitively enabled
INFO  : demo-simple     initialized in ${jetty.base}/start.d/demo-simple.ini
INFO  : deploy          transitively enabled, ini template available with --add-module=deploy
INFO  : mkdir ${jetty.base}/webapps
INFO  : download https://repo1.maven.org/maven2/org/eclipse/jetty/demos/demo-simple-webapp/11.0.6/demo-simple-webapp-11.0.6.war to ${jetty.base}/webapps/demo-simple.war
INFO  : Base directory was modified
  • 起動
> java -jar $JETTY_HOME/start.jar
  • 動いているらしい
> curl -v http://localhost:8080/demo-simple/
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /demo-simple/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Last-Modified: Wed, 12 May 2021 06:27:22 GMT
< Content-Type: text/html
< Accept-Ranges: bytes
< Content-Length: 76
< Server: Jetty(11.0.6)
< 
<!DOCTYPE html>
<html>
  <body>
    <h2>Hello World!</h2>
  </body>
</html>
* Connection #0 to host localhost left intact
  • 起動後の $JETTY_BASE はこうなっていた
  • webappsにwarを配置すれば読み込んでくれそう
$ tree .
.
├── resources
│   └── jetty-logging.properties
├── start.d
│   ├── demo-simple.ini
│   └── http.ini
└── webapps
    └── demo-simple.war

3 directories, 4 files
  • 今日はここまで