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)的更多相关文章

  1. redis集群报Jedis does not support password protected Redis Cluster configurations异常解决办法

    解决spring-data-redis操作redis集群报“Jedis does not support password protected Redis Cluster configurations ...

  2. Redis Cluster集群搭建后,客户端的连接研究(Spring/Jedis)(待实践)

    说明:无论是否已经搭建好集群,还是使用什么样的客户端去连接,都是必须把全部IP列表集成进去,然后随机往其中一个IP写. 这样做的好处: 1.随机IP写入之后,Redis Cluster代理层会自动根据 ...

  3. python 连接 redis cluster 集群

    一. redis集群模式有多种, cluster模式只是其中的一种实现方式, 其原理请自行谷歌或者百度, 这里只举例如何使用Python操作 redis cluster 集群 二. python 连接 ...

  4. jedis处理redis cluster集群的密码问题

    环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...

  5. Redis Cluster部署、管理和测试

    背景: Redis 3.0之后支持了Cluster,大大增强了Redis水平扩展的能力.Redis Cluster是Redis官方的集群实现方案,在此之前已经有第三方Redis集群解决方案,如Twen ...

  6. Redis Cluster的搭建与部署,实现redis的分布式方案

    前言 上篇Redis Sentinel安装与部署,实现redis的高可用实现了redis的高可用,针对的主要是master宕机的情况,我们发现所有节点的数据都是一样的,那么一旦数据量过大,redi也会 ...

  7. 超详细的 Redis Cluster 官方集群搭建指南

    今天从 0 开始搭建 Redis Cluster 官方集群,解决搭建过程中遇到的问题,超详细. 安装ruby环境 因为官方提供的创建集群的工具是用ruby写的,需要ruby2.2.2+版本支持,rub ...

  8. 【精】搭建redis cluster集群,JedisCluster带密码访问【解决当中各种坑】!

    转: [精]搭建redis cluster集群,JedisCluster带密码访问[解决当中各种坑]! 2017年05月09日 00:13:18 冉椿林博客 阅读数:18208  版权声明:本文为博主 ...

  9. redis cluster 实现

    Redis cluster是一个redis官方提供的集群功能,集群节点最小3个节点,配置比较多,记录下来,以供下次使用.我在这使用的redis 4.0.6. 因为最新的ruby redis扩展需要ru ...

随机推荐

  1. NIO学习笔记六:channel 之前数据传输及scatter/gather

    在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel传输到另外一个channel. FileChannel的transferFrom()方法可以将 ...

  2. HDU1007(最近点对)

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. 【 js 算法类】这么全的数组去重,你怕不怕?

    以 var arr = [1,2,3,1]; 作为测试用例 方法一:双循环   (时间复杂度比较高,性能一般.) A.(1) function unique(arr) { var newArr = [ ...

  4. grunt搭建自动化的web前端开发环境(转)

    1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过.但是不会熟练使用grunt,那你就真的真的真的out了(三个“真的”重复,表示重点).至于grunt的作用,这里不详细说了, ...

  5. linux上,mysql使用聚合函数group by 时报错:SELECT list is not in GROUP BY clause and contains nonaggre的问题

    之前在windows上测试是可以正常使用的,但是上传到Linux上后,就报错: Expression # of SELECT list is not in GROUP BY clause and co ...

  6. python中matplotlib.pyplot中cm的属性

    https://matplotlib.org/gallery/color/colormap_reference.html

  7. Java基础笔记(1) 语言 JAVA的历史 Java的搭建环境

    本文除了搭建是重点,其他的都当阅读小说一样去看就好了,不想看可以直接抓住重点,我会改变颜色勾出重点! 英语是人与人交流沟通的重要方式之一.JAVA:是人与计算机沟通交流重要方式之一.我们除了用java ...

  8. Play framework 安装

    1.确定配置好了Java环境,在命令窗口输入java -version,得到版本号,则表示配好了Java环境 2.下载play文件进行安装http://www.playframework.org/,下 ...

  9. Expo大作战(二十四)--expo sdk api之Accelerometer

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  10. WebSocket实现web即时通信(后端nodejs实现)

    WebSocket实现web即时通信 一.首先看一下,HTTP.ajax轮询.long poll和WebSocket的区别: 1.HTTP 协议(短连接):一个 Request 一个 Response ...