つまりなにしたの?
Redisを使ってPublish–Subscribe Pattern(出版者-購読者モデル)を実践してみた。
Publish-Subscribe Patternについて
前回の記事を参照
ensekitt.hatenablog.com
Redisで手軽にPub/Subするには
Redisサーバを準備する。コレは仲介者として仕事をする。
Redis-cliを準備する。 Redisコマンドラインインターフェイスで、
Redisサーバにコマンドを送信し、サーバから送信された応答を端末から
直接読み取れる。これは、出版者と購読者として仕事をする。
実行環境
$ cat /etc/os-release NAME="Ubuntu" VERSION="16.04.4 LTS (Xenial Xerus)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 16.04.4 LTS" VERSION_ID="16.04" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/" VERSION_CODENAME=xenial UBUNTU_CODENAME=xenial $ redis-server --version Redis server v=4.0.11 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4bc3aa215ca9cf78 $ redis-cli --version redis-cli 4.0.11
実践
ターミナルを3つ開いて、それぞれ、Redis、Redis-cli.A、Redis-cli.Bとして実行する
仲介者の準備(Redisの準備)
Redisをインストールする
$ sudo apt show redis-server Package: redis-server Version: 2:3.0.6-1 Priority: optional Section: universe/misc Source: redis Origin: Ubuntu Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com> Original-Maintainer: Chris Lamb <lamby@debian.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 918 kB Depends: adduser, redis-tools (= 2:3.0.6-1), init-system-helpers (>= 1.18~), libc6 (>= 2.14), libjemalloc1 (>= 2.1.1) Homepage: http://redis.io/ Download-Size: 343 kB APT-Sources: http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages Description: Persistent key-value database with network interface Redis is a key-value database in a similar vein to memcache but the dataset is non-volatile. Redis additionally provides native support for atomically manipulating and querying data structures such as lists and sets. . The dataset is stored entirely in memory and periodically flushed to disk.
RedisのバージョンはUbuntuのaptだとやや古い(Version: 2:3.0.6-1)ので、
リポジトリを更新してインストールする
$ sudo add-apt-repository ppa:chris-lea/redis-server $ sudo apt update $ sudo apt show redis-server Package: redis-server Version: 5:4.0.11-1chl1~xenial1 Priority: optional Section: database Source: redis Maintainer: Chris Lamb <lamby@debian.org> Installed-Size: 162 kB Depends: adduser, lsb-base (>= 3.2-14), redis-tools (= 5:4.0.11-1chl1~xenial1), init-system-helpers (>= 1.18~) Download-Size: 90.9 kB APT-Sources: http://ppa.launchpad.net/chris-lea/redis-server/ubuntu xenial/main amd64 Packages Description: Persistent key-value database with network interface Redis is a key-value database in a similar vein to memcache but the dataset is non-volatile. Redis additionally provides native support for atomically manipulating and querying data structures such as lists and sets. . The dataset is stored entirely in memory and periodically flushed to disk.
$ sudo apt install redis-server # わらわら出る $ redis-server --version Redis server v=4.0.11 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=4bc3aa215ca9cf78
できた。
Redis-serverをインストールするとredis-cliもインストールされているので
確認する
$ redis-cli --version redis-cli 4.0.11
できた。
出版者(Redis-cli.A)と購読者(Redis-cli.B)
概念的にはこんな感じ
ここでは、
my-channelというチャンネルを購読者が購読しており、
出版者がmy-channelというチャンネルに出版するシンプルな例を示す。
# Redis-cli.A $ redis-cli 127.0.0.1:6379> SUBSCRIBE my-channel Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "my-channel" 3) (integer) 1 1) "message" 2) "my-channel" 3) "Message!!!"
# Redis-cli.B $ redis-cli 127.0.0.1:6379> PUBLISH my-channel Message!!! (integer) 1 127.0.0.1:6379>
便利な購読条件
パターンマッチを利用したPub/Sub
my.*にマッチするチャンネルをサブスクライブして、
my.channel1とmy.channel2からパブリッシュしてみた。
出版側
$ redis-cli 127.0.0.1:6379> PUBLISH my.channel1 hello (integer) 1 127.0.0.1:6379> PUBLISH my.channel2 world (integer) 1 127.0.0.1:6379>
購読側
127.0.0.1:6379> PSUBSCRIBE my.* Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "my.*" 3) (integer) 1 1) "pmessage" 2) "my.*" 3) "my.channel1" 4) "hello" 1) "pmessage" 2) "my.*" 3) "my.channel2" 4) "world"
できた様子。
参考文献
*1:諸事情で時空が歪んでいます