一、JedisCluster方式(推荐)

jedisCluster专门用来连接redis集群

jedisCluster单例存在的

1、yml配置

spring:redis:password:clusterNodes: xxx.xx.xx:8001, xxx.xx.xx:8002expireSeconds: 120commandTimeout: 10000  #redis操作的超时时间pool:maxActive: 5000 #最大连接数maxIdle: 30 #最大空闲连接数minIdle: 5 #最小空闲连接数maxWait: 3000  #获取连接最大等待时间 ms  #default -1

2、新增类RedisProperties

package com.example.redis_cluster.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisProperties {private int expireSeconds;private String clusterNodes;private int commandTimeout;}

3、JedisCluster方式连接集群的配置

package com.example.redis_cluster.config;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.HostAndPort;import java.util.HashSet;
import java.util.Set;@Configuration
public class JedisClusterConfig {@Autowiredprivate RedisProperties redisProperties;@Beanpublic JedisCluster getJedisCluster() {String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)Set<HostAndPort> nodes = new HashSet<>();for (String ipPort : serverArray) {String[] ipPortPair = ipPort.split(":");nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));}return new JedisCluster(nodes,redisProperties.getCommandTimeout(),1000,1,new GenericObjectPoolConfig());}
}

4、工具类RedisUtil

package com.example.redis_cluster.config;import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;@Component
public class RedisUtil {@Autowiredprivate JedisCluster jedisCluster;/*** 设置缓存* @param key    缓存key* @param value  缓存value*/public void set(String key, String value) {jedisCluster.set(key, value);}/*** 设置缓存对象* @param key    缓存key* @param obj  缓存value*/public <T> void setObject(String key, T obj , int expireTime) {jedisCluster.setex(key, expireTime, JSON.toJSONString(obj));}/*** 获取指定key的缓存* @param key---JSON.parseObject(value, User.class);*/public String getObject(String key) {return jedisCluster.get(key);}/*** 判断当前key值 是否存在** @param key*/public boolean hasKey(String key) {return jedisCluster.exists(key);}/*** 设置缓存,并且自己指定过期时间* @param key* @param value* @param expireTime 过期时间*/public void setWithExpireTime( String key, String value, int expireTime) {jedisCluster.setex(key, expireTime, value);}/*** 获取指定key的缓存* @param key*/public String get(String key) {String value = jedisCluster.get(key);return value;}/*** 删除指定key的缓存* @param key*/public void delete(String key) {jedisCluster.del(key);}
}

5、工具类使用

二、使用jedis连接池

1、yml配置

spring:redis:database: 0jedis:pool:max-active: 8max-wait: -1max-idle: 8min-idle: 0timeout: 10000cluster:nodes: xxx.xx.xx:8001, xxx.xx.xx:8002command-timeout: 5000expire-seconds: 120

2、连接池注入配置信息

package com.example.redis_cluster.config;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Autowiredprivate RedisConnectionFactory factory;@Beanpublic RedisTemplate<String, Object> redisTemplate() {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());redisTemplate.setConnectionFactory(factory);return redisTemplate;}}

3、工具类使用

 @Resource(name="redisTemplate")private RedisTemplate<String,String> redisTemplate;@RequestMapping("/redisTest")public String redisTest(){return redisTemplate.opsForValue().get("name");}@RequestMapping("/getKey")public String findRedis(String key) {String value = redisTemplate.opsForValue().get(key);return value;}

springboot集成redis_cluster两种方式相关推荐

  1. 快速搭建Springboot项目的两种方式!!

    大家好,我是雄雄,欢迎关注微信公众号[雄雄的小课堂]. 前言 Springboot的特点就是简单.快速和方便,使用idea不到一分钟就可以快速搭建springboot项目,并且,在这里,你不用写spr ...

  2. SpringBoot2.1.5 (5)---快速构建SpringBoot 项目的两种方式

    快速构建SpringBoot项目的两种方发 一. 通过 http://start.spring.io/ 在线构建 二. 通过IntelliJ IDEA 快速构建 通过 http://start.spr ...

  3. kafka修改分区数_大数据技术:解析SparkStreaming和Kafka集成的两种方式

    Spark Streaming是基于微批处理的流式计算引擎,通常是利用Spark Core或者Spark Core与Spark Sql一起来处理数据.在企业实时处理架构中,通常将Spark Strea ...

  4. IDEA生成springboot项目的两种方式

    文章目录 1.第一种 1.1 file-->new-->project 1.2 选择spring Initializr--> next 1.3 填写项目名等 type详解 1.4 选 ...

  5. SpringBoot实现多数据源的两种方式

    前言 公司项目有连接多个不同数据库的需求,特研究了一下,根据网上的资料,造了一个基于AOP方式的数据源切换轮子,但继续探索,突然发现有开源的多数据源管理启动器.不过,本篇两种方式都会介绍. 基于dyn ...

  6. 继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错? springboot 两种方式稳定解决跨域问题

    继承WebMvcConfigurer 和 WebMvcConfigurerAdapter类依然CORS报错???springboot 两种方式稳定解决跨域问题! 之前我写了一篇文章,来解决CORS报错 ...

  7. 【SpringBoot】项目实现热部署的两种方式

    前言 spring boot : 2.0.0.RELEASE maven eclipse 另外还需清楚什么是热部署,以及为什么要热部署. SpringBoot项目中实现热部署的两种方式,使得部署变得异 ...

  8. SpringBoot配置绑定的两种方式

    SpringBoot配置绑定的两种方式 演示文件 bean public class Student {private String name;private Integer age;public S ...

  9. SpringBoot 配置文件加密的两种方式

    SpringBoot配置文件加密的两种方式 jasypt使用方式 用法一: 1.Application.java上增加注解@EnableEncryptableProperties(jasypt-spr ...

  10. springboot项目中利用@WebFilter注解和@Bean配置类两种方式实现Filter过滤器

    过滤器(Filter) 过滤器实际上就是对web资源进行拦截,做一些处理后再交给下一个过滤器或servlet处理.通常都是用来拦截request进行处理的,也可以对返回的response进行拦截处理 ...

最新文章

  1. 一个Git项目多个仓库
  2. 组原,汇编语言关于代码段的定义
  3. MySQL中的数据类型
  4. 怎样调整XenServer下面Linux虚拟机的磁盘大小
  5. 《剑指offer》-- 构建乘积数组、求1+2+3+...+n、不用加减乘除做加法、包含min函数的栈、用两个栈实现队列
  6. 《孩子,你如此优美:一位作家母亲的家教笔记》
  7. Bootstrap3 插件的原理
  8. js储存数据sessionStorage,localStorage
  9. Matlab重建信号实验总结,实验三信号采样与重建(实验报告).doc
  10. 【设计模式】解释器模式
  11. 进入Python的多彩世界
  12. oracle plsql破解
  13. 更新个祥硕ASM1153E开卡转接板的固件,详细教程
  14. ACCESS数据库基本使用
  15. SAP FICO顾问入门
  16. 《第6讲 非线性优化 》读书笔记
  17. 清华大学老师的一席话
  18. HTML5 视频直播(一)
  19. elementUI表格样式自定义修改
  20. 《麻省理工科技评论》在京举办第二届 EmTech China 峰会,全球顶级科技头脑打造年度最强话语场

热门文章

  1. Html加jq实现5星好评效果,关于jquery实现五星好评的方法
  2. python win32模块详解_Windows平台Python编程必会模块之pywin32介绍
  3. JS 打印 data数据_用D3.js 十分钟实现字符跳动效果
  4. nyoj461 Fiboncci数列(4)解通项公式
  5. nyoj107hdu A Famous ICPC Team
  6. 节点本地范围和链路本地范围_微服务链路追踪——skywalking
  7. 【CF1107G】Vasya and Maximum Profit(单调栈/单调栈+线段树最大子段和)
  8. 【linux程序设计】使用POSIX API的文件I/O操作方法仿写cp命令
  9. Java获取resin端口_线上解决Resin服务响应过慢的几个方法
  10. php面向对象代码_PHP 面向对象实现代码