jedis操作sentinel,采用连接池方式

遇到问题总结

1.首先server.conf和sentinel.con没有采取密码操作,所以redis默认采取了保护模式,导致没有权限连接,关闭保护模式即可
protected-model no

2.接下来是无法获得127.0.0.1 not getSource from the pool
因为的sentinel没有绑定对应master ip
package com;

import java.util.HashSet;
import java.util.Set;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;

public class SentinelTest {
    private Logger logger=LoggerFactory.getLogger(SentinelTest.class);
    private  JedisSentinelPool jSentinelPool;
    @Before
    public  void setUp(){
        String masterName="mymaster";
        //sentinel地址集合
        Setset=new HashSet();
        set.add("192.168.138.128:26380");
        set.add("192.168.138.128:26381");
        set.add("192.168.138.128:26382");
        GenericObjectPoolConfig gPoolConfig=new GenericObjectPoolConfig();
        gPoolConfig.setMaxIdle(10); 
        gPoolConfig.setMaxTotal(10); 
        gPoolConfig.setMaxWaitMillis(10);
        gPoolConfig.setJmxEnabled(true);
        jSentinelPool=new JedisSentinelPool(masterName,set,gPoolConfig);
    }
    @Test
    public void testWriet(){
        Jedis jedis=null;

        for(int i=0;i<10;i++){
            try {

                jedis=jSentinelPool.getResource();  

                    System.out.println(i);

                String userKey="user"+i;
                jedis.set(userKey, String.valueOf(i));
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                logger.error(e.getMessage(),e);
            }
            finally{
                if (jedis!=null) {
                    jedis.close();
                }
            }
        }
    }
}

你可能感兴趣的:(redis)