redis集群 java dmeo

package cn.tvm.shaketv.search.test;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class RedisClusterTest {

    private static RedisClusterTest redisClusterComponent = new RedisClusterTest();

    private static final String HOST = "192.168.38.59";
    /**
         * redisCluster客户端
         */
        private JedisCluster redisCluster;
        /**
         * 客户端连接超时时间
         */
        private final static int TIME_OUT = 3000;

        /**
         * soket超时时间
         */
        private final static int SO_TIME_OUT = 3000;

        /**
         * 最大尝试次数
         */
        private final static int MAX_ATTEMP = 5;

        private RedisClusterTest() {
            try {
                // redis节点信息
                Set<HostAndPort> nodeList = new HashSet<HostAndPort>();
                nodeList.add(new HostAndPort(HOST, 7000));
                nodeList.add(new HostAndPort(HOST, 7001));
                nodeList.add(new HostAndPort(HOST, 7002));
                nodeList.add(new HostAndPort(HOST, 7003));
                nodeList.add(new HostAndPort(HOST, 7004));
                nodeList.add(new HostAndPort(HOST, 7005));
                //redisCluster = new JedisCluster(nodeList, getCommonPoolConfig()); 不需要密码
                redisCluster = new JedisCluster(nodeList, TIME_OUT, 
                SO_TIME_OUT, MAX_ATTEMP, "yao_ana#2015", getCommonPoolConfig());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        /**
         * pool配置
         * @return
         */
        public static GenericObjectPoolConfig getCommonPoolConfig() {
            GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
            poolConfig.setMaxTotal(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL * 10);
            poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 5);
            poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 2);
            // JedisPool.borrowObject最大等待时间
            poolConfig.setMaxWaitMillis(1000L);
            // 开启jmx
            poolConfig.setJmxEnabled(true);
            return poolConfig;
        }

        public static RedisClusterTest getInstance() {
            return redisClusterComponent;
        }

        public void destroy() {
            if (redisCluster != null) {
                try {
                    redisCluster.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public JedisCluster getRedisCluster() {
            return redisCluster;
        }

        public static void main(String[] args) {
            JedisCluster cluster = RedisClusterTest.getInstance().getRedisCluster();
            cluster.set("testkey","testvalue");
            System.out.println(cluster.get("testkey"));
        }
}

说明:如果需要有密码的方式访问,最好是使用最新的redis架包
例如maven最新的2.9.0版架包:

    <dependency>
         <groupId>redis.clients</groupId>
         <artifactId>jedis</artifactId>
         <version>2.9.0</version>
         <type>jar</type>
    </dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值