Spring Boot 2.3.x の Web アプリを 2.4.x へバージョンアップする ( その9 )( RedisClusterConfig の RedisConnectionFactory Bean を削除し、application.properties で設定する方法に切り替える )
概要
記事一覧はこちらです。
Spring Boot 2.3.x の Web アプリを 2.4.x へバージョンアップする ( その8 )( Docker コンテナの image をバージョンアップする ) の続きです。
- 今回の手順で確認できるのは以下の内容です。
- これまで Redis に接続するための RedisConnectionFactory Bean は Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その30 )( Redis のクライアントライブラリを Jedis → Lettuce に変更する ) で作成したものを使用していましたが、Redis 6+lettuce-core 6 で Redis 6 の新機能を試そうと思って Spring Boot 2.4.3 の Common Application properties を見ていたところ
spring.redis.~
やspring.redis.lettuce.~
の property があることに気づきました。自分で RedisConnectionFactory Bean を定義しなくても設定ファイルで定義できそうです。 - RedisConnectionFactory bean を定義している src/main/java/ksbysample/webapp/lending/config/RedisClusterConfig.java を削除して、設定ファイルで定義する方式に切り替えてみます。
- これまで Redis に接続するための RedisConnectionFactory Bean は Spring Boot 1.5.x の Web アプリを 2.0.x へバージョンアップする ( その30 )( Redis のクライアントライブラリを Jedis → Lettuce に変更する ) で作成したものを使用していましたが、Redis 6+lettuce-core 6 で Redis 6 の新機能を試そうと思って Spring Boot 2.4.3 の Common Application properties を見ていたところ
参照したサイト・書籍
Common Application properties
https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/appendix-application-properties.htmlLettuce Reference Guide
https://lettuce.io/core/6.0.2.RELEASE/reference/lettuce-io / lettuce-core - Redis Cluster
https://github.com/lettuce-io/lettuce-core/wiki/Redis-Cluster
目次
- RedisClusterConfig.java を削除し、application.properties に Redis へ接続するための設定を追加する
- 動作確認
- Spring Boot のバージョンアップ時に Common Application properties に追加されたものを確認するには?
手順
RedisClusterConfig.java を削除し、application.properties に Redis へ接続するための設定を追加する
まず src/main/java/ksbysample/webapp/lending/config/RedisClusterConfig.java を削除します。
src/main/resources/application.properties を以下のように変更します。
spring.redis.client-name=ksbysample-webapp-lending spring.redis.cluster.nodes=${host.ip.address}:6379,${host.ip.address}:6380,${host.ip.address}:6381,${host.ip.address}:6382,${host.ip.address}:6383,${host.ip.address}:6384 spring.redis.lettuce.cluster.refresh.period=5s spring.redis.timeout=30s
- これまでは
spring.redis.cluster.nodes[0]=${host.ip.address}:6379
、spring.redis.cluster.nodes[1]=${host.ip.address}:6380
、... のようにspring.redis.cluster.nodes
に添え字を付けて設定していましたが、spring.redis.cluster.nodes を見るとカンマ区切りで設定できると書いてあったので、spring.redis.cluster.nodes=${host.ip.address}:6379,${host.ip.address}:6380,...
に変更します。 - 以下の設定を追加します。
spring.redis.client-name=ksbysample-webapp-lending
- redis-cli で
client list
コマンドを実行した時に Web アプリケーションから接続しているセッションの name にここで設定している文字列が表示されます。
- redis-cli で
spring.redis.lettuce.cluster.refresh.period=5s
- この設定は実際には 60s 程度にしておいた方がよいでしょう。今は cluster 内で redis サーバを落とした時の切り替えを短時間で行うために 5s と短い時間に設定しています。
spring.redis.timeout=30s
- あまり短い時間(例えば 5s)を設定すると Spring Session が Redis からデータを取得しようとして Command time out が発生した時に Tomcat のエラー画面が直接表示されることがあるので、ある程度長い時間を設定する必要があります。
- あまり短い時間(例えば 5s)を設定すると Spring Session が Redis からデータを取得しようとして Command time out が発生した時に Tomcat のエラー画面が直接表示されることがあるので、ある程度長い時間を設定する必要があります。
clean タスク実行 → Rebuild Project 実行 → build タスクを実行して BUILD SUCCESSFUL のメッセージが出力されることを確認します。
動作確認
IntelliJ IDEA から Web アプリを起動してログイン・ログアウトは問題なくできました。ログインしてから Web アプリを再起動した後画面を操作しても強制的にログアウトさせられることもありませんでした。
ログインしている状態で、
redis-cli で cluster nodes
コマンドを実行して master になっている redis サーバを確認した後、
6379、6380、6381 を落とします。
その後 40秒程何もしなければ画面を操作してもエラーが出ませんでした。
40秒以下で画面を操作すると、spring.redis.timeout に設定した 30s が経過した後 500 Internal Server Error のエラー画面が表示されました。
Spring Boot のバージョンアップ時に Common Application properties に追加されたものを確認するには?
spring-boot-autoconfigure-x.x.x.jar の META-INF/spring-configuration-metadata.json を見れば一覧が分かります。
今回設定した spring.redis.lettuce.cluster.refresh.period
がいつ追加されたのか確認するために 2.4.3、2.3.9、2.2.13 の spring-configuration-metadata.json を取得して比較したところ、2.3 から追加されていました。
履歴
2021/03/10
初版発行。