3

以下は私のジェディスの設定です

@Bean
public JedisConnectionFactory getJedisConnectionFactory() {
    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setUsePool(true);
    return jedisConnectionFactory;
}

@Bean
public RedisTemplate<String, Object> getRedisTemplate() {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(getJedisConnectionFactory());
    return redisTemplate;
}

この構成は、サーバーが 1 つの場合にうまく機能します。私がやりたいことは、1 つの redis マスターと複数の redis スレーブを持つことです。redis のドキュメントによると、読み取りはスレーブから、書き込みはマスターから行う必要があります。書き込みにマスターを使用し、読み取りにスレーブを使用するように上記の構成を変更するにはどうすればよいですか?

私のマスターが 192.168.10.10 にあり、スレーブが localhost にあるとしましょう。

ありがとう!

4

4 に答える 4

4

Redis Sentinelを使用して、redis のマスター/スレーブ構成を維持する必要があります...以下を使用して、sentinel プールに接続できます-

@Bean
public RedisConnectionFactory jedisConnectionFactory() {
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster")
    .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380);
return new JedisConnectionFactory(sentinelConfig);
}

参考:Spring Sentinelのサポート

于 2015-10-18T09:51:20.387 に答える
0

マスター/スレーブ アーキテクチャでのレプリケーションは、Sentinel とは異なります。

redis リードレプリカの設定は非常に簡単です。

私のスプリングブートアプリケーションyaml.

redis:
  master:
    host: localhost
    port: 6379
  slaves:
    - host: localhost
      port: 16379
    - host: localhost
      port: 26379

上記のインスタンスの場合、スレーブ インスタンスの redis 構成を以下に示すように更新する必要があります。

################################# REPLICATION #################################

# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
#   +------------------+      +---------------+
#   |      Master      | ---> |    Replica    |
#   | (receive writes) |      |  (exact copy) |
#   +------------------+      +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
#    stop accepting writes if it appears to be not connected with at least
#    a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
#    master if the replication link is lost for a relatively small amount of
#    time. You may want to configure the replication backlog size (see the next
#    sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
#    network partition replicas automatically try to reconnect to masters
#    and resynchronize with them.
#
replicaof master 6379

ドッカーのセットアップについては、こちらを確認してください。

https://www.vinsguru.com/redis-master-slave-with-spring-boot/

于 2020-11-14T16:25:49.637 に答える