Redis初始化
来源:互联网 发布:uber软件下载 编辑:程序博客网 时间:2024/05/29 17:55
import com.config.data.RedisNode;import com.utils.PropertiesUtils;import redis.clients.jedis.*;import com.cache.redis.connection.*;import java.util.*;public final class RedisManager { public static final RedisManager I = new RedisManager(); private final RedisConnectionFactory factory = new RedisConnectionFactory(); List<RedisNode> nodes; JedisClient jedisClient; public RedisManager(){ init(); } public void init() { /** * maxTotal:8, maxIdle:4, minIdle:1, lifo:true, fairness:false, maxWaitMillis:5000, minEvictableIdleTimeMillis:300000, softMinEvictableIdleTimeMillis:1800000, numTestsPerEvictionRun:3, testOnCreate:false, testOnBorrow:false, testOnReturn:false, testWhileIdle:false, timeBetweenEvictionRunsMillis:60000, blockWhenExhausted:true, jmxEnabled:false, jmxNamePrefix:pool, jmxNameBase:pool */ JedisPoolConfig config=new JedisPoolConfig(); config.setMaxTotal(Integer.valueOf(PropertiesUtils.getproperties("redis.pool.maxActive","10"))); config.setMaxIdle(Integer.valueOf(PropertiesUtils.getproperties("redis.pool.maxIdle","10"))); config.setMaxWaitMillis(Integer.valueOf(PropertiesUtils.getproperties("redis.pool.maxWait","10"))); config.setTestOnBorrow(Boolean.valueOf(PropertiesUtils.getproperties("redis.pool.testOnBorrow","10"))); config.setTestOnReturn(Boolean.valueOf(PropertiesUtils.getproperties("redis.pool.testOnReturn","10"))); nodes = new ArrayList<RedisNode>(); nodes.add(new RedisNode( PropertiesUtils.getproperties("redis.ip","192.168.1.53"), Integer.valueOf(PropertiesUtils.getproperties("redis.port","6379")) )); factory.setPassword(""); factory.setPoolConfig( config ); factory.setRedisServers( nodes ); factory.setCluster(false); factory.init(); jedisClient = new JedisClient(); jedisClient.redisConnectionFactory = factory; jedisClient.get("ok"); } public RedisClient getRedisClient(){ return jedisClient; } public RedisClient newRedisClient(){ JedisClient jedisClient1 = new JedisClient(); jedisClient1.redisConnectionFactory = factory; return jedisClient1; } public RedisConnectionFactory getFactory(){ return factory;} public List<RedisNode> getNodes(){ return nodes; }}
package com.config.data;public class RedisNode { public String host; public int port; public RedisNode() { } public RedisNode(String host, int port) { this.host = host; this.port = port; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public static RedisNode from(String config) { String[] array = config.split(":"); if (array.length == 2) { return new RedisNode(array[0], Integer.parseInt(array[1])); } else { return new RedisNode(array[0], Integer.parseInt(array[1])); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; RedisNode server = (RedisNode) o; if (port != server.port) return false; return host.equals(server.host); } @Override public int hashCode() { int result = host.hashCode(); result = 31 * result + port; return result; } @Override public String toString() { return "RedisServer{" + "host='" + host + '\'' + ", port=" + port + '}'; }}
package com.utils;import org.apache.commons.lang.StringUtils;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class PropertiesUtils { public static final Properties common = loadProperties("common.properties"); public static String getproperties(String key,String defaultValue) { String ret = common.getProperty(key); return StringUtils.isBlank(ret) ? defaultValue :ret; } private static Properties loadProperties(String resources) { // 使用InputStream得到一个资源文件 InputStream inputstream = PropertiesUtils.class.getClassLoader().getResourceAsStream(resources); // new 一个Properties Properties properties = new Properties(); try { // 加载配置文件 properties.load(inputstream); return properties; } catch (IOException e) { throw new RuntimeException(e); } finally { try { inputstream.close(); } catch (IOException e) { throw new RuntimeException(e); } } }}
common.properties
redis.pool.maxActive=1024redis.pool.maxIdle=200redis.pool.maxWait=1000redis.pool.testOnBorrow=trueredis.pool.testOnReturn=trueredis.ip=${redis_url}#redis.ip=127.0.0.1redis.port=${redis_port}
阅读全文
0 0
- Redis初始化
- redis的初始化
- redis 服务器初始化
- 深入redis内部--初始化服务器
- Redis初识:2.通过初始化脚本启动Redis
- redis源码分析(1)——初始化
- Redis源码解析:20sentinel(一)初始化、建链
- redis集群实现(一)集群架构与初始化
- redis集群实现(一)集群架构与初始化
- 初始化
- 初始化
- 初始化
- 初始化
- 初始化
- 初始化
- 初始化
- 初始化
- 初始化
- mysql 表分区 按天分区
- Siemens.NX.Nastran.12.0.Win64.&.Linux64 2DVD
- iOS-判断字符串为空你以为这么简单吗?
- 进程线程例子
- ZooKeeper的典型应用场景之分布式协调/通知。
- Redis初始化
- git commit -am 之后,撤销并保留修改
- 数据结构期末课堂总结
- poi读取Excel时日期为数字 的解决方法
- 正则笔记
- Idea工具的破解
- 数据结构实验之栈与队列一:进制转换
- CocoaPods获取与使用
- 渗透测试之本地文件包含漏洞