您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用docker搭建redis主從哨兵模式,并整合進(jìn)springboot項(xiàng)目”,在日常操作中,相信很多人在怎么用docker搭建redis主從哨兵模式,并整合進(jìn)springboot項(xiàng)目問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用docker搭建redis主從哨兵模式,并整合進(jìn)springboot項(xiàng)目”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
一,在主機(jī)上搭建redis
整體redis和哨兵架構(gòu)均為3節(jié)點(diǎn)(1主2從)
默認(rèn)容器啟動(dòng)為bridge模式,這樣哨兵取到的主節(jié)點(diǎn)ip為容器內(nèi)網(wǎng)ip,客戶端無法訪問容器內(nèi)網(wǎng),故需要使用宿主機(jī)網(wǎng)絡(luò)模式編排(network_mode:“host”)
宿主機(jī)外網(wǎng)IP,192.168.1.254
1,(redis編排文件)master-slave/docker-compose.yml
version: "3" services: master: image: redis:latest container_name: redis-master command: redis-server --requirepass 123456 --port 6379 ports: - "6379" network_mode: "host" slave1: image: redis:latest container_name: redis-slave-1 command: redis-server --port 6380 --slaveof 192.168.1.254 6379 --requirepass 123456 --masterauth 123456 depends_on: - master ports: - "6380" network_mode: "host" slave2: image: redis:latest container_name: redis-slave-2 command: redis-server --port 6381 --slaveof 192.168.1.254 6379 --requirepass 123456 --masterauth 123456 depends_on: - master ports: - "6381" network_mode: "host"
在當(dāng)前目錄下使用命令啟動(dòng):docker-compose up -d
共三臺(tái)redis,其中master(6379),slave(6380,6381)
2,(哨兵編排文件)sentinel/docker-compose.yml
# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf version: "3" services: sentinel1: image: redis:latest container_name: redis-sentinel-1 command: redis-sentinel /usr/local/etc/redis/sentinel.conf ports: - "26379" volumes: - "/root/redis/sentinel1.conf:/usr/local/etc/redis/sentinel.conf" network_mode: "host" 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" network_mode: "host" 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" network_mode: "host"
在當(dāng)前目錄下使用命令啟動(dòng):docker-compose up -d
共三臺(tái)哨兵,端口分別為26379, 26380, 26381
其中三個(gè)哨兵配置文件內(nèi)容除了端口不同,其他內(nèi)容均相同
/root/redis/sentinel1.conf
port 26379 # 其他兩節(jié)點(diǎn)分別為26380, 26381 dir /tmp sentinel monitor mymaster 192.168.1.254 6379 2 sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 10000 sentinel deny-scripts-reconfig yes
#第三行表示Redis監(jiān)控一個(gè)叫做mymaster的運(yùn)行在192.168.1.254:6379的master,投票達(dá)到2則表示master以及掛掉了。
#第四行設(shè)置主節(jié)點(diǎn)的認(rèn)證密碼
#第五行表示在一段時(shí)間范圍內(nèi)sentinel向master發(fā)送的心跳PING沒有回復(fù)則認(rèn)為master不可用了。
#第六行的parallel-syncs表示設(shè)置在故障轉(zhuǎn)移之后,同時(shí)可以重新配置使用新master的slave的數(shù)量。數(shù)字越低,更多的時(shí)間將會(huì)用故障轉(zhuǎn)移完成,但是如果slaves配置為服務(wù)舊數(shù)據(jù),你可能不希望所有的slave同時(shí)重新同步master。因?yàn)橹鲝膹?fù)制對于slave是非阻塞的,當(dāng)停止從master加載批量數(shù)據(jù)時(shí)有一個(gè)片刻延遲。通過設(shè)置選項(xiàng)為1,確信每次只有一個(gè)slave是不可到達(dá)的。
#第七行表示10秒內(nèi)mymaster還沒活過來,則認(rèn)為master宕機(jī)了。
這是啟動(dòng)后的容器列表:
二,整合springboot項(xiàng)目
1,引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2,配置application.yml
spring: redis: sentinel: master: mymaster nodes: - "192.168.1.254:26379" - "192.168.1.254:26380" - "192.168.1.254:26381" host: 192.168.1.254 password: 123456 jedis: pool: min-idle: 8 max-active: 100 max-wait: 3000 max-idle: 100
3,使用RedisTemplate操作redis
@Autowired RedisTemplate<String, String> redisTemplate; @Test public void testRedisMasterSlave() throws Exception { ValueOperations<String, String> 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); }
這里使用3個(gè)線程模擬并發(fā)寫入redis,在寫的過程中,停掉master,查看控制臺(tái)輸出日志
2019-10-06 18:52:51.949 INFO 128972 --- [pool-1-thread-1] io.lettuce.core.EpollProvider : Starting without optional epoll library 2019-10-06 18:52:51.951 INFO 128972 --- [pool-1-thread-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library 2019-10-06 18:53:55.979 INFO 128972 --- [xecutorLoop-1-8] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was /192.168.1.254:6379 2019-10-06 18:53:57.992 WARN 128972 --- [ioEventLoop-4-4] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:6379 2019-10-06 18:54:02.277 INFO 128972 --- [xecutorLoop-1-7] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:6379 2019-10-06 18:54:04.286 WARN 128972 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:6379 2019-10-06 18:54:08.577 INFO 128972 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:6379 2019-10-06 18:54:10.587 WARN 128972 --- [ioEventLoop-4-8] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:6379 2019-10-06 18:54:15.678 INFO 128972 --- [xecutorLoop-1-3] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:6379 2019-10-06 18:54:17.687 WARN 128972 --- [ioEventLoop-4-6] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:6379 2019-10-06 18:54:22.877 INFO 128972 --- [xecutorLoop-1-7] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:6379 2019-10-06 18:54:24.885 WARN 128972 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog : Cannot reconnect: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: /192.168.1.254:6379 2019-10-06 18:54:29.078 INFO 128972 --- [xecutorLoop-1-1] i.l.core.protocol.ConnectionWatchdog : Reconnecting, last destination was 192.168.1.254:6379 2019-10-06 18:54:29.085 INFO 128972 --- [ioEventLoop-4-4] i.l.core.protocol.ReconnectionHandler : Reconnected to 192.168.1.254:6380
可以看到在18:53:55.979,已經(jīng)連不上主節(jié)點(diǎn)了(6379),之后一直在重連,直到18:54:29.085才連上,但是這時(shí)候的節(jié)點(diǎn)已經(jīng)變了(6380),證明哨兵已經(jīng)可以自動(dòng)切換主節(jié)點(diǎn)了。
再看redis中的數(shù)據(jù),一共3000條數(shù)據(jù),一條沒丟,證明即使在切換主備的過程中,數(shù)據(jù)也不會(huì)丟
接著啟動(dòng)剛停掉的redis-master節(jié)點(diǎn),發(fā)現(xiàn)已經(jīng)啟動(dòng)不了了,報(bào)錯(cuò)
2019-10-05T16:00:57.899358510Z 1:S 05 Oct 2019 16:00:57.897 * Connecting to MASTER 192.168.1.254:6380 2019-10-05T16:00:57.899381586Z 1:S 05 Oct 2019 16:00:57.897 * MASTER <-> REPLICA sync started 2019-10-05T16:00:57.899385377Z 1:S 05 Oct 2019 16:00:57.897 * Non blocking connect for SYNC fired the event. 2019-10-05T16:00:57.899388194Z 1:S 05 Oct 2019 16:00:57.898 * Master replied to PING, replication can continue... 2019-10-05T16:00:57.899391119Z 1:S 05 Oct 2019 16:00:57.898 * (Non critical) Master does not understand REPLCONF listening-port: -NOAUTH Authentication required. 2019-10-05T16:00:57.899401303Z 1:S 05 Oct 2019 16:00:57.898 * (Non critical) Master does not understand REPLCONF capa: -NOAUTH Authentication required. 2019-10-05T16:00:57.899404228Z 1:S 05 Oct 2019 16:00:57.898 * Partial resynchronization not possible (no cached master) 2019-10-05T16:00:57.899406828Z 1:S 05 Oct 2019 16:00:57.898 # Unexpected reply to PSYNC from master: -NOAUTH Authentication required. 2019-10-05T16:00:57.899409536Z 1:S 05 Oct 2019 16:00:57.898 * Retrying with SYNC... 2019-10-05T16:00:57.899412136Z 1:S 05 Oct 2019 16:00:57.899 # MASTER aborted replication with an error: NOAUTH Authentication required.
這時(shí)候已經(jīng)作為從節(jié)點(diǎn)連接新主節(jié)點(diǎn)了,但是授權(quán)認(rèn)證失敗,因?yàn)橹爸鞴?jié)點(diǎn)根本就沒有配置slaveof,這里我以為哨兵會(huì)自動(dòng)加上slaveof配置,但是沒有,難道是沒有使用配置文件的緣故?
已經(jīng)找到原因,并且在這篇中完全實(shí)現(xiàn)了
到此,關(guān)于“怎么用docker搭建redis主從哨兵模式,并整合進(jìn)springboot項(xiàng)目”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。