博多南ウェブサービスのblog

博多南ウェブサービスのサービス紹介

【Play Framework 3.0.x サンプル】Apache Pekko Receptionist(Cluster) を使って、複数サーバー間でのメッセージ送信したい

Play Framework 3.0.x (以下、Play) を始めたばかりの方向けに、サンプルを進めるうえで困ったところを共有する目的で書いています。

過去のPlay Framework 2.8.x とAkka Typed を使ったServer-Sent Events (以下、SSE) を、Apache Pekko (以下、Pekko) に変更したものです。

以下、目次

やりたいこと

Playインスタンス1 (localhost:9000) にアクセスしたクライアントが、サーバーより受け取るメッセージを、Playインスタンス2(localhost:9001) にアクセスしているクライアントも受け取ることができる

見た目

Play Pekko Server Sent Events Sample

設計のポイント

Receptionist は、ActorRef を登録、検索できる仕組みで、cluster(node が複数ある場合)もサポートしている。サポートというのは、コードは変更せずに設定だけで、local、clusterを切り替えられる。

今回の(も)、サーバー間のメッセージ送信を実現するため、Receptionist を利用している。

Server1
Sender
Sen...
Receptionist
Receptionist
伝搬
伝搬
Actor1(SourceRef)
Actor1(SourceRef)
Server2
Receptionist
Receptionist
Actor1(SourceRef)
Actor1(SourceRef)
Actor2(SourceRef)
Actor2(SourceRef)
Actor1
Act...
Actor2
Act...
Source1
Source1
Source2
Source2
Tell
Tell
Tell
Tell
Client1
Client1
Client2
Client2
Tell
Tell
Actor2(SourceRef)
Actor2(SourceRef)
Find and Tell
Find and Tell
Text is not SVG - cannot display

実装してみて

  • 前回同様、Receptionist に、SSE 用のSource[String, ActorRef[String]] にメッセージを送るためのActorRef[String] を直接登録すると、Cluster にしたときにうまくいかない
  • なので、ActorRef[String] にメッセージを送る(リレーする)だけのActor を作って、Receptionist に登録すると、うまくいった

Githubこちら

以上でした。