Spring Boot で書籍の貸出状況確認・貸出申請する Web アプリケーションを作る ( その14 )( Spring Session を使用する3 )
概要
Spring Boot で書籍の貸出状況確認・貸出申請する Web アプリケーションを作る ( その13 )( Spring Session を使用する2 ) の続きです。
参照したサイト・書籍
目次
手順
Tomcat, Redis を複数起動して試してみる
Web アプリケーションを Redis Sentinel 対応に設定変更する
Spring Data Redis で Redis Sentinel を利用する場合、現在 master となっている Redis の IPアドレス、ポート番号は Redis Sentinel の管理プロセスから取得します。
- src/main/resources の下の application-develop.properties, application-unittest.properties, application-product.properties を リンク先の内容 に変更します。
Tomcat を複数起動して動作確認する
jar ファイルを作成した後、ポート番号を変えた Tomcat を複数起動して動作確認します。
Gradle projects View から build タスクを実行して ksbysample-webapp-lending-1.0.0-SNAPSHOT.jar を作成します。jar ファイルは C:\project-springboot\ksbysample-webapp-lending\build\libs の下に生成されます。
コマンドラインからポート番号を変更した Tomcat を3台起動します。
Redis のデータを全て削除します。現在は上の 6379番ポートの Redis が master です。
まずはログイン、ログアウトをした時にどの Tomcat にアクセスしても認証情報が共有されて同じ画面が表示されることを確認してみます。
ブラウザで http://localhost:8080/ にアクセスし、ログイン画面を表示します。ID に "tanaka.taro@sample.com"、Password に "taro" を入力し、「次回から自動的にログインする 」はチェックせず「ログイン」ボタンをクリックします。認証に成功し「ログイン成功!」の画面が表示されます。
Redis を見ると master, slave どちらにもセッションデータが保存されています。
ブラウザで http://localhost:8081/loginsuccess にアクセスします。ログイン画面には遷移せず、「ログイン成功!」の画面が表示されます。
http://localhost:8082/loginsuccess にアクセスしても、ログイン画面には遷移せず、「ログイン成功!」の画面が表示されます。
ブラウザで http://localhost:8082/logout にアクセスしてログアウトし、ログイン画面に戻ります。
ブラウザで http://localhost:8080/loginsuccess にアクセスすると、「ログイン成功!」の画面は表示されず、ログイン画面が表示されます。
Redis のデータの状態は以下のようになっていました。
次にログイン後に Redis をフェイルオーバー、フェイルバックしてからどの Tomcat にアクセスしても、ログインした状態が維持されることを確認してみます。
ブラウザで http://localhost:8080/ にアクセスし、ログイン画面を表示します。ID に "tanaka.taro@sample.com"、Password に "taro" を入力し、「次回から自動的にログインする 」はチェックせず「ログイン」ボタンをクリックします。認証に成功し「ログイン成功!」の画面が表示されます。
6379番ポートの Redis を shutdown し、6380番ポートの Redis を master にします。
ブラウザで http://localhost:8080/loginsuccess にアクセスします。ログイン画面には遷移せず、「ログイン成功!」の画面が表示されました。
http://localhost:8081/loginsuccess, http://localhost:8082/loginsuccess にアクセスしても、ログイン画面には遷移せず、「ログイン成功!」の画面が表示されることが確認できます。
Web アプリケーションのログを見ると 6380番ポートの Redis に切り替わっていることが確認できます。
セッション ID を変更するために、http://localhost:8081/logout にアクセスしてログイン画面に戻った後、ID に "tanaka.taro@sample.com"、Password に "taro" を入力し、「次回から自動的にログインする 」はチェックせず「ログイン」ボタンをクリックしてログインし直します。認証に成功し「ログイン成功!」の画面が表示されます。
Redis のデータは以下の状態です。
「サービス」画面から 6379番ポートの Redis を起動します。
起動した後は 6379番ポートの Redis が slave、6380番ポートの Redis が master です。
6380番ポートの Redis で変更したデータが 6379番ポートの Redis へ反映されていることが確認できます。
6380番ポートの Redis を shutdown し、6379番ポートの Redis を master にします。
Web アプリケーションのログを見ると 6379番ポートの Redis に切り替わっていることが確認できます。
http://localhost:8080/loginsuccess, http://localhost:8081/loginsuccess, http://localhost:8082/loginsuccess にアクセスすると、ログイン画面には遷移せず、「ログイン成功!」の画面が表示されることが確認できます。
Redis を元の状態に戻します。C:\Redis\2.8.21 の下の redis2.windows-service.conf の最後に
slaveof 127.0.0.1 6379
を追加します。※今回は redis.windows-service.conf の最後に
slaveof 127.0.0.1 6380
の記述が出力されていなかったので上の操作しかしていませんが、出力されている場合には 6379番ポートの Redis でslaveof no one
コマンドを実行する必要があります。「サービス」画面から 6380番ポートの Redis を起動します。
6379番ポートの Redis が master、6380番ポートの Redis が slave になっていることを確認します。
以上で動作確認は終わります。起動していた Tomcat 3つを終了します。
Redis Sentinel を使ってみた感想
- 最初は Redis Sentinel のプロセスを複数起動する必要があると見かけたので結構面倒なのかなと思いましたが、やり方が分かればそんなに難しいものではありませんでした。
- 最初に master - slave 構成の Redis を構築しておいて ( この時点では Redis Sentinel のことを考慮する必要がありません )、後から Redis Sentinel の管理プロセスを起動することが出来るので、仕組みとしては簡単だと思います。
- master, slave 間の切り替えもすぐに切り替わりました ( ローカル PC で、かつ sentinel down-after-milliseconds の設定を小さくしているからだとは思いますが )。
- Spring Data Redis + Redis Sentinel の構成も application.properties に記述しておけばよいだけなので楽ですね。これで自動的に切り替わるようになるとは驚きです。
- Redis Sentinel を使う上で気になった点をメモしておきます。
- Redis が設定ファイルを書き換えてしまう点は要注意です。master → slave に切り替わった後で slave → master へ戻す際には、設定ファイルを編集してきちんと最初の master - slave 構成になるよう設定しないと、元に戻りません。
- Redis を2台とも停止してから起動し直した場合には Redis Sentinel の管理プロセスも Redis 起動後に起動し直す必要があります。そうしないと自動フェイルオーバーをしてくれなくなります。
commit、Push、Pull Request、マージ](#2)
commit します。「Code Analysis」ダイアログが出ますが、無視して「Commit」ボタンをクリックします。
コマンドラインから以下のコマンドを実行して commit を1つにまとめます。
> git rebase -i HEAD~3
> git commit --amend -m "#13 Spring Session + Redis Sentinel を使用する設定に変更しました。"GitHub へ Push、1.0.x-use-spring-session -> 1.0.x へ Pull Request、1.0.x でマージ、1.0.x-use-spring-session ブランチを削除、をします。
ソースコード
application-develop.properties, application-unittest.properties, application-product.properties
■application-develop.properties
spring.datasource.url=jdbc:log4jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=net.sf.log4jdbc.sql.jdbcapi.DriverSpy spring.mail.host=localhost spring.mail.port=25 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383 spring.messages.cache-seconds=0 spring.thymeleaf.cache=false spring.velocity.cache=false
spring.redis.host=localhost
,spring.redis.port=6379
を削除して、spring.redis.sentinel.master=mymaster
,spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383
を追加します。
■application-unittest.properties
spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383 spring.thymeleaf.cache=true
■application-product.properties
server.tomcat.basedir=C:/webapps/ksbysample-webapp-lending spring.datasource.url=jdbc:postgresql://localhost/ksbylending spring.datasource.username=ksbylending_user spring.datasource.password=xxxxxxxx spring.datasource.driverClassName=org.postgresql.Driver spring.mail.host=localhost spring.mail.port=25 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=localhost:6381,localhost:6382,localhost:6383 spring.thymeleaf.cache=true
履歴
2015/08/01
初版発行。