redis cluster应用连接(password)
application.properties
集群配置
application.properties
#各Redis节点信息
spring.redis.cluster.nodes=47.96.*.*:6370,47.96.*.*:6371,47.96.*.*:6372,116.62.*.*:6373,116.62.*.*:6374,116.62.*.*:6375
spring.redis.cluster.password=******
#执行命令超时时间
spring.redis.cluster.command-timeout= 10000
#重试次数
spring.redis.cluster.max-attempts=2
#跨集群执行命令时要遵循的最大重定向数量
spring.redis.cluster.max-redirects=3
#连接池最大连接数(使用负值表示没有限制)
spring.redis.cluster.max-active=16
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.cluster.max-wait=-1
#连接池中的最大空闲连接
spring.redis.cluster.max-idle=8
#连接池中的最小空闲连接
spring.redis.cluster.min-idle=0
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
spring.redis.cluster.test-on-borrow=true

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class RedisProperties { private String nodes; private String password; 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 String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} 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;
} @Override
public String toString() {
return "RedisProperties{" +
"nodes='" + nodes + '\'' +
", password='" + password + '\'' +
", commandTimeout=" + commandTimeout +
", maxAttempts=" + maxAttempts +
", maxRedirects=" + maxRedirects +
", maxActive=" + maxActive +
", maxWait=" + maxWait +
", maxIdle=" + maxIdle +
", minIdle=" + minIdle +
", testOnBorrow=" + testOnBorrow +
'}';
}
}
@Configuration
@ConditionalOnClass(JedisCluster.class)
public class RedisConfig {
Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class); @Resource
private RedisProperties redisProperties; /**
* 配置 Redis 连接池信息
*/
@Bean
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow()); return jedisPoolConfig;
} /*
*返回单例JedisCluster
*/
@Bean
public JedisCluster getJedisCluster(){
String[] serverArray = redisProperties.getNodes().split(",");
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
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,redisProperties.getPassword(),new GenericObjectPoolConfig());
} /**
* 设置数据存入redis 的序列化方式
* redisTemplate序列化默认使用的jdkSerializeable
* 存储二进制字节码,导致key会出现乱码,所以自定义序列化类
*/
@Bean
public 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();
logger.info("redis cluster集群连接成功");
return redisTemplate;
}
}
@Component
public class RedisUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class); @Autowired
private JedisCluster jedisCluster; /**
* 设置缓存
* @param key 缓存key
* @param value 缓存value
*/
public void set(String key, String value) {
jedisCluster.set(key, value);
LOGGER.debug("RedisUtil:set cache key={},value={}", 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);
LOGGER.debug("RedisUtil:setWithExpireTime cache key={},value={},expireTime={}", key, value, expireTime);
} /**
* 获取指定key的缓存
* @param key
*/
public String get(String key) {
String value = jedisCluster.get(key);
LOGGER.debug("RedisUtil:get cache key={},value={}",key, value);
return value;
} /**
* 删除指定key的缓存
* @param key
*/
public void delete(String key) {
jedisCluster.del(key);
LOGGER.debug("RedisUtil:delete cache key={}", key);
} }
单机版
spring.redis.database=0
spring.redis.host=116.62.*.*
spring.redis.password=********
spring.redis.port=6379
spring.redis.jedis.pool.max-active=4
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=2
spring.redis.jedis.pool.min-idle=0
#spring.redis.default.expire.time=60
spring.redis.timeout=10000
spring.redis.clientname=knowsecret

@Configuration
@Order(value = )
public class StartService implements ApplicationRunner {
private static Logger logger = LoggerFactory.getLogger(StartService.class); @Autowired
private ICheckSecret iCheckSecret;
@Override
public void run(ApplicationArguments args) throws Exception {
logger.info("project start secret init");
String uuid = UUID.randomUUID().toString();
//0代表永久有效
iCheckSecret.setSectetMes("secretvalue",uuid,);
}
}
@Configuration
@EnableCaching
public class RedisCacheConfiguration extends CachingConfigurerSupport {
Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class); @Value("${spring.redis.host}")
private String host; @Value("${spring.redis.port}")
private int port; @Value("${spring.redis.timeout}")
private int timeout; @Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle; @Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis; @Value("${spring.redis.password}")
private String password; @Value("${spring.redis.database}")
private int database; @Value("${spring.redis.clientname}")
private String clienttoken; @Bean
public JedisPool redisPoolFactory() {
logger.info("JedisPool注入成功!!");
logger.info("redis地址:" + host + ":" + port);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
// JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password,database,clienttoken);
return jedisPool;
}
}
redis cluster应用连接(password)的更多相关文章
- redis集群报Jedis does not support password protected Redis Cluster configurations异常解决办法
解决spring-data-redis操作redis集群报“Jedis does not support password protected Redis Cluster configurations ...
- Redis Cluster集群搭建后,客户端的连接研究(Spring/Jedis)(待实践)
说明:无论是否已经搭建好集群,还是使用什么样的客户端去连接,都是必须把全部IP列表集成进去,然后随机往其中一个IP写. 这样做的好处: 1.随机IP写入之后,Redis Cluster代理层会自动根据 ...
- python 连接 redis cluster 集群
一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...
- jedis处理redis cluster集群的密码问题
环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...
- Redis Cluster部署、管理和测试
背景: Redis 3.0之后支持了Cluster,大大增强了Redis水平扩展的能力.Redis Cluster是Redis官方的集群实现方案,在此之前已经有第三方Redis集群解决方案,如Twen ...
- Redis Cluster的搭建与部署,实现redis的分布式方案
前言 上篇Redis Sentinel安装与部署,实现redis的高可用实现了redis的高可用,针对的主要是master宕机的情况,我们发现所有节点的数据都是一样的,那么一旦数据量过大,redi也会 ...
- 超详细的 Redis Cluster 官方集群搭建指南
今天从 0 开始搭建 Redis Cluster 官方集群,解决搭建过程中遇到的问题,超详细. 安装ruby环境 因为官方提供的创建集群的工具是用ruby写的,需要ruby2.2.2+版本支持,rub ...
- 【精】搭建redis cluster集群,JedisCluster带密码访问【解决当中各种坑】!
转: [精]搭建redis cluster集群,JedisCluster带密码访问[解决当中各种坑]! 2017年05月09日 00:13:18 冉椿林博客 阅读数:18208 版权声明:本文为博主 ...
- redis cluster 实现
Redis cluster是一个redis官方提供的集群功能,集群节点最小3个节点,配置比较多,记录下来,以供下次使用.我在这使用的redis 4.0.6. 因为最新的ruby redis扩展需要ru ...
随机推荐
- Java - Thread 和 Runnable实现多线程
Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过jav ...
- js 日文全半角转换
客户的需求是,输入半角字符或日语假名,筛选出来的结果显示包含该字符的半角形式和全角形式的所有结果,输入全角也是同样的结果.这里就需要将输入的字符全部转为半角和全角,再去匹配结果. 在网上搜了一圈之后, ...
- Maven 那些破事
deploy 只上传了pom 晚上输命令,打算打包上传到本地库里,然后去服务器上部署新版本 mvn clean package deploy 结果看着mvn的build过程只是上传了pom,去库服务器 ...
- 关于子元素的margin-top溢出和元素浮动对父元素高度影响解决方案
以下是个人学习笔记,仅供学习参考. 1.关于子元素的margin-top作用在无margin-top-border的父元素上导致子元素的margin-top溢出问题. 在给没有margin-top-b ...
- 敏捷团队的组织与管理--- MPD软件工作坊培训感想(下)
注:由麦思博(MSUP)主办的2013年亚太软件研发团队管理峰会(以下简称MPD大会)分别于6月15及6月22日在北京.上海举办,葡萄城的部分程序员参加了上海的会议,本文是参会的一些感受和心得. 今年 ...
- windows 10安装jdk8
1.下载jdk,选择jdk软件版本和对应windows 32/64位版本 jdk下载链接:https://www.oracle.com/technetwork/java/javase/download ...
- 类与接口(五)java多态、方法重写、隐藏
一.Java多态性 面向对象的三大特性:封装.继承.多态. 多态的类型,分为以下两种: 编译时多态: 指的是 方法重载.编译时多态是在编译时确定调用处选择那个重载方法,所以也叫 静态多态,算不上真正的 ...
- 美图DPOS以太坊教程(Docker版)
一.前言 最近,需要接触区块链项目的主链开发,在EOS.BTC.ethereum.超级账本这几种区块链技术当中,相互对比后,最终还是以go-ethereum为解决方案. 以ethereum为基准去找解 ...
- spring-bean 版本的问题(报错:org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 75;)
当XML中配置的xsd是4.0,而引用的包是4以下的spring-bean.jar时,当服务器能连网时没问题,不能连网时,就报以下类似错误: org.xml.sax.SAXParseException ...
- 解决windows下vim中文乱码
解决windows下vim中文乱码 windows安装了vim8,也就是gvim后,打开带有中文的文档,显示中文是乱码. 毕竟有许多文档我是用utf-8编码的,所以解决的办法是设置一下编码为utf-8 ...