LoginSignup
0
0

More than 1 year has passed since last update.

PulsarとWebSocketを使って緊急地震速報配信サーバを構築

Posted at

はじめに

あくまで今回構築したシステムは趣味&学術研究のためであり、商用利用などは想定していないのでご承知おき下さい。

今回作ったシステムを使うには

https://github.com/mizofumi/Namazu
上記のページにてAPI等を公開しているのでお試し下さい。

防災科研のデータについて

http://www.kmoni.bosai.go.jp/webservice/hypo/eew/yyyyMMddHHmmss.json

ここにリクエストをすると、緊急地震速報に関する電文が取得出来ます。

問題点

上記のURLを直接呼び出すとなるとクライアントの台数に比例して防災科研側のサーバに負荷を掛けてしまいます。

対応

防災科研のデータを叩く代表サーバを用意し、WebSocket等で配信する方法を取れば負荷をなるべく掛けずに利用する事ができます。

構築したシステム

namazu-detail.png

システムの特徴

  • Pulsar
    • もともとYahoo.Incで開発されたメッセージングPFで現在はApache Software Foundationに移管され管理されているPFです。
    • 特徴
      • 順番が保証されている
      • 1対多数の配信に向いている
      • スケーラブルである
      • 簡単(個人的感想)

防災科研のデータを取りに行き、そのままメッセージング配信しています。 そうする事でWebSocketサーバの負荷が高まりスケールアップが必要となった場合でもPulsarとの繋ぎ込みに関しては設定など特に必要はなくボタン一つで対応可能になっています。

配信側
val client = PulsarClient.builder()
    .serviceUrl("pulsar://pulsar.hoge.net:6650")
    .build()
var stringProducer = client.newProducer<String>(Schema.STRING)
    .topic("eew")
    .create()
while (true) {
    try {
        stringProducer.send("配信データ")
    } catch (e: InterruptedException) {
        e.printStackTrace()
    }
}
受信側
val client = PulsarClient.builder()
    .serviceUrl("pulsar://pulsar.hoge.net:6650")
    .build()
val consumer: Consumer<*> = client.newConsumer()
    .topic("eew")
    .subscriptionName(UUID.randomUUID().toString())
    .subscribe()
while (true) {
    val message = consumer.receive()
    println(message)
}

コード見てもお分かり頂けるかと思います。特に配信側はPulsarへ送信するコードのみで、WebSocketのサーバを指定せずとも送信出来ています。

0
0
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
0
0