1. 在配置文件中添加以下
spring:redis:cluster:# 各 Redis 节点信息nodes: 密码# 执行命令超时时间command-timeout: 15000# 重试次数max-attempts: 5# 跨集群执行命令时要遵循的最大重定向数量max-redirects: 3# 连接池最大连接数(使用负值表示没有限制)max-active: 16# 连接池最大阻塞等待时间(使用负值表示没有限制)max-wait: -1# 连接池中的最大空闲连接max-idle: 8# 连接池中的最小空闲连接min-idle: 0# 是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个test-on-borrow: true
  1. 添加对应的Maven依赖
 <!--redis-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 通过java方式添加对应的Redis集群配置
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;/*** @Author:HappyGiraffe* @Description:Redis 配置类* @CreateDate:13:48 2018/7/4*/
@Configuration
@ConditionalOnClass(JedisCluster.class)
public class RedisConfig {@Resourceprivate RedisProperties redisProperties;/*** 配置 Redis 连接池信息*/@Beanpublic JedisPoolConfig getJedisPoolConfig() {JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());return jedisPoolConfig;}/*** 配置 Redis Cluster 信息*/@Beanpublic RedisClusterConfiguration getJedisCluster() {RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();redisClusterConfiguration.setMaxRedirects(redisProperties.getMaxRedirects());List<RedisNode> nodeList = new ArrayList<>();String[] cNodes = redisProperties.getNodes().split(",");//分割出集群节点for(String node : cNodes) {String[] hp = node.split(":");nodeList.add(new RedisNode(hp[0], Integer.parseInt(hp[1])));}redisClusterConfiguration.setClusterNodes(nodeList);return redisClusterConfiguration;}/*** 配置 Redis 连接工厂*/@Beanpublic JedisConnectionFactory getJedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig) {JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);return jedisConnectionFactory;}/*** 设置数据存入redis 的序列化方式*  redisTemplate序列化默认使用的jdkSerializeable*  存储二进制字节码,导致key会出现乱码,所以自定义序列化类*/@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(objectMapper);redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}}

添加第二个配置

/*** @Author:HappyGiraffe* @Description:Redis 基本环境信息* @CreateDate:13:48 2018/7/4*/
@Component
//
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisProperties {private String nodes;private Integer commandTimeout;private Integer maxAttempts;private Integer maxRedirects;private Integer maxActive;private Integer maxWait;private Integer maxIdle;private Integer minIdle;private boolean testOnBorrow;public String getNodes() {return nodes;}public void setNodes(String nodes) {this.nodes = nodes;}public Integer getCommandTimeout() {return commandTimeout;}public void setCommandTimeout(Integer commandTimeout) {this.commandTimeout = commandTimeout;}public Integer getMaxAttempts() {return maxAttempts;}public void setMaxAttempts(Integer maxAttempts) {this.maxAttempts = maxAttempts;}public Integer getMaxRedirects() {return maxRedirects;}public void setMaxRedirects(Integer maxRedirects) {this.maxRedirects = maxRedirects;}public Integer getMaxActive() {return maxActive;}public void setMaxActive(Integer maxActive) {this.maxActive = maxActive;}public Integer getMaxWait() {return maxWait;}public void setMaxWait(Integer maxWait) {this.maxWait = maxWait;}public Integer getMaxIdle() {return maxIdle;}public void setMaxIdle(Integer maxIdle) {this.maxIdle = maxIdle;}public Integer getMinIdle() {return minIdle;}public void setMinIdle(Integer minIdle) {this.minIdle = minIdle;}public boolean isTestOnBorrow() {return testOnBorrow;}public void setTestOnBorrow(boolean testOnBorrow) {this.testOnBorrow = testOnBorrow;}
}

SpringCloud实现Redis集群配置相关推荐

  1. Redis集群配置和常见异常解决

    Redis集群配置和常见异常解决 参考文章: (1)Redis集群配置和常见异常解决 (2)https://www.cnblogs.com/hzb462606/p/11121281.html 备忘一下 ...

  2. Docker高级篇-Docker容器内Redis集群配置

    Docker容器内Redis集群配置 1.三主三从集成配置 1.1.关闭防火墙.启动docker服务 1.2.新建6个docker容器实例 1.3.进入容器redis-node-1,构建主从关系 1. ...

  3. Redis 集群配置教程

    Linux环境Redis 集群配置教程 下载 一.确认单节点的配置已完成. 1.确认已安装Redis 2.Redis的安装包解压后的目录必须存在,不能丢. 3.修改Redis的环境变量,并source ...

  4. springBoot整合redis单节点、redis哨兵、redis集群配置及redisClient区别

    springBoot整合redis单节点.redis哨兵.redis集群配置,redisClient jedis lettuce 区别? 1.springboot 整合redis单机模式: sprin ...

  5. springBoot整合redis集群配置

    最近发现这篇博客阅读量比较大,今天特意抽空创建了一个可运行的开源项目. 项目的代码和之前的博客内容相比,做了些优化,请大家参考项目源码. 开源项目源码: springboot-redis-cluste ...

  6. 【Redis】Redis集群配置

    参考链接 Redis集群配置

  7. spring 使用redis集群配置

    上面两篇介绍了redis集群的配置合一些基本的概念,所以接下来当然是要在项目中使用咯,redis的java支持已经做的非常好了,所以我们来试着使用这些api来进行redis的操作,首先我们需要操作re ...

  8. redis 集群配置(centos)

    1.安装wget yum -y install wget 2.下载redis安装包(版本3.0以上) wget http://download.redis.io/releases/redis-3.0. ...

  9. windows版本下的 redis 集群配置

    windows下的redis配置 https://www.cnblogs.com/thirteen-zxh/p/9187875.html ( 集群后篇) https://www.cnblogs.com ...

最新文章

  1. 科技公司开始重视AI伦理,他们都是怎么做的?
  2. JavaWeb 错误/异常时页面提示
  3. java 微信证书文件_JAVA微信企业付款如何使用证书、证书调用实例
  4. 【转】WPF入门教程系列六——布局介绍与Canvas(一)
  5. 程序员如何成为编程高手并以此创业
  6. 大型企业网络配置系列课程详解(六) --PPP链路的配置与相关概念的理解
  7. php fork demo,php多进程demo
  8. JAVA代码翻译更新(第五篇)
  9. 全国省份及其对应的城市字典
  10. 软件工程网络15个人作业3——案例分析(201521123107)
  11. ASEMI快恢复二极管RS1M、US1M和US1G能相互代换吗
  12. 2021年全球圆锥破碎机收入大约1357.4百万美元,预计2028年达到1665.6百万美元
  13. 私房小菜菜谱和煲汤大全汇总
  14. 最近公共祖先(LCA,Tarjan)
  15. 人心本无染,心静自然清 ——赞“落梅”
  16. Doxygen——根据代码注释生成文档的工具
  17. CC2640之OAD固件升级及合并方法
  18. springboot+vue+nodejs学生平时综合成绩管理系统java_o8mkp
  19. java对象内存布局中的基本类型字段排列顺序
  20. 互联网基因充分发酵后,移卡讲出了一个什么样的产品故事?

热门文章

  1. ★不容错过的PPT教程!
  2. 数据分析如何有效驱动产品迭代
  3. 无人机测绘整体流程-外业
  4. MDA165-16美高森美同款ASEMI原装品质
  5. 2022-2027年中国飞机租赁行业市场深度分析及投资战略规划研究报告
  6. Python实现K临近法(KNN)回归(村里最笨的小鸡都可以学会)阅读预计20分钟
  7. qlu_新生赛_2019
  8. js 删除HTML标签指定的属性
  9. Java和c++的区别!
  10. 类中的向上转型与向下转型详解