当前位置:   article > 正文

docker部署redis集群并与springboot整合_redis cluster集群springboot高可用配置

redis cluster集群springboot高可用配置

一、redis集群部署方案

  1. 哨兵模式(Redis Sentinel)

Redis Sentinel 是 Redis 官方提供的高可用性方案,它通过对 Redis 的主从复制功能进行扩展,实现了 Redis 集群的高可用性。

哨兵模式的优点包括:

  1. 自动故障转移:当 Redis 主节点出现故障时,哨兵模式可以自动将一个从节点升级为主节点,从而实现自动故障转移。

  1. 自动恢复:当 Redis 主节点恢复正常时,哨兵模式可以自动将其重新加入到集群中,从而实现自动恢复。

  1. 监控功能:哨兵模式可以监控 Redis 集群中所有节点的状态,包括主节点和从节点,从而实现对 Redis 集群的全面监控。

哨兵模式的缺点包括:

  1. 性能损失:哨兵模式需要对 Redis 集群进行频繁的状态检查和故障转移操作,这会对 Redis 集群的性能产生一定的影响。

  1. 部署复杂:哨兵模式需要部署多个哨兵节点来实现高可用性,这会增加部署的复杂度。

因此,哨兵模式适用于对 Redis 集群的高可用性要求比较高的场景,但需要注意其对性能和部署复杂度的影响。如果对性能和部署复杂度要求较高,可以考虑使用其他的 Redis 集群方案,比如 Redis Cluster。

  1. Redis Cluster

在项目中最常用的 Redis 集群部署方式是 Redis Cluster。Redis Cluster 是 Redis 官方提供的 Redis 集群方案,它通过对 Redis 的源码进行修改,实现了 Redis 集群的分布式功能。在 Redis Cluster 中,每个节点都可以处理读写请求,同时还可以将数据自动分片到不同的节点上,从而实现了 Redis 集群的高可用性和扩展性。

Redis Cluster 的优点包括:

  1. 高可用性:Redis Cluster 可以将数据自动分片到不同的节点上,从而实现了 Redis 集群的高可用性。当某个节点出现故障时,Redis Cluster 可以自动将请求转发到其他节点上,从而实现自动故障转移。

  1. 扩展性:Redis Cluster 可以将数据自动分片到不同的节点上,从而实现了 Redis 集群的扩展性。当需要扩展 Redis 集群的容量时,只需要增加节点即可,无需对现有节点进行修改。

  1. 性能:Redis Cluster 可以将读写请求分散到不同的节点上,从而实现了 Redis 集群的负载均衡。同时,Redis Cluster 还可以将数据自动分片到不同的节点上,从而实现了 Redis 集群的并行处理能力,从而提高了 Redis 集群的性能。

Redis Cluster 的缺点包括:

  1. 部署复杂:Redis Cluster 需要部署多个节点来实现高可用性和扩展性,这会增加部署的复杂度。

  1. 数据一致性:Redis Cluster 采用的是无中心化的分布式架构,因此在节点之间进行数据同步时,可能会出现数据不一致的情况。Redis Cluster 采用了 Gossip 协议来解决数据同步的问题。

二、docker部署Redis Cluster集群

  1. 安装docker参考官网https://docs.docker.com/desktop/install/linux-install/

我使用的是CentOS-7-x86_64-DVD-2009.iso

  1. 关闭防火墙

  1. systemctl stop firewalld
  2. systemctl disable firewalld
  1. 设置固定ip

  1. cd /etc/sysconfig/network-scripts
  2. ll
  3. vim ifcfg-ens33

修改(有的修改,没有的添加)

  1. BOOTPROTO=static
  2. IPADDR="192.168.10.11"
  3. NETMASK="255.255.255.0"
  4. GATEWAY="192.168.10.1"
  5. DNS1="114.114.114.114"

重启网关

systemctl restart network
  1. 拉取镜像

docker pull redis:6.2.10

主节点

从节点

第一集群

192.168.10.11:6379

192.168.10.12:6380

第二集群

192.168.10.12:6379

192.168.10.13:6380

第三集群

192.168.10.13:6379

192.168.10.11:6380

  1. 编写docker-compose.yml文件

主节点

  1. version: '3.1'
  2. services:
  3. redis-master:
  4. image: redis:6.2.10 # 创建容器时所需的镜像
  5.     container_name: redis-master # 服务名称
  6.     restart: always # 容器总是重新启动
  7.     network_mode: "host" # host 网络模式
  8. command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
  9. volumes: # 数据卷,目录挂载
  10. - ./redis.conf:/usr/local/etc/redis/redis.conf
  11.       - ./data:/data
  12. ports:
  13. - "6379:6379"
  14.       - "16379:16379"

准备redis.conf文件

可自行在官网下载,需要修改的属性有如下:

  1. port 6379 #端口,默认是6379
  2. bind 0.0.0.0 #监听的IP,默认是本地127.0.0.1
  3. protected-mode no #保护模式默认是开启状态,需要修改为no
  4. cluster-enabled yes #开启集群
  5. cluster-config-file nodes.conf #集群的配置
  6. cluster-node-timeout 5000 #请求超时默认15秒,可自行设置
  7. appendonly yes #开启aof持久化
  8. requirepass ****** #密码
  9. masterauth *****
  10. daemonize no #开启守护线程,默认为no,可不开启

执行docker compose up -d

从节点

  1. version: '3.1'
  2. services:
  3. redis-master:
  4. image: redis:6.2.10 # 创建容器时所需的镜像
  5. container_name: redis-slave # 服务名称
  6. restart: always # 容器总是重新启动
  7.     network_mode: "host" # host 网络模式
  8. command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
  9. volumes: # 数据卷,目录挂载
  10. - ./redis.conf:/usr/local/etc/redis/redis.conf
  11. - ./data:/data
  12. ports:
  13. - "6380:6380"
  14. - "16380:16380"

准备redis.conf文件

可自行在官网下载,需要修改的属性有如下:

  1. port 6380 #端口,默认是6379
  2. bind 0.0.0.0 #监听的IP,默认是本地127.0.0.1
  3. protected-mode no #保护模式默认是开启状态,需要修改为no
  4. cluster-enabled yes #开启集群
  5. cluster-config-file nodes.conf #集群的配置
  6. cluster-node-timeout 5000 #请求超时默认15秒,可自行设置
  7. appendonly yes #开启aof持久化
  8. requirepass ****** #密码
  9. masterauth *****
  10. daemonize no #开启守护线程,默认为no,可不开启

执行docker compose up -d

请先确保你的两台机器可以互相通信,然后随便进入一个容器节点,并进入 /usr/local/bin/ 目录:

  1. # 进入容器
  2. docker exec -it redis-master bash
  3. # 切换至指定目录
  4. cd /usr/local/bin/

接下来我们就可以通过以下命令实现 Redis Cluster 集群的创建

redis-cli -a ***** --cluster create 192.168.10.11:6379 192.168.10.11:6380 192.168.10.12:6379 192.168.10.12:6380 192.168.10.13:6379 192.168.10.13:6380 --cluster-replicas 1

*****对应密码

出现选择提示信息,输入 yes,结果如下所示:

成功后显示

可参考:https://www.cnblogs.com/pgyLang/p/16253803.html

三、springboot整合Redis Cluster集群

引入Redis依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <!-- redis依赖commons-pool 这个依赖一定要添加 -->
  6. <dependency>
  7. <groupId>org.apache.commons</groupId>
  8. <artifactId>commons-pool2</artifactId>
  9. </dependency>

编写配置文件

  1. spring:
  2. redis:
  3. password: ****** #密码
  4. lettuce: #lettuce连接池配置
  5. pool:
  6. max-active: 8
  7. max-idle: 8
  8. min-idle: 0
  9. max-wait: 1000
  10. shutdown-timeout: 100
  11. cluster: #集群配置
  12. nodes: 192.168.10.11:6379,192.168.10.11:6380,192.168.10.12:6379,192.168.10.12:6380,192.168.10.13:6379,192.168.10.13:6380
  13. max-redirects: 3

编写启动项

  1. @Configuration
  2. public class RedisCacheAutoConfiguration extends CachingConfigurerSupport {
  3. @Value("${spring.redis.cluster.nodes}")
  4. private String nodes;
  5. @Value("${spring.redis.password}")
  6. private String password;
  7. //redis集群配置,6个ip以,分割,然后再以:分割
  8. @Bean
  9. public RedisClusterConfiguration redisClusterConfiguration(){
  10. RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
  11. String[] cNodes = nodes.split(",");
  12. Set<RedisNode> hp = new HashSet<>();
  13. for (String node : cNodes) {
  14. String[] split = node.split(":");
  15. hp.add(new RedisNode(split[0].trim(),Integer.parseInt(split[1])));
  16. }
  17. redisClusterConfiguration.setPassword(password);
  18. redisClusterConfiguration.setClusterNodes(hp);
  19. return redisClusterConfiguration;
  20. }
  21. /**
  22. * 注入RedisTemplate
  23. * tips:泛型可以适用于各种类型的注入
  24. */
  25. @Bean
  26. public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory, RedisSerializer<?> redisSerializer) {
  27. // 指定序列化方式
  28. RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
  29. redisTemplate.setKeySerializer(RedisSerializer.string());
  30. redisTemplate.setHashKeySerializer(RedisSerializer.string());
  31. redisTemplate.setValueSerializer(redisSerializer);
  32. redisTemplate.setHashValueSerializer(redisSerializer);
  33. redisTemplate.setConnectionFactory(factory);
  34. return redisTemplate;
  35. }
  36. /**
  37. * 初始化Redis序列器,使用jackson
  38. */
  39. @Bean
  40. public RedisSerializer<Object> redisSerializer() {
  41. ObjectMapper objectMapper = new ObjectMapper();
  42. // jdk8日期格式支持
  43. DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  44. DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  45. Module timeModule = new JavaTimeModule()
  46. .addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter))
  47. .addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(timeFormatter))
  48. .addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter))
  49. .addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(timeFormatter));
  50. objectMapper.registerModule(timeModule);
  51. Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
  52. jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
  53. return jackson2JsonRedisSerializer;
  54. }
  55. }

编写工具类

  1. @Component
  2. public final class RedisClient {
  3. @Autowired
  4. private RedisTemplate<String, Object> redisTemplate;
  5. /**
  6. * 指定缓存失效时间
  7. * (命令EXPIRE)
  8. *
  9. * @param key 键
  10. * @param time 时间(秒)
  11. * @param timeUnit 时间单位
  12. * @return true / false
  13. */
  14. public boolean expire(String key, long time, TimeUnit timeUnit) {
  15. if (time > 0) {
  16. return redisTemplate.expire(key, time, timeUnit);
  17. }
  18. return false;
  19. }
  20. /**
  21. * 根据 key 获取过期时间
  22. * (命令TTL)
  23. *
  24. * @param key 键
  25. * @return
  26. */
  27. public long ttl(String key) {
  28. return redisTemplate.getExpire(key, TimeUnit.SECONDS);
  29. }
  30. /**
  31. * 判断 key 是否存在
  32. * (命令EXISTS)
  33. *
  34. * @param key 键
  35. * @return true / false
  36. */
  37. public boolean exists(String key) {
  38. return redisTemplate.hasKey(key);
  39. }
  40. /**
  41. * 删除缓存(不存在key不会抛异常)
  42. * (命令 DEL)
  43. *
  44. * @param keys 集合
  45. */
  46. public long delete(Collection<String> keys) {
  47. return redisTemplate.delete(keys);
  48. }
  49. /**
  50. * 删除缓存
  51. * (命令DEL)
  52. *
  53. * @param key 键(一个或者多个)
  54. */
  55. public boolean delete(String... key) {
  56. if (key != null && key.length > 0) {
  57. if (key.length == 1) {
  58. return redisTemplate.delete(key[0]);
  59. } else {
  60. return redisTemplate.delete(Arrays.asList(key)) > 0;
  61. }
  62. }
  63. return true;
  64. }
  65. /**
  66. * 按表达式删除缓存,使用scan命令实现,不会有太多性能影响
  67. *
  68. * @param pattern 表达式
  69. */
  70. public long deleteAll(String pattern) {
  71. if (pattern == null || pattern.length() == 0) {
  72. throw new IllegalArgumentException("参数不正确");
  73. }
  74. return redisTemplate.delete(scan(pattern));
  75. }
  76. /**
  77. * 查询所有满足表达式的key
  78. * (命令KEYS,实际是利用SCAN命令实现,不会产生阻塞)
  79. *
  80. * @param pattern 表达式
  81. * @return 符合表达式的key的集合
  82. * @complexity O(n)
  83. */
  84. public Set<String> scan(String pattern) {
  85. if (pattern == null || pattern.length() == 0) {
  86. throw new IllegalArgumentException("参数不正确");
  87. }
  88. return redisTemplate.execute((RedisCallback<Set<String>>) connection -> {
  89. Set<String> keys = new HashSet<>();
  90. Cursor<byte[]> cursor = connection.scan(ScanOptions.scanOptions().match(pattern).count(1000).build());
  91. while (cursor.hasNext()) {
  92. keys.add(new String(cursor.next()));
  93. }
  94. return keys;
  95. }
  96. );
  97. }
  98. // ============================== String ==============================
  99. /**
  100. * 放入缓存
  101. * (命令 SET)
  102. *
  103. * @param key 键
  104. * @param value 值
  105. */
  106. public void set(String key, Object value, long timeout, TimeUnit timeUnit) {
  107. redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
  108. }
  109. /**
  110. * 放入缓存
  111. * (命令 SET)
  112. *
  113. * @param key 键
  114. * @param value 值
  115. */
  116. public void set(String key, Object value) {
  117. redisTemplate.opsForValue().set(key, value);
  118. }
  119. /**
  120. * 获取缓存(string数据结构) 通过泛型T指定缓存数据类型
  121. * (命令 GET)
  122. *
  123. * @param key 键
  124. * @return
  125. * @see #hGetAll
  126. */
  127. public <T> T get(String key) {
  128. return key == null ? null : (T) redisTemplate.opsForValue().get(key);
  129. }
  130. /**
  131. * 获取缓存(string数据结构) 通过泛型T指定缓存数据类型
  132. * (命令 GETSET)
  133. *
  134. * @param key 键
  135. * @return
  136. * @see #hGetAll
  137. */
  138. public <T> T getSet(String key, T newValue) {
  139. return key == null ? null : (T) redisTemplate.opsForValue().getAndSet(key, newValue);
  140. }
  141. /**
  142. * 缓存普通键值对,并设置失效时间
  143. *
  144. * @param key 键
  145. * @param value 值
  146. * @param seconds 时间(秒),如果 time <= 0 则不设置失效时间
  147. */
  148. public void set(String key, Object value, int seconds) {
  149. if (seconds > 0) {
  150. redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
  151. } else {
  152. redisTemplate.opsForValue().set(key, seconds);
  153. }
  154. }
  155. /**
  156. * 当key不存在时放入键值对
  157. * 如果已经存在key返回false
  158. *
  159. * @param key 键
  160. * @param value 值
  161. * @return true/false
  162. */
  163. public boolean setNx(String key, Object value) {
  164. return redisTemplate.opsForValue().setIfAbsent(key, value);
  165. }
  166. /**
  167. * 当key不存在时放入键值对,并在指定时间后自动删除
  168. * 如果已经存在key返回false
  169. *
  170. * @param key 键
  171. * @param value 值
  172. * @param time 时间
  173. * @param timeUnit 时间单位
  174. * @return true/false
  175. */
  176. public boolean setNx(String key, Object value, long time, TimeUnit timeUnit) {
  177. return redisTemplate.opsForValue().setIfAbsent(key, value, time, timeUnit);
  178. }
  179. /**
  180. * 递增
  181. * 如果不存在key将自动创建
  182. * (命令INCR)
  183. *
  184. * @param key 键
  185. * @param increment 递增大小
  186. * @return 递增后的值
  187. */
  188. @SuppressWarnings("unchecked")
  189. public <T extends Number> T increment(String key, T increment) {
  190. return (T) redisTemplate.opsForValue().increment(key, increment.doubleValue());
  191. }
  192. /**
  193. * 递增1
  194. * 如果不存在key将自动创建
  195. * (命令INCR)
  196. *
  197. * @param key 键
  198. * @return 递增后的值
  199. */
  200. public long increment(String key) {
  201. return redisTemplate.opsForValue().increment(key, 1);
  202. }
  203. /**
  204. * 递减
  205. * 如果不存在key将自动创建
  206. * (命令DECR)
  207. *
  208. * @param key 键
  209. * @param decrement 递减大小
  210. * @return 递减后的值
  211. */
  212. @SuppressWarnings("unchecked")
  213. public <T extends Number> T decrement(String key, T decrement) {
  214. return (T) redisTemplate.opsForValue().increment(key, -decrement.longValue());
  215. }
  216. /**
  217. * 递减1
  218. * 如果不存在key将自动创建
  219. * (命令DECR)
  220. *
  221. * @param key 键
  222. * @return 递减后的值
  223. */
  224. public long decrement(String key) {
  225. return decrement(key, 1L);
  226. }
  227. // ============================== Map ==============================
  228. /**
  229. * 往指定HashMap中添加一对键值对,key不存在将自动创建
  230. * (命令HSET)
  231. *
  232. * @param name HashMap的名字
  233. * @param key 添加的键
  234. * @param value 添加的值
  235. */
  236. public void hSet(String name, String key, Object value) {
  237. redisTemplate.opsForHash().put(name, key, value);
  238. }
  239. /**
  240. * 往指定HashMap中添加另一个map
  241. * (命令HSET)
  242. *
  243. * @param name HashMap的名字
  244. * @param map 值
  245. */
  246. public void hSet(String name, Map<String, ?> map) {
  247. redisTemplate.opsForHash().putAll(name, map);
  248. }
  249. /**
  250. * 从指定HashMap中获取指定key的值
  251. * (命令HGET)
  252. *
  253. * @param mapName HashMap的名字(no null)
  254. * @param key HashMap中的键(no null)
  255. * @param <T> 根据实际类型自定义
  256. * @return
  257. */
  258. @SuppressWarnings("unchecked")
  259. public <T> T hGet(String mapName, String key) {
  260. return (T) redisTemplate.opsForHash().get(mapName, key);
  261. }
  262. /**
  263. * 从指定HashMap中获取多个key的值
  264. * (命令HMGET)
  265. *
  266. * @param mapName HashMap的名字(no null)
  267. * @param key HashMap中的键,传多个(no null)
  268. * @param <T> 根据实际类型自定义
  269. * @return list
  270. */
  271. public <T> List<T> hMultiGet(String mapName, String... key) {
  272. return redisTemplate.<String, T>opsForHash().multiGet(mapName, Arrays.asList(key));
  273. }
  274. /**
  275. * 获取指定的HashMap
  276. * (命令HGETALL)
  277. *
  278. * @param mapName HashMap的名字(no null)
  279. * @return HashMap
  280. */
  281. public <T> Map<String, T> hGetAll(String mapName) {
  282. return redisTemplate.<String, T>opsForHash().entries(mapName);
  283. }
  284. /**
  285. * 删除 HashMap 中的值
  286. * (命令HDEL)
  287. *
  288. * @param mapName HashMap的名字
  289. * @param keys HashMap中的key(可以多个,no null)
  290. * @return 成功删除个数
  291. */
  292. public long hDelete(String mapName, Object... keys) {
  293. return redisTemplate.opsForHash().delete(mapName, keys);
  294. }
  295. /**
  296. * map 递增1
  297. * (命令 HINCRBY)
  298. *
  299. * @param hashMapName HashMap的名字(no null)
  300. * @param key HashMap中的key(no null)
  301. * @return true / false
  302. */
  303. public long hIncrement(String hashMapName, String key) {
  304. return redisTemplate.opsForHash().increment(hashMapName, key, 1);
  305. }
  306. /**
  307. * map 递增指定值
  308. * (命令 HINCRBY)
  309. *
  310. * @param hashMapName HashMap的名字(no null)
  311. * @param key HashMap中的key(no null)
  312. * @return true / false
  313. */
  314. public Long hIncrementBy(String hashMapName, String key, long delta) {
  315. return redisTemplate.opsForHash().increment(hashMapName, key, delta);
  316. }
  317. /**
  318. * 是否存在hashkey
  319. * (命令 HEXISTS)
  320. *
  321. * @param hashMapName HashMap的名字(no null)
  322. * @param hashKey HashMap中的key(no null)
  323. * @return true / false
  324. */
  325. public boolean hExists(String hashMapName, Object hashKey) {
  326. return redisTemplate.opsForHash().hasKey(hashMapName, hashKey);
  327. }
  328. /**
  329. * 设置map中的值,只有当key不存在时,才能设置成功
  330. * (命令 HSETNX)
  331. *
  332. * @param hashMapName HashMap的名字
  333. * @param value value(no null)
  334. * @return true / false
  335. */
  336. public boolean hSetNx(String hashMapName, String key, Object value) {
  337. return redisTemplate.opsForHash().putIfAbsent(hashMapName, key, value);
  338. }
  339. /**
  340. * 获取map的大小
  341. * (命令 HLEN)
  342. *
  343. * @param hashMapName HashMap的名字
  344. * @return map size
  345. */
  346. public Long hSize(String hashMapName) {
  347. return redisTemplate.opsForHash().size(hashMapName);
  348. }
  349. /**
  350. * 获取map中的所有key
  351. * (命令 HKEYS)
  352. *
  353. * @param hashMapName HashMap的名字
  354. * @return all keys
  355. */
  356. public Set<String> hKeys(String hashMapName) {
  357. return redisTemplate.<String, Object>opsForHash().keys(hashMapName);
  358. }
  359. /**
  360. * 获取map中的所有value
  361. * (命令 HVALS)
  362. *
  363. * @param hashMapName HashMap的名字
  364. * @return all values
  365. */
  366. public <T> List<T> hValues(String hashMapName) {
  367. return redisTemplate.<String, T>opsForHash().values(hashMapName);
  368. }
  369. // ============================== zset ==============================
  370. /**
  371. * 添加zset元素 有则覆盖
  372. *
  373. * @param key
  374. * @param v
  375. * @param score
  376. * @return
  377. */
  378. public <T> boolean zAdd(String key, T v, long score) {
  379. return redisTemplate.opsForZSet().add(key, v, score);
  380. }
  381. /**
  382. * 批量添加zset元素
  383. * (命令 ZADD)
  384. *
  385. * @param key
  386. * @param tuples
  387. * @return
  388. */
  389. public Long zAdd(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
  390. return redisTemplate.opsForZSet().add(key, tuples);
  391. }
  392. /**
  393. * 移除指定位置的zset元素
  394. * (命令 ZREVRANGE)
  395. *
  396. * @param key
  397. * @param start
  398. * @param end
  399. * @return
  400. */
  401. public Long zRemoveRange(String key, long start, long end) {
  402. return redisTemplate.opsForZSet().removeRange(key, start, end);
  403. }
  404. /**
  405. * 添加zset元素 有则覆盖
  406. * (命令 ZREM)
  407. *
  408. * @param key
  409. * @return
  410. */
  411. public Long zRemove(String key, Object... vs) {
  412. return redisTemplate.opsForZSet().remove(key, vs);
  413. }
  414. /**
  415. * zset 元素个数
  416. * (命令 ZCARD)
  417. *
  418. * @param key
  419. * @return
  420. */
  421. public Long zSize(String key) {
  422. return redisTemplate.opsForZSet().size(key);
  423. }
  424. /**
  425. * zset中是否存在对应元素
  426. *
  427. * @param key
  428. * @param v
  429. * @return
  430. */
  431. public <T> boolean zExists(String key, T v) {
  432. return redisTemplate.opsForZSet().rank(key, v) != null;
  433. }
  434. /**
  435. * 自增zset score
  436. * (命令 ZINCRBY)
  437. *
  438. * @param key
  439. * @param v
  440. * @param delta
  441. * @return
  442. */
  443. public <T> Double zIncrementBy(String key, T v, Double delta) {
  444. return redisTemplate.opsForZSet().incrementScore(key, v, delta);
  445. }
  446. /**
  447. * 获取指定key的所有元素
  448. *
  449. * @param key
  450. * @param <T>
  451. * @return
  452. */
  453. public <T> LinkedHashSet<T> zAll(String key) {
  454. return (LinkedHashSet<T>) redisTemplate.opsForZSet().range(key, 0, -1);
  455. }
  456. /**
  457. * 获取指定位置元素
  458. *
  459. * @param key
  460. * @param index
  461. * @param <T>
  462. * @return
  463. */
  464. public <T> T zGet(String key, int index) {
  465. Set<Object> objects = redisTemplate.opsForZSet().range(key, index, index + 1);
  466. return (T) objects.iterator().next();
  467. }
  468. /**
  469. * zset返回指定元素的索引
  470. * (命令 ZRANK)
  471. *
  472. * @param key
  473. * @param v
  474. * @return
  475. */
  476. public Long zRank(String key, Object v) {
  477. return redisTemplate.opsForZSet().rank(key, v);
  478. }
  479. /**
  480. * zset通过索引区间获取指定区间内的元素
  481. * (命令 ZRANGE)
  482. *
  483. * @param key
  484. * @param start 开始索引
  485. * @param end 结束索引
  486. * @return 元素集合
  487. */
  488. public <T> Set<T> zRange(String key, long start, long end) {
  489. return (Set<T>) redisTemplate.opsForZSet().range(key, start, end);
  490. }
  491. /**
  492. * zset查询元素分数值
  493. * (命令 ZSCORE)
  494. *
  495. * @param key
  496. * @param v
  497. * @return 元素分数值
  498. */
  499. public Double zScore(String key, Object v) {
  500. return redisTemplate.opsForZSet().score(key, v);
  501. }
  502. /**
  503. * zset查询指定元素排名
  504. * (命令 ZREVRANK)
  505. *
  506. * @param key
  507. * @param v
  508. * @return 排名
  509. */
  510. public Long zReverseRank(String key, Object v) {
  511. return redisTemplate.opsForZSet().reverseRank(key, v);
  512. }
  513. /**
  514. * 通过分数返回zset指定区间内的元素
  515. * (命令 ZRANGEBYSCORE)
  516. *
  517. * @param key
  518. * @param min 最小分数值
  519. * @param max 最大分数值
  520. * @return 元素集合
  521. */
  522. public <T> Set<T> zRangeByScore(String key, double min, double max) {
  523. return (Set<T>) redisTemplate.opsForZSet().rangeByScore(key, min, max);
  524. }
  525. /**
  526. * 移除zset中指定分数区间的元素
  527. * (命令 ZREMRANGEBYRANK)
  528. *
  529. * @param key
  530. * @param min 最小分数值
  531. * @param max 最大分数值
  532. * @return 删除元素个数
  533. */
  534. public Long zRemoveRangeByScore(String key, double min, double max) {
  535. return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
  536. }
  537. /**
  538. * 获取zset中指定分数区间的元素数量
  539. * (命令 ZCOUNT)
  540. *
  541. * @param key
  542. * @param min 最小分数值
  543. * @param max 最大分数值
  544. * @return 删除元素个数
  545. */
  546. public Long zCount(String key, double min, double max) {
  547. return redisTemplate.opsForZSet().count(key, min, max);
  548. }
  549. /**
  550. * 获取指定范围分数的元素
  551. * (命令 ZREVRANGEBYSCORE )
  552. *
  553. * @param key
  554. * @param min 最小分数值
  555. * @param max 最大分数值
  556. * @param offset 偏移量
  557. * @param count 最大元素集合
  558. * @return 元素集合
  559. */
  560. public <T> Set<T> zReverseRangeByScore(String key, double min, double max, long offset, long count) {
  561. return (Set<T>) redisTemplate.opsForZSet().reverseRangeByScore(key, min, max, offset, count);
  562. }
  563. /**
  564. * zset根据得分范围获取元素(倒序)
  565. * 命令:ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
  566. * 时间复杂度:O(log(N)+M) N是zest长度,M是返回数量
  567. *
  568. * @param key redis key
  569. * @param min 最小分数(含)
  570. * @param max 最大分数(含)
  571. * @param offset 偏移量
  572. * @param count 返回数量
  573. * @param <T>
  574. * @return 包含元素和分数的TypedTuple集合
  575. * @see <a href="https://redis.io/commands/zrevrangebyscore">Redis Documentation: ZREVRANGEBYSCORE</a>
  576. */
  577. public <T> Set<ZSetOperations.TypedTuple<T>> zReverseRangeByScoreWithScores(String key, double min, double max, long offset, long count) {
  578. return ((RedisTemplate<String, T>) redisTemplate).opsForZSet().reverseRangeByScoreWithScores(key, min, max, offset, count);
  579. }
  580. // ============================== set ==============================
  581. /**
  582. * 新增set 元素
  583. *
  584. * @param key
  585. * @param v
  586. * @return
  587. */
  588. public <T> Long sAdd(String key, T v) {
  589. return redisTemplate.opsForSet().add(key, v);
  590. }
  591. /**
  592. * 获取set所有元素
  593. * (命令 SMEMBERS )
  594. *
  595. * @param key
  596. * @return
  597. */
  598. public <T> Set<T> sMembers(String key) {
  599. return (Set<T>) redisTemplate.opsForSet().members(key);
  600. }
  601. /**
  602. * 新增set元素
  603. *
  604. * @param key
  605. * @param values
  606. */
  607. public <T> void sAdd(String key, T... values) {
  608. redisTemplate.opsForSet().add(key, values);
  609. }
  610. /**
  611. * 批量移除set元素
  612. *
  613. * @param key
  614. * @param values
  615. * @return 删除元素数量
  616. */
  617. public Long sRemove(String key, Object... values) {
  618. return redisTemplate.opsForSet().remove(key, values);
  619. }
  620. /**
  621. * 获取set的元素数量
  622. * (命令 SCARD)
  623. *
  624. * @param key
  625. * @return 元素数量
  626. */
  627. public Long sSize(String key) {
  628. return redisTemplate.opsForSet().size(key);
  629. }
  630. /**
  631. * set判断元素是否存在
  632. * (命令 SISMEMBER)
  633. *
  634. * @param key
  635. * @return true / false
  636. */
  637. public boolean sExists(String key, Object v) {
  638. return redisTemplate.opsForSet().isMember(key, v);
  639. }
  640. /**
  641. * set判断元素是否存在
  642. * (命令 SISMEMBER)
  643. *
  644. * @param key
  645. * @return true / false
  646. */
  647. public boolean sIsMember(String key, Object v) {
  648. return redisTemplate.opsForSet().isMember(key, v);
  649. }
  650. /**
  651. * 移除并返回set中的一个随机元素
  652. * (命令 SPOP)
  653. *
  654. * @param key
  655. * @return 元素
  656. */
  657. public <T> T sPop(String key) {
  658. return (T) redisTemplate.opsForSet().pop(key);
  659. }
  660. /**
  661. * 随机返回set中的一个或者多个元素(不移除)
  662. * (命令 SRANDMEMBER)
  663. *
  664. * @param key
  665. * @param count 元素数量
  666. * @return 元素集合
  667. */
  668. public <T> List<T> sRandomMembers(String key, long count) {
  669. return (List<T>) redisTemplate.opsForSet().randomMembers(key, count);
  670. }
  671. /**
  672. * 获取第一个set和其他set的差集
  673. * (命令 SDIFF)
  674. *
  675. * @param key
  676. * @param otherKey
  677. * @return 元素集合
  678. */
  679. public <T> Set<T> sDifference(String key, String otherKey) {
  680. return (Set<T>) redisTemplate.opsForSet().difference(key, otherKey);
  681. }
  682. /**
  683. * 获取第一个set和其他set的差集
  684. * (命令 SDIFF)
  685. *
  686. * @param key
  687. * @param otherKeys
  688. * @return 元素集合
  689. */
  690. public <T> Set<T> sDifference(String key, Collection<String> otherKeys) {
  691. return (Set<T>) redisTemplate.opsForSet().difference(key, otherKeys);
  692. }
  693. /**
  694. * 获取第一个set和其他set的并集
  695. * (命令 SUNION)
  696. *
  697. * @param key
  698. * @param otherKey
  699. * @return 元素集合
  700. */
  701. public <T> Set<T> sUnion(String key, String otherKey) {
  702. return (Set<T>) redisTemplate.opsForSet().union(key, otherKey);
  703. }
  704. /**
  705. * 获取第一个set和其他set的并集
  706. * (命令 SUNION)
  707. *
  708. * @param key
  709. * @param otherKeys
  710. * @return 元素集合
  711. */
  712. public <T> Set<T> sUnion(String key, Collection<String> otherKeys) {
  713. return (Set<T>) redisTemplate.opsForSet().union(key, otherKeys);
  714. }
  715. /**
  716. * 获取第一个set和其他set的交集
  717. * (命令 SINTER)
  718. *
  719. * @param key
  720. * @param otherKey
  721. * @return 元素集合
  722. */
  723. public <T> Set<T> sIntersect(String key, String otherKey) {
  724. return (Set<T>) redisTemplate.opsForSet().intersect(key, otherKey);
  725. }
  726. /**
  727. * 获取第一个set和其他set的交集
  728. * (命令 SINTER)
  729. *
  730. * @param key
  731. * @param otherKeys
  732. * @return 元素集合
  733. */
  734. public <T> Set<T> sIntersect(String key, Collection<String> otherKeys) {
  735. return (Set<T>) redisTemplate.opsForSet().intersect(key, otherKeys);
  736. }
  737. // ============================== list ==============================
  738. /**
  739. * 查询list中指定位置元素
  740. * (命令 LINDEX)
  741. *
  742. * @param key key
  743. * @param index 位置
  744. */
  745. public <T> T lIndex(String key, long index) {
  746. return (T) redisTemplate.opsForList().index(key, index);
  747. }
  748. /**
  749. * 从左边塞入元素
  750. *
  751. * @param key
  752. * @param v
  753. */
  754. public <T> Long lPush(String key, T v) {
  755. return redisTemplate.opsForList().leftPush(key, v);
  756. }
  757. /**
  758. * 从左边塞入元素
  759. *
  760. * @param key
  761. * @param values
  762. */
  763. public Long lPushAll(String key, Collection<Object> values) {
  764. return redisTemplate.opsForList().leftPushAll(key, values);
  765. }
  766. /**
  767. * 从右边取出元素
  768. * (命令 LRANGE)
  769. *
  770. * @param key
  771. * @param start
  772. * @param end
  773. * @param <T>
  774. * @return
  775. */
  776. public <T> List<T> lRange(String key, long start, long end) {
  777. return (List<T>) redisTemplate.opsForList().range(key, start, end);
  778. }
  779. /**
  780. * 做左边拉元素
  781. * 命令 (LPOP)
  782. *
  783. * @param key
  784. * @param <T>
  785. * @return
  786. */
  787. public <T> T lPop(String key) {
  788. return (T) redisTemplate.opsForList().rightPop(key);
  789. }
  790. /**
  791. * 从右边批量塞入元素
  792. *
  793. * @param key
  794. * @param vs
  795. */
  796. public <T> Long rPushAll(String key, T... vs) {
  797. return redisTemplate.opsForList().rightPushAll(key, vs);
  798. }
  799. /**
  800. * 从右边批量塞入元素
  801. *
  802. * @param key
  803. * @param vs
  804. */
  805. public <T> Long rPush(String key, T t) {
  806. return redisTemplate.opsForList().rightPush(key, t);
  807. }
  808. /**
  809. * 删除右边的元素
  810. *
  811. * @param key
  812. * @param removeCount
  813. */
  814. public void rRemove(String key, Long removeCount) {
  815. redisTemplate.opsForList().trim(key, 0, lSize(key) - removeCount);
  816. }
  817. /**
  818. * list集合中元素个数
  819. * 命令 (LLEN)
  820. *
  821. * @param key
  822. */
  823. public Long lSize(String key) {
  824. return redisTemplate.opsForList().size(key);
  825. }
  826. /**
  827. * list集合中元素个数
  828. * 命令 (LLEN)
  829. *
  830. * @param key
  831. */
  832. public Long lLen(String key) {
  833. return redisTemplate.opsForList().size(key);
  834. }
  835. /**
  836. * 移除list集合中元素
  837. * 命令 (LMOVE)
  838. *
  839. * @param k
  840. * @param v
  841. */
  842. public Long lRemove(String k, Object v) {
  843. return redisTemplate.opsForList().remove(k, 0, v);
  844. }
  845. /**
  846. * 批量查询
  847. *
  848. * @param key
  849. */
  850. public <T> List<T> lAll(String key) {
  851. return (List<T>) redisTemplate.opsForList().range(key, 0, -1);
  852. }
  853. /**
  854. * list截取列表
  855. * (命令 LTRIM)
  856. *
  857. * @param key key
  858. * @param start
  859. * @param end
  860. */
  861. public void lTrim(String key, long start, long end) {
  862. redisTemplate.opsForList().trim(key, start, end);
  863. }
  864. /**
  865. * list设置指定位置的元素
  866. * (命令 LSET)
  867. *
  868. * @param key key
  869. * @param index 位置
  870. * @param v value
  871. */
  872. public void lSet(String key, long index, Object v) {
  873. redisTemplate.opsForList().set(key, index, v);
  874. }
  875. // ============================== pipelined ==============================
  876. /**
  877. * 管道操作
  878. * 一次性提交多个命令,提升效率,适用于批量操作
  879. *
  880. * @param consumer
  881. * @param <T>
  882. * @return 命令结果
  883. */
  884. public <T> List<T> pipelined(Consumer<RedisOperations<String, Object>> consumer) {
  885. return (List<T>) redisTemplate.executePipelined(new SessionCallback<Object>() {
  886. @Override
  887. public Object execute(RedisOperations operations) throws DataAccessException {
  888. consumer.accept(operations);
  889. return null;
  890. }
  891. });
  892. }
  893. // ============================== hyperLogLogAdd ==============================
  894. /**
  895. * HyperLogLog是用来做基数统计的算法,它提供不精确的去重计数方案(这个不精确并不是非常不精确),标准误差是0.81%,
  896. * 对于UV这种统计来说这样的误差范围是被允许的。HyperLogLog的优点在于,输入元素的数量或者体积非常大时,
  897. * 基数计算的存储空间是固定的。在Redis中,每个HyperLogLog键只需要花费12KB内存,就可以计算接近2^64个不同的基数。
  898. * ————————————————
  899. * HyperLogLog指令都是pf(PF)开头,这是因为HyperLogLog的发明人是Philippe Flajolet,pf是他的名字的首字母缩写。
  900. */
  901. /**
  902. * hyperLogLogAdd 加入元素
  903. * (命令 PFADD)
  904. *
  905. * @param key key
  906. * @param value value
  907. */
  908. public void pfAdd(String key, Object value) {
  909. redisTemplate.opsForHyperLogLog().add(key, value);
  910. }
  911. /**
  912. * hyperLogLogAdd 获取元素数量
  913. * (命令 PFCOUNT)
  914. *
  915. * @param key key
  916. */
  917. public Long pfCount(String key) {
  918. return redisTemplate.opsForHyperLogLog().size(key);
  919. }
  920. // ============================== bitmap ==============================
  921. /**
  922. * bitmap设置指定位置 为 1或0
  923. * (命令 SETBIT)
  924. *
  925. * @param key key
  926. * @param offset 偏移量
  927. * @param v true / false
  928. */
  929. public boolean setBit(String key, long offset, boolean value) {
  930. return redisTemplate.opsForValue().setBit(key, offset, value);
  931. }
  932. /**
  933. * bitmap获取指定位置结果
  934. * (命令 GETBIT)
  935. *
  936. * @param key key
  937. * @param offset 偏移量
  938. * @return true / false
  939. */
  940. public boolean getBit(String key, long offset) {
  941. return redisTemplate.opsForValue().getBit(key, offset);
  942. }
  943. /**
  944. * bitmap 统计和,所有位置为1的总数量
  945. * (命令 BITCOUNT)
  946. *
  947. * @param key key
  948. */
  949. public long bitCount(String key) {
  950. return redisTemplate.execute((RedisCallback<Long>) con -> con.bitCount(key.getBytes()));
  951. }
  952. /**
  953. * bitField
  954. * (命令 BITFIELD)
  955. *
  956. * @param key key
  957. * @param bitFieldSubCommands demo: BitFieldSubCommands bitFieldSubCommands =BitFieldSubCommands.create().get(BitFieldSubCommands.BitFieldType.UINT_16).valueAt(0);
  958. */
  959. public List<Long> bitField(String key, BitFieldSubCommands bitFieldSubCommands) {
  960. return redisTemplate.opsForValue().bitField(key, bitFieldSubCommands);
  961. }
  962. }

测试

  1. @SpringBootTest
  2. class TestApplicationTests {
  3. @Autowired
  4. private RedisClient redisClient;
  5. @Test
  6. void contextLoads() {
  7. List<Student> students = new ArrayList<>();
  8. Student student1 = new Student("java", 22, "Chinese");
  9. Student student2 = new Student("go", 11, "USA");
  10. Student student3 = new Student("js", 20, "Japan");
  11. Student student4 = new Student("python", 16, "Thailand");
  12. Student student5 = new Student("php", 33, "Chinese");
  13. Student student6 = new Student("php", 16, "Chinese");
  14. Student student7 = new Student("java", 16, "Thailand");
  15. students.add(student1);
  16. students.add(student2);
  17. students.add(student3);
  18. students.add(student4);
  19. students.add(student5);
  20. students.add(student6);
  21. students.add(student7);
  22. redisClient.set("student", students);
  23. }
  24. }

结果显示

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号