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}