LoginSignup
3
3

More than 3 years have passed since last update.

マイクロサービスフレームワーク Lagom の新規アプリケーションを作成する

Last updated at Posted at 2019-02-02

マイクロサービスフレームワークの Lagom を使用した新規アプリケーションを作成する方法をメモっとく。

前提条件

  • Java 8
  • sbt 1.2.1+

新規アプリケーションを作成する

Giter8 テンプレートから Lagom を使用した新規アプリケーションを作成できる。ここでは、テンプレートパラメーターをコマンドライン引数として指定しているが、指定しない場合は対話的にテンプレートパラメーターを指定できる。

$ sbt new lagom/lagom-scala.g8 \
  --name=Hello \
  --organization=com.example \
  --version=1.0-SNAPSHOT \
  --package=com.example.hello

アプリケーションを起動する

作成したアプリケーションのルートディレクトリで sbt を起動する。依存関係の処理などが終了して、対話型シェルが立ち上がったら、runAll を実行する。例として定義されているサービスと一緒に関連するミドルウェアも立ち上がる。緑の字で (Services started, ...) と表示されたらアプリケーションの起動完了だ。

$ cd hello
$ sbt
sbt:hello> runAll
...
[info] (Services started, press enter to stop and go back to the console...)

アプリケーションを使用する

/api/hello/:id エンドポイントに GET リクエストすると挨拶 Hello, {:id}! が返ってくる。

$ curl -i -X GET 127.0.0.1:9000/api/hello/Alice
HTTP/1.1 200 OK
Date: Sat, 02 Feb 2019 07:30:43 GMT
Server: akka-http/10.0.15
Content-Type: text/plain
Content-Length: 13

Hello, Alice!

/api/hello/:id エンドポイントに JSON 付きで POST リクエストすると挨拶の Hello を指定したものに変更できる。ID に Alice を指定した場合は、ID が Alice のときのみ指定した挨拶が返ってくるようになる。

$ curl -i -X POST 127.0.0.1:9000/api/hello/Alice \
       -H 'Content-Type: application/json' \
       -d '{ "message": "Hi" }'
HTTP/1.1 200 OK
Date: Sat, 02 Feb 2019 07:31:23 GMT
Server: akka-http/10.0.15
Content-Length: 0
$ curl -i -X GET 127.0.0.1:9000/api/hello/Alice
HTTP/1.1 200 OK
Date: Sat, 02 Feb 2019 07:46:00 GMT
Server: akka-http/10.0.15
Content-Type: text/plain
Content-Length: 10

Hi, Alice!
$ curl -i -X GET 127.0.0.1:9000/api/hello/Bob
HTTP/1.1 200 OK
Date: Sat, 02 Feb 2019 07:48:23 GMT
Server: akka-http/10.0.15
Content-Type: text/plain
Content-Length: 11

Hello, Bob!

/stream エンドポイントに wscat を使用して接続すると挨拶を WebSocket で実行できる。

$ wscat -c 127.0.0.1:9000/stream
connected (press CTRL+C to quit)
> Alice
< Hi, Alice!
> Bob
< Hello, Bob!
>

メッセージを確認する

/api/hello/:id エンドポイントへの POST リクエストが成功すると、ブローカー (Kafka) に向けて greetings トピックのメッセージが発行される。残念ながらサンプルコードに greetings トピックを購読しているサービスは含まれていないため、kafkacat を使用してメッセージの発行を確認してみる。

runAll で一緒に起動する Kafka サーバーに kafkacat を使用して接続する。

$ kafkacat -b 127.0.0.1:9092 -t greetings
% Auto-selecting Consumer mode (use -P or -C to override)
% Reached end of topic greetings [0] at offset 0

kafkacat とは別のターミナルで /api/hello/:id エンドポイントに POST リクエストを送信する。

$ curl -i -X POST 127.0.0.1:9000/api/hello/Alice \
       -H 'Content-Type: application/json' \
       -d '{ "message": "Hi" }'
HTTP/1.1 200 OK
Date: Thu, 18 Jul 2019 15:12:59 GMT
Server: akka-http/10.1.8
Content-Length: 0

そうすると、kafkacat が下記のようにメッセージを出力する。

$ kafkacat -b 127.0.0.1:9092 -t greetings
% Auto-selecting Consumer mode (use -P or -C to override)
% Reached end of topic greetings [0] at offset 0
{"name":"Alice","message":"Hi"}
% Reached end of topic greetings [0] at offset 1

アプリケーションを停止する

runAll を実行した sbt シェルで Enter を押下するとアプリケーションが停止する。

Cassandra のデータベースを初期化する

指定した挨拶は開発用で起動する Cassandra に永続化される。lagomCassandraCleanOnStart セッティングキーを true に設定すると起動時に Cassandra のデータベースが初期化されるようになる。

$ sbt
sbt:hello> set lagomCassandraCleanOnStart in ThisBuild := true
sbt:hello> runAll
...
[info] (Services started, press enter to stop and go back to the console...)

Kafka のログを初期化する

発行されたメッセージは開発用で起動する Kafka に永続化される。lagomKafkaCleanOnStart セッティングキーを true に設定すると起動時に Kafka のログ(メッセージ)が初期化されるようになる。

$ sbt
sbt:hello> set lagomKafkaCleanOnStart in ThisBuild := true
sbt:hello> runAll
...
[info] (Services started, press enter to stop and go back to the console...)

今回はマイクロサービスフレームワーク Lagom の新規アプリケーションを作成する方法について説明した。次回はこの新規アプリケーションに含まれるコードの詳細について説明したい。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3