Redisリーダーフォロワー(マスタ/スレーブ)レプリケーションの設定 パーマリンク to "Redisリーダーフォロワー(マスタ/スレーブ)レプリケーションの設定"

このTipは@zhx828によって提出されました

最新のJHipsterジェネレータでは、プロダクションデプロイメントのためのRedisクラスタ設定を提供しています。しかし、多くの場合、小規模なプロジェクトではそれが過剰になる可能性があります。このドキュメントでは、Redisリーダーフォロワー(マスタ/スレーブ)レプリケーションを設定するためのソリューションを提供します。Redisレプリケーションの詳細については、ここを参照してください。

以下の変更は、私自身のプロジェクト設定に基づいています。Redisパスワードを設定するためにアプリケーションプロパティを変更したので、それに応じて独自のパスワードを調整してください。

ステップ1 パーマリンク to "ステップ1"

ファイルRedisProperties.javaを追加します。

public class RedisProperties {
    String type;
    String password;
    MasterSlaveRedisCache masterSlaveCache;
    SingleRedisCache singleCache;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public MasterSlaveRedisCache getMasterSlaveCache() {
        return masterSlaveCache;
    }

    public void setMasterSlaveCache(MasterSlaveRedisCache masterSlaveCache) {
        this.masterSlaveCache = masterSlaveCache;
    }

    public SingleRedisCache getSingleCache() {
        return singleCache;
    }

    public void setSingleCache(SingleRedisCache singleCache) {
        this.singleCache = singleCache;
    }
}

ステップ2 パーマリンク to "ステップ2"

RedisPropertiesをApplicationProperties.javaに追加します。

public class ApplicationProperties {
    ...

    private RedisProperties redis;
    
    public RedisProperties getRedis() {
        return redis;
    }

    public void setRedis(RedisProperties redis) {
        this.redis = redis;
    }
    ...
}

ステップ3 パーマリンク to "ステップ3"

ファイルCacheConfiguration.java、メソッドjcacheConfigurationを更新します。これらの変更は、現在のクラスタ設定と組み合わせて行う必要があります。

if (applicationProperties.getRedis().getType().equals(RedisType.SINGLE.getType())) {
    config.useSingleServer()
        .setAddress(applicationProperties.getRedis().getSingleCache().getAddress())
        .setPassword(applicationProperties.getRedis().getPassword());
} else if (applicationProperties.getRedis().getType().equals(RedisType.MASTER_SLAVE.getType())) {
    config.useMasterSlaveServers()
        .setMasterAddress(applicationProperties.getRedis().getMasterSlaveCache().getMasterAddress())
        .addSlaveAddress(applicationProperties.getRedis().getMasterSlaveCache().getSlaveAddress())
        .setPassword(applicationProperties.getRedis().getPassword());
} else {
    throw new Exception("The redis type " + applicationProperties.getRedis().getType() + " is not supported. Only single and master-slave are supported.");
}

ステップ4 パーマリンク to "ステップ4"

単一サーバーを使用するようにapplication-dev.ymlを更新します。

application:
  profile: dev
  redis:
    type: 'single'
    password: 'public-redis-password'
    single-cache:
      address: 'redis://localhost:6379'

ステップ5 パーマリンク to "ステップ5"

マスタ/スレーブサーバーを使用するようにapplication-prod.ymlを更新します。

application:
  profile: prod
  redis:
    type: 'master-slave'
    password: 'public-redis-password'
    master-slave-cache:
      master-address: 'redis://redis-master:6379'
      slave-address: 'redis://redis-slave:6379'