In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use docker to build redis master-slave sentinel mode and integrate it into springboot project". In daily operation, I believe many people have doubts about how to use docker to build redis master-slave sentry mode and integrate it into springboot project. The editor consulted all kinds of materials and sorted out simple and useful operation methods, hoping to answer "how to use docker to build redis master-slave sentinel mode." And integrate into the springboot project "doubts help!" Next, please follow the editor to study!
In fact, the idea of the previous article was based on Baidu's reference to various blog articles, but when it was found that there was still a problem in the end, I went directly to the official website to read the documentation, so I built it again on the basis of the previous familiarity. in the case of container network, realize the function of automatic switching between master and standby.
1. First, go to redis master / slave choreography file, master-slave/docker-compose.yml.
Version: "3" services: master: image: redis:latest container_name: redis-master command: redis-server / usr/local/etc/redis/redis.conf ports:-"6379" volumes: "/ root/redis/redis-master.conf:/usr/local/etc/redis/redis.conf" networks:-sentinel-master slave1: image: redis:latest container_name: redis-slave-1 command : redis-server / usr/local/etc/redis/redis.conf depends_on:-master ports:-"6380 volumes: -" / root/redis/redis-slave-1.conf:/usr/local/etc/redis/redis.conf "networks:-sentinel-master slave2: image: redis:latest container_name: redis-slave-2 command: redis-server / usr/local/etc/redis/redis.conf Depends_on:-master ports:-"6381 volumes: -" / root/redis/redis-slave-2.conf:/usr/local/etc/redis/redis.conf "networks:-sentinel-masternetworks: sentinel-master:
The configuration file is still used here, and two additional configuration items are added to announce the ip and port to the Sentinel to prevent the Sentinel from fetching the private ip of the container.
Redis-master.conf
Port 6379requirepass 12345 prevents the ip announced to the sentry by authentication failure masterauth 12345 when the slave node joins the cluster after restart. If it is not configured, the sentry will fetch the internal ip of the container so that the client cannot access the port slave-announce-port 6379 announced to the sentry by slave-announce-ip 192.168.1.25
Redis-slave-1.conf
Port 6379requirepass 123456slaveof 192.168.1.254 6379masterauth 123456slave-announce-ip 192.168.1.254slave-announce-port 6380
Redis-slave-2.conf
Port 6379requirepass 123456slaveof 192.168.1.254 6379masterauth 123456slave-announce-ip 192.168.1.254slave-announce-port 6381
2, Sentinel scheduling file, sentinel/docker-compose.yml
# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.confversion: "3" services: sentinel1: image: redis:latest container_name: redis-sentinel-1 command: redis-sentinel / usr/local/etc/redis/sentinel.conf ports:-"26379 services: -" / root/redis/sentinel1.conf:/usr/local/etc/redis/sentinel.conf "sentinel2: image : redis:latest container_name: redis-sentinel-2 command: redis-sentinel / usr/local/etc/redis/sentinel.conf ports:-"26380 volumes: -" / root/redis/sentinel2.conf:/usr/local/etc/redis/sentinel.conf "sentinel3: image: redis:latest container_name: redis-sentinel-3 command: redis-sentinel / usr/local/etc/redis/sentinel.conf ports:- "26381 volumes: -" / root/redis/sentinel3.conf:/usr/local/etc/redis/sentinel.conf "networks: default: external: name:" master-slave_sentinel-master "
The initial content of sentinel1.conf,sentinel2.conf and sentinel3.conf is exactly the same, except that the rewrite will be different when the Sentinel starts. This is done automatically by the Sentinel.
Port 26379dir / tmpsentinel monitor mymaster 192.168.1.254 6379 2sentinel auth-pass mymaster 123456sentinel down-after-milliseconds mymaster 30000sentinel parallel-syncs mymaster 1sentinel failover-timeout mymaster 10000sentinel deny-scripts-reconfig yes
3Springboot integration, and the test program is still the same
Introduce dependency
Org.springframework.boot spring-boot-starter-data-redis
Application.yml
Spring: redis: sentinel: master: mymaster nodes:-"192.168.1.254 master 26379"-"192.168.1.254 max-wait 26380"-"192.168.1.254 max-wait 26381" host: 192.168.1.254 password: 123456 jedis: pool: min-idle: 8 max-active: 100 max-wait: 3000 max-idle: 100
Test program, 3 threads simulate and insert data concurrently, a total of 3000
@ Testpublic void testRedisMasterSlave () throws Exception {ValueOperations valueOperations = redisTemplate.opsForValue (); ExecutorService es = Executors.newFixedThreadPool (3); for (int j = 0; j
< 3; j++) { es.submit(() ->{for (int I = 0; I)
< 1000; i++) { try { String threadName = Thread.currentThread().getName(); valueOperations.set(threadName + i, i + "", 30L, TimeUnit.MINUTES); TimeUnit.MILLISECONDS.sleep(200L); } catch (InterruptedException e) { System.out.println("error: " + e.getMessage()); } } }); } es.shutdown(); es.awaitTermination(30L, TimeUnit.MINUTES);} 准备完成,分别启动redis( master-slave/docker-compose.yml),哨兵(sentinel/docker-compose.yml) 执行junit测试程序,并在中途停掉redis-master节点 2019-10-06 21:50:24.199 INFO 128256 --- [pool-1-thread-1] io.lettuce.core.EpollProvider : Starting without optional epoll library2019-10-06 21:50:24.201 INFO 128256 --- [pool-1-thread-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library2019-10-06 21:51:11.141 INFO 128256 --- [xecutorLoop-1-6] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was /192.168.1.254:63792019-10-06 21:51:13.161 WARN 128256 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:63792019-10-06 21:51:17.440 INFO 128256 --- [xecutorLoop-1-7] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:63792019-10-06 21:51:19.450 WARN 128256 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:63792019-10-06 21:51:23.741 INFO 128256 --- [xecutorLoop-1-8] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:63792019-10-06 21:51:25.748 WARN 128256 --- [ioEventLoop-4-8] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:63792019-10-06 21:51:30.840 INFO 128256 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:63792019-10-06 21:51:32.848 WARN 128256 --- [ioEventLoop-4-6] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:63792019-10-06 21:51:38.041 INFO 128256 --- [xecutorLoop-1-7] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:63792019-10-06 21:51:40.050 WARN 128256 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:63792019-10-06 21:51:44.241 INFO 128256 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:63792019-10-06 21:51:44.248 INFO 128256 --- [ioEventLoop-4-4] i.l.core.protocol.ReconnectionHandler : Reconnected to 192.168.1.254:6380 控制台打印的消息由6379端口,切换到了6380端口,证明哨兵已经完成主备切换了。 接下来再把redis-master节点重新启动,查看容器启动日志 2019-10-05T19:05:51.594625998Z 1:S 05 Oct 2019 19:05:51.594 * Before turning into a replica, using my master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.2019-10-05T19:05:51.594679191Z 1:S 05 Oct 2019 19:05:51.594 * REPLICAOF 192.168.1.254:6380 enabled (user request from 'id=4 addr=172.23.0.1:43460 fd=9 name=sentinel-36a0e6b8-cmd age=10 idle=0 flags=x db=0 sub=0 psub=0 multi=3 qbuf=153 qbuf-free=32615 obl=36 oll=0 omem=0 events=r cmd=exec')2019-10-05T19:05:51.594683850Z 1:S 05 Oct 2019 19:05:51.594 # CONFIG REWRITE failed: Permission denied2019-10-05T19:05:51.961959923Z 1:S 05 Oct 2019 19:05:51.961 * Connecting to MASTER 192.168.1.254:63802019-10-05T19:05:51.961989715Z 1:S 05 Oct 2019 19:05:51.961 * MASTER REPLICA sync started2019-10-05T19:05:51.961995348Z 1:S 05 Oct 2019 19:05:51.961 * Non blocking connect for SYNC fired the event.2019-10-05T19:05:51.962964412Z 1:S 05 Oct 2019 19:05:51.962 * Master replied to PING, replication can continue...2019-10-05T19:05:51.963959151Z 1:S 05 Oct 2019 19:05:51.963 * Trying a partial resynchronization (request d0c6a5694d17b9337656d0ef009aa580e0743431:1).2019-10-05T19:05:51.967159605Z 1:S 05 Oct 2019 19:05:51.966 * Full resync from master: bfcd3bb8bbfb2393ded951443e9e2100ed490548:1339112019-10-05T19:05:51.967200123Z 1:S 05 Oct 2019 19:05:51.966 * Discarding previously cached master state.2019-10-05T19:05:52.064097244Z 1:S 05 Oct 2019 19:05:52.062 * MASTER REPLICA sync: receiving 51791 bytes from master2019-10-05T19:05:52.064114578Z 1:S 05 Oct 2019 19:05:52.062 * MASTER REPLICA sync: Flushing old data2019-10-05T19:05:52.064124761Z 1:S 05 Oct 2019 19:05:52.062 * MASTER REPLICA sync: Loading DB in memory2019-10-05T19:05:52.064128120Z 1:S 05 Oct 2019 19:05:52.063 * MASTER REPLICA sync: Finished with success 发现节点已经主动找到当前master节点192.168.1.254:6380,并进行全量复制了。 再进入哨兵节点查看slave节点信息 127.0.0.1:26379>Sentinel slaves mymaster1) 1) "name" 2) "192.168.1.254" 5) "port" 6) "6379" 7) "runid" 8) "d4d7ce3f1cc4d5c6cca2345318e4dcfebe12fcce" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link" -refcount "14)" 1 "15)" last-ping-sent "16)" 0 "17)" last-ok-ping-reply "18)" last-ping-reply "20)" 872 "21)" down-after-milliseconds "22)" 30000 "23)" info-refresh "24)" 2519 "25)" role-reported "26)" slave "27) "role-reported-time" 28) "5867007" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "192.168.1.254" 35) "master-port" 36) "6380" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "1526467" 2) 1) "name" 2) "192.168.1.254" 3) "ip" 4) "192.168.1.254" 5) "port" 6) "6381" 7) "runid" 8) "2fecd472915d77b55d230c339f5982491ee55d69" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "last-ping-reply" 20) "872" 21) "down-after-milliseconds" 22) "30000" 23) "info-refresh" 24) "6520" 25) "role-reported" 26) "slave" 27 ) "role-reported-time" 28) "5940478" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "192.168.1.254" 35) "master-port" 36) "6380" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "1525639"
It is found that 6379 nodes have also been added to the slave node, so the highly available solution in Sentinel mode is complete.
At this point, the study on "how to use docker to build redis master-slave sentry mode and integrate it into the springboot project" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.