Spring-data-redis哨兵配置例
7761 ワード
Spring-data-redis哨兵配置例
スプリングが持参した哨兵は確かに高可用性の配置を簡素化し、使用も簡単である.
まずspring-redis-sentinelです.xml(ファイル名は任意に命名できます)プロファイル:
上のp:password=「pwdisadmin」はredisのパスワードを指し、mymasterはredis哨兵に配置された名前を指す.
次に、redisTemplateのインスタンスを注釈またはgetBeanで取得できます.
配置では哨兵の配置を指定するだけでいいです.この中で哨兵を指定すると、哨兵は自動的にredisの主のredis serverを発行します.
設定後、起動項目ログは以下のように印刷されます(例:
sentinel.conf
================================
port 26379
dir "/letv/redis-sentinel/tmp"
//monitor master 10.120.16.45 6379
sentinel monitor shoppingcar1 10.120.16.45 6379 1
sentinel down-after-milliseconds shoppingcar1 3000
sentinel failover-timeout shoppingcar1 5000
sentinel auth-pass mycart1 passwordabc
sentinel config-epoch mycart1 1554
sentinel leader-epoch mycart1 1975
//monitor slave 10.120.16.61 16379
sentinel known-slave mycart1 10.120.16.61 16379
//other sentinels
sentinel known-sentinel mycart1 10.120.16.161 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.61 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.184 26379 12345abc
sentinel current-epoch 1975
スプリングが持参した哨兵は確かに高可用性の配置を簡素化し、使用も簡単である.
まずspring-redis-sentinelです.xml(ファイル名は任意に命名できます)プロファイル:
上のp:password=「pwdisadmin」はredisのパスワードを指し、mymasterはredis哨兵に配置された名前を指す.
次に、redisTemplateのインスタンスを注釈またはgetBeanで取得できます.
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:conf/spring-redis-sentinel.xml");
RedisTemplate template = (RedisTemplate) context.getBean("redisTemplate");
template.opsForValue().set("aaa", "aaabbb");
System.err.println(template.opsForValue().get("aaa"));
}
配置では哨兵の配置を指定するだけでいいです.この中で哨兵を指定すると、哨兵は自動的にredisの主のredis serverを発行します.
設定後、起動項目ログは以下のように印刷されます(例:
19, 2017 4:33:35 redis.clients.jedis.JedisSentinelPool initSentinels
: Trying to find master from available Sentinels...
19, 2017 4:33:35 redis.clients.jedis.JedisSentinelPool initSentinels
: Redis master running at 10.10.39.104:16379, starting Sentinel listeners...
19, 2017 4:33:35 redis.clients.jedis.JedisSentinelPool initPool
: Created JedisPool to master at 10.10.39.104:16379
sentinel
sentinel.conf
================================
port 26379
dir "/letv/redis-sentinel/tmp"
//monitor master 10.120.16.45 6379
sentinel monitor shoppingcar1 10.120.16.45 6379 1
sentinel down-after-milliseconds shoppingcar1 3000
sentinel failover-timeout shoppingcar1 5000
sentinel auth-pass mycart1 passwordabc
sentinel config-epoch mycart1 1554
sentinel leader-epoch mycart1 1975
//monitor slave 10.120.16.61 16379
sentinel known-slave mycart1 10.120.16.61 16379
//other sentinels
sentinel known-sentinel mycart1 10.120.16.161 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.61 26379 12345abc
sentinel known-sentinel mycart1 10.120.16.184 26379 12345abc
sentinel current-epoch 1975