Springboot2.x集成Redis集群模式

说明

Redis集群模式是Redis高可用方案的一种实现方式,通过集群模式可以实现Redis数据多处存储,以及自动的故障转移。如果想了解更多集群模式的相关知识介绍,欢迎往上爬楼。

准备条件

pom.xml中引入相关jar

  1. <!-- 集成Redis -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-redis</artifactId>
  5. </dependency>
  6. <!-- Jedis 客户端 -->
  7. <dependency>
  8. <groupId>redis.clients</groupId>
  9. <artifactId>jedis</artifactId>
  10. </dependency>
  11. <!-- lettuce客户端需要使用到 -->
  12. <dependency>
  13. <groupId>org.apache.commons</groupId>
  14. <artifactId>commons-pool2</artifactId>
  15. </dependency>

application.yml集群模式配置属性示例。

  1. spring:
  2. redis:
  3. host: 192.168.8.121
  4. port: 6379
  5. password: enjoyitlife
  6. timeout: 30000
  7. jedis:
  8. pool:
  9. max-active: 256
  10. max-wait: 30000
  11. max-idle: 64
  12. min-idle: 32
  13. lettuce:
  14. pool:
  15. max-active: 256
  16. max-idle: 64
  17. max-wait: 30000
  18. min-idle: 32
  19. cluster:
  20. nodes:
  21. - 192.168.8.121:7000
  22. - 192.168.8.121:7001
  23. - 192.168.8.121:7002
  24. - 192.168.8.121:7003
  25. - 192.168.8.121:7004
  26. - 192.168.8.121:7005
  27. - 192.168.8.121:7006
  28. - 192.168.8.121:7007

nodes节点读取。因为nodes是集合方式,所以spring中的@value$("xxx.xxx.xx")是无法读取的,本文提供了一种获取改节点属性的方式。

RedisClusterNodesCfg.java 获取nodes节点数据的代码示例。

  1. package top.enjoyitlife.redis;
  2. import java.util.List;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5. @Component
  6. @ConfigurationProperties(prefix = "spring.redis.cluster")
  7. public class RedisClusterNodesCfg {
  8. private List<String> nodes;
  9. public List<String> getNodes() {
  10. return nodes;
  11. }
  12. public void setNodes(List<String> nodes) {
  13. this.nodes = nodes;
  14. }
  15. }

集群模式下的整合教程

Jedis客户端整合

JedisClusterConfig.java 相关配置

  1. package top.enjoyitlife.redis.jedis;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import org.springframework.context.annotation.Profile;
  8. import org.springframework.data.redis.connection.RedisClusterConfiguration;
  9. import org.springframework.data.redis.connection.RedisConnectionFactory;
  10. import org.springframework.data.redis.connection.RedisNode;
  11. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.data.redis.serializer.StringRedisSerializer;
  14. import top.enjoyitlife.redis.RedisClusterNodesCfg;
  15. @Configuration
  16. @Profile("JedisCluster")
  17. public class JedisClusterConfig {
  18. @Autowired
  19. private RedisClusterNodesCfg redisClusterNodesCfg;
  20. @Bean
  21. public JedisConnectionFactory redisPoolFactory() throws Exception{
  22. RedisClusterConfiguration rcc=new RedisClusterConfiguration();
  23. List<String> nodesList=redisClusterNodesCfg.getNodes();
  24. String host=null;
  25. int port=0;
  26. for(String node:nodesList) {
  27. host=node.split(":")[0];
  28. port=Integer.valueOf(node.split(":")[1]);
  29. rcc.addClusterNode(new RedisNode(host,port));
  30. }
  31. return new JedisConnectionFactory(rcc);
  32. }
  33. @Bean
  34. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
  35. RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  36. template.setKeySerializer(new StringRedisSerializer());
  37. template.setValueSerializer(new StringRedisSerializer());
  38. template.setConnectionFactory(redisConnectionFactory);
  39. return template;
  40. }
  41. }

Lettuce客户端整合

  1. package top.enjoyitlife.redis.lettuce;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Profile;
  7. import org.springframework.data.redis.connection.RedisClusterConfiguration;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.data.redis.connection.RedisNode;
  10. import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
  11. import org.springframework.data.redis.core.RedisTemplate;
  12. import org.springframework.data.redis.serializer.StringRedisSerializer;
  13. import top.enjoyitlife.redis.RedisClusterNodesCfg;
  14. @Configuration
  15. @Profile("lettuceCluster")
  16. public class LettuceClusterConfig {
  17. @Autowired
  18. private RedisClusterNodesCfg redisClusterNodesCfg;
  19. @Bean
  20. public LettuceConnectionFactory redisConnectionFactory() {
  21. RedisClusterConfiguration rcc=new RedisClusterConfiguration();
  22. List<String> nodesList=redisClusterNodesCfg.getNodes();
  23. String host=null;
  24. int port=0;
  25. for(String node:nodesList) {
  26. host=node.split(":")[0];
  27. port=Integer.valueOf(node.split(":")[1]);
  28. rcc.addClusterNode(new RedisNode(host,port));
  29. }
  30. return new LettuceConnectionFactory(rcc);
  31. }
  32. @Bean
  33. public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
  34. RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
  35. template.setKeySerializer(new StringRedisSerializer());
  36. template.setValueSerializer(new StringRedisSerializer());
  37. template.setConnectionFactory(redisConnectionFactory);
  38. return template;
  39. }
  40. }

Springboot通过RedisClusterConfiguration来统一了连接集群的方式,区别Jedis客户端是通过JedisConnectionFactory进行初始化,而Lettuce客户端是通过LettuceConnectionFactory初始化。

单元测试

Jedis单元测试

  1. package top.enjoyitlife.redis.jedis;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.test.context.ActiveProfiles;
  7. @SpringBootTest
  8. @ActiveProfiles("JedisCluster")
  9. class JedisClusterTest {
  10. @Autowired
  11. private RedisTemplate<String, Object> redisTemplate;
  12. @Test
  13. void contextLoads() {
  14. String name=redisTemplate.opsForValue().get("name").toString();
  15. redisTemplate.opsForValue().set("hahha", "enjoyitlife2020");
  16. System.out.println(name);
  17. }
  18. }

Lettuce 单元测试

  1. package top.enjoyitlife.redis.lettuce;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.data.redis.core.RedisTemplate;
  6. import org.springframework.test.context.ActiveProfiles;
  7. @SpringBootTest
  8. @ActiveProfiles("lettuceCluster")
  9. class LettuceClusterTest {
  10. @Autowired
  11. private RedisTemplate<String, Object> redisTemplate;
  12. @Test
  13. void contextLoads() {
  14. String name = redisTemplate.opsForValue().get("name").toString();
  15. System.out.println(name);
  16. }
  17. }

好了以上就是Springboot2.x集成Redis集群模式的代码示例,希望对你能有所帮助。谢谢阅读。

Springboot2.x集成Redis集群模式的更多相关文章

  1. 7.redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?

    作者:中华石杉 面试题 redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗? 面试官心理分析 在前几年, ...

  2. 突破Java面试-Redis集群模式的原理

    1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...

  3. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  4. Redis集群模式配置

    redis集群部署安装: https://blog.csdn.net/huwh_/article/details/79242625 https://www.cnblogs.com/mafly/p/re ...

  5. Redis集群模式之分布式集群模式

    前言 Redis集群模式主要有2种: 主从集群 分布式集群. 前者主要是为了高可用或是读写分离,后者为了更好的存储数据,负载均衡. 本文主要讲解主从集群.本章主要讲解后一半部分,Redis集群. 与本 ...

  6. springmvc3.2集成redis集群

    老项目需要集成redis集群 因为spring版本才从2.x升级上来,再升级可能改动较大,且并非maven项目升级麻烦,故直接集成. jar包准备: jedis-2.9.0.jar  -- 据说只有这 ...

  7. AWS 创建redis 集群模式遇到的问题

    问题描述 前几天在aws 平台创建了Redis 集群模式,但是链接集群的时候发现无法连接,返回信息超时. 通过参数组创建redis的时候提示报错:Replication group with spec ...

  8. 5分钟实现用docker搭建Redis集群模式和哨兵模式

    如果让你为开发.测试环境分别搭一套哨兵和集群模式的redis,你最快需要多久,或许你需要一天?2小时?事实是可以更短. 是的,你已经猜到了,用docker部署,真的只需要十几分钟. 一.准备工作 拉取 ...

  9. Spring Boot集成Redis集群(Cluster模式)

    目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...

随机推荐

  1. SpringMVC @ModelAttribute详解

    被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...

  2. python修炼之路——控制语句

    Python编程之print python2.x和python3.x的print函数区别:python3.x的print函数需要加括号(),python2.x可以不加. #-*- coding:utf ...

  3. Apache Flink CEP 实战

    本文根据Apache Flink 实战&进阶篇系列直播课程整理而成,由哈啰出行大数据实时平台资深开发刘博分享.通过一些简单的实际例子,从概念原理,到如何使用,再到功能的扩展,希望能够给打算使用 ...

  4. underscore的使用

    1.链接 npm underscore:https://www.npmjs.com/package/underscore 官网:https://underscorejs.org/ 2.npm安装:np ...

  5. Linux培训教程 linux磁盘分区详解

    在学习 Linux 的过程中,安装 Linux 是每一个初学者的第一个门槛.在这个过程中间,最大的困惑莫过于给硬盘进行分区.虽然,现在各种发行版本的 Linux 已经提供了友好的图形交互界面,但是很多 ...

  6. BZOJ 3197: [Sdoi2013]assassin 树形DP + 最小费用流 + 树的同构

    Description Input Output 其实就是给出两颗树,求一种两种树同构的方式,使得不同颜色个数最少$.$树的重新构建,其实就是指定不同的点为根节点$.$ 好在树的重心有一个重要的性质: ...

  7. 拨号操作——android.intent.action.CALL

    button_14.setOnClickListener(new View.OnClickListener() {          @Override     public void onClick ...

  8. cvpr 2019 workshop&oral session

    1. Verification and Certification of Neural Networks神经网络的验证与认证 2. Automated Analysis of Marine Video ...

  9. bitmap相关工具类

    一,bitmap工具 封装了以下方法: 1,获取activity屏幕截图,保存为图片文件 2,从文件中获取截图,返回bitmap对象 package com.ctbri.weather.utils; ...

  10. ScvQ的技术栈

    前端:spring mybatis hibernate struts2 SpringMVC echache quartz 后台:servlet jsp jdbc io html css javascr ...