全国网站建设人员数量,我的营业执照网上查询,自助建微网站,花蝴蝶免费视频在线观看高清版单机模式#xff1a;单台缓存服务器#xff0c;开发、测试环境下使用#xff1b;哨兵模式#xff1a;主-从模式#xff0c;提高缓存服务器的高可用和安全性。所有缓存的数据在每个节点上都一致。每个节点添加监听器#xff0c;不断监听节点可用状态#xff0c;一旦主节点…单机模式单台缓存服务器开发、测试环境下使用哨兵模式主-从模式提高缓存服务器的高可用和安全性。所有缓存的数据在每个节点上都一致。每个节点添加监听器不断监听节点可用状态一旦主节点不能再提供服务。各监听器会立即在“从节点“中投票选择一台将之作为”主节点“器。从而使业务系统服务不会被中断。当然主节点具体出了什么问题还得运维人员排查并及时修复并上线集群模式分布式主从模式具有高可用、安全性高、数据量大、并发量大等优势。一般需要6台服务器3台主3台从。一般在缓存数据量大或者并发访问量非常高以至于单台服务器已经无法承受这样的数据量或访问量时才考虑集群模式。集群模式中几台主服务器中的数据是不一致的只有主和从中的数据是相同的。
在SpringBoot中使用哨兵模式和集群模式也是不费吹灰之力。对于我们使用来说和前面单机模式没有任何区别。唯一需要做的就是告诉SbringBoot框架这个项目我要使用哨兵模式这个项目我要使用集群模式。如何告诉框架呢当然是通过application.yml文件中的配置来说明 pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdlab-03-redis/artifactIdgroupIdcom.luo/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdlab-03-redis-06-redis-cluster/artifactIdpropertiesmaven.compiler.source8/maven.compiler.sourcemaven.compiler.target8/maven.compiler.target/propertiesdependencies!-- 实现对 Spring MVC 的自动化配置 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency!-- 实现对 Spring Data Redis 的自动化配置 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency!-- pool 对象池 --dependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactId/dependency!-- 阿里JSON解析器 --dependencygroupIdcom.alibaba.fastjson2/groupIdartifactIdfastjson2/artifactIdversion2.0.34/version/dependency!-- 引入 Swagger 依赖 --dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger2/artifactIdversion2.9.2/version/dependency!-- 引入 Swagger UI 依赖以实现 API 接口的 UI 界面 --dependencygroupIdio.springfox/groupIdartifactIdspringfox-swagger-ui/artifactIdversion2.9.2/version/dependency!-- 方便等会写单元测试 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency/dependencies/project
关于使用哨兵模式的Redis缓存配置
spring:profiles:active: cluster
---
spring:profiles: standalone# 默认使用的是lettuce框架封装的redis操作# 默认连接redis的s顺序 先 Sentinel哨兵模式 - Cluster集群 - 单机Redisredis:host: 127.0.0.1 # 连接redis的ipport: 6379database: 0 # 连接的是redis的几号数据库password: 123456 # 连接redis的密码lettuce:# 连接池pool:max-wait: 100ms # 连接的最大等待时间max-active: 8 # 最大连接数max-idle: 4 # 最大空闲连接数min-idle: 0 # 最小空闲连接数---
spring:profiles: sentinelredis:host: 127.0.0.1 # 连接redis的ipport: 6379database: 0 # 连接的是redis的几号数据库password: 123456 # 连接redis的密码lettuce:# 连接池pool:max-wait: 100ms # 连接的最大等待时间max-active: 8 # 最大连接数max-idle: 4 # 最大空闲连接数min-idle: 0 # 最小空闲连接数sentinel:master: mymaster # 配置哨兵时候master的名字nodes:- 127.0.0.1:26379- 127.0.0.1:26380- 127.0.0.1:26381---
spring:profiles: clusterredis:host: 127.0.0.1 # 连接redis的ipport: 6379database: 0 # 连接的是redis的几号数据库password: 123456 # 连接redis的密码lettuce:# 连接池pool:max-wait: 100ms # 连接的最大等待时间max-active: 8 # 最大连接数max-idle: 4 # 最大空闲连接数min-idle: 0 # 最小空闲连接数# 配置集群cluster:nodes:- 127.0.0.1:6379- 127.0.0.1:6380- 127.0.0.1:6381- 127.0.0.1:7379- 127.0.0.1:7380- 127.0.0.1:7381哨兵模式的配置中特别注意*ndes配置的是哨兵的IP和端口并非缓存服务器的 集群模式的配置都是缓存服务器的IP和端口。
接下来我们验证一下哨兵模式和集群模式在使用结果上的差异
哨兵模式
如下图所示三台缓存服务器每台服务都有对应哨兵
将6个服务启动后如下图所示 主6380从6379、6381
1、一主多从读写分离 //测试方法public Object getUserInfo(String username) {if (redisTemplate.opsForValue().get(username) null) {System.out.println(未获取到缓存新建用户信息.............);MapString, Object user new HashMap();user.put(username, username);user.put(usercode, zhangsan);user.put(sex, 男);user.put(createtime, new Date());redisTemplate.opsForValue().set(username, user);}return redisTemplate.opsForValue().get(username);}Testpublic void testRedis() throws InterruptedException {System.out.println(userService.getUserInfo(1));System.out.println(userService.getUserInfo(2));System.out.println(userService.getUserInfo(3));}三台缓存服务器存储结果都是一致的由此可见哨兵模式是一主多从和读写分离模式。
2、主服务宕机从服务升级主
哨兵模式另一目的就是当Master宏机后从服务可快速自动升级为Master不致于业务被中断。
当我们将主服务器6380停机之后将会出现以下的内容。 三台哨兵控制台中都打印了切换master的日志可以看出主服务器已经变为6381这台服务器了。 对于我们的系统而言由于缓存配置也是配置的哨兵的地址主服务挂了之后对于我们系统并无影响。
集群模式
集群模式一般需要6台服务器3主3从如下图共有6台缓存服务器。 分别启动6台服务器后再cmd命令模式下执行一个命令完成集群配置
redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380 127.0.0.1:7381 --cluster-replicas 1 -a 123456集群启动后 集群情况 主6379、6381、6380从7379、7380、7381
1、分布式存储 Testpublic void testRedis() throws InterruptedException {System.out.println(userService.getUserInfo(1));System.out.println(userService.getUserInfo(2));System.out.println(userService.getUserInfo(20000001));System.out.println(userService.getUserInfo(20200002));}从结果看 我们存储了4个值1、2、2000001、20200002。 1,2------------ 主6380从7381 2000001-----主6381从7379 20200002—主6379从7380
2、高可用
我们关闭主节点6380然后查看。 查看集群中各节点情况6380显示fail。而7380已经变成了master节点了。
现在从缓存中读取前面缓存的4个值虽然是异常提示127.0.0.1:6380连接失败但还是能获取到缓存内的值。这里的异常是因为执行Test方法时集群配置中有这台机器初始化时连接不上的异常。若是一般在运行中的web项目不会出现这样的异常 再次将6380启动起来后6380变成了slave从节点。 6380启动日志
[20520] 16 Jan 14:46:39.163 # Server initialized
[20520] 16 Jan 14:46:39.163 * DB loaded from append only file: 0.000 seconds
[20520] 16 Jan 14:46:39.163 * Ready to accept connections
[20520] 16 Jan 14:46:39.164 # Configuration change detected. Reconfiguring myself as a replica of c96d92167f25658f92b8e68fbe2cd641db0c9962
[20520] 16 Jan 14:46:39.164 * 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.
[20520] 16 Jan 14:46:39.165 # Cluster state changed: ok
[20520] 16 Jan 14:46:40.253 * Connecting to MASTER 127.0.0.1:7380
[20520] 16 Jan 14:46:40.253 * MASTER - REPLICA sync started
[20520] 16 Jan 14:46:40.256 * Non blocking connect for SYNC fired the event.
[20520] 16 Jan 14:46:40.256 * Master replied to PING, replication can continue...
[20520] 16 Jan 14:46:40.258 * Trying a partial resynchronization (request 3ce7caf46b989d9524c450b595a19e5d0c82b868:1).
[20520] 16 Jan 14:46:40.280 * Full resync from master: 609626e133d8d053640826b2da85b3789ed7ec02:5125
[20520] 16 Jan 14:46:40.280 * Discarding previously cached master state.
[20520] 16 Jan 14:46:40.419 * MASTER - REPLICA sync: receiving 419 bytes from master
[20520] 16 Jan 14:46:40.421 * MASTER - REPLICA sync: Flushing old data
[20520] 16 Jan 14:46:40.426 * MASTER - REPLICA sync: Loading DB in memory
[20520] 16 Jan 14:46:40.427 * MASTER - REPLICA sync: Finished with success
[20520] 16 Jan 14:46:40.442 * Background append only file rewriting started by pid 18008
[20520] 16 Jan 14:46:40.578 * AOF rewrite child asks to stop sending diffs.
[20520] 16 Jan 14:46:40.687 # fork operation complete
[20520] 16 Jan 14:46:40.697 * Background AOF rewrite terminated with success
[20520] 16 Jan 14:46:40.698 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
[20520] 16 Jan 14:46:40.700 * Background AOF rewrite finished successfully