/** * Tests opening an SSL/TLS connection to redis with a custom hostname * verifier. */ @Test public void connectWithShardInfoAndCustomHostnameVerifier() { final URI uri = URI.create("rediss://localhost:6390"); final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); final SSLParameters sslParameters = new SSLParameters(); HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); Jedis jedis = new Jedis(shardInfo); jedis.get("foo"); jedis.disconnect(); jedis.close(); }
/** * Tests opening an SSL/TLS connection to redis with a custom socket factory. */ @Test public void connectWithShardInfoAndCustomSocketFactory() throws Exception { final URI uri = URI.create("rediss://localhost:6390"); final SSLSocketFactory sslSocketFactory = createTrustStoreSslSocketFactory(); final SSLParameters sslParameters = new SSLParameters(); HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); Jedis jedis = new Jedis(shardInfo); jedis.get("foo"); jedis.disconnect(); jedis.close(); }
/** * Tests opening an SSL/TLS connection to redis with a custom hostname * verifier. This test should fail because "127.0.0.1" does not match the * certificate subject common name and there are no subject alternative names * in the certificate. */ @Test public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { final URI uri = URI.create("rediss://127.0.0.1:6390"); final SSLSocketFactory sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); final SSLParameters sslParameters = new SSLParameters(); HostnameVerifier hostnameVerifier = new BasicHostnameVerifier(); JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); Jedis jedis = new Jedis(shardInfo); try { jedis.get("foo"); Assert.fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { Assert.assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); } try { jedis.close(); } catch (Throwable e1) { // Expected. } }
@Test public void testMasterSlaveShardingConsistency() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT)); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1)); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2)); Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MURMUR_HASH); List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT)); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1)); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2)); Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards, Hashing.MURMUR_HASH); for (int i = 0; i < 1000; i++) { JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i)); JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i)); assertEquals(shards.indexOf(jedisShardInfo), otherShards.indexOf(jedisShardInfo2)); } }
@Test public void testMasterSlaveShardingConsistencyWithShardNaming() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(3); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT, "HOST1:1234")); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 1, "HOST2:1234")); shards.add(new JedisShardInfo("localhost", Protocol.DEFAULT_PORT + 2, "HOST3:1234")); Sharded<Jedis, JedisShardInfo> sharded = new Sharded<Jedis, JedisShardInfo>(shards, Hashing.MURMUR_HASH); List<JedisShardInfo> otherShards = new ArrayList<JedisShardInfo>(3); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234")); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234")); otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234")); Sharded<Jedis, JedisShardInfo> sharded2 = new Sharded<Jedis, JedisShardInfo>(otherShards, Hashing.MURMUR_HASH); for (int i = 0; i < 1000; i++) { JedisShardInfo jedisShardInfo = sharded.getShardInfo(Integer.toString(i)); JedisShardInfo jedisShardInfo2 = sharded2.getShardInfo(Integer.toString(i)); assertEquals(jedisShardInfo.getName(), jedisShardInfo2.getName()); } }
@Test public void checkCloseable() { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort())); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort())); shards.get(0).setPassword("foobared"); shards.get(1).setPassword("foobared"); ShardedJedis jedisShard = new ShardedJedis(shards); try { jedisShard.set("shard_closeable", "true"); } finally { jedisShard.close(); } for (Jedis jedis : jedisShard.getAllShards()) { assertTrue(!jedis.isConnected()); } }
@Bean(name="monitorJedisPool") public ShardedJedisPool getJedisPool() { try { List<JedisShardInfo> shardList = new ArrayList<>(); String hostStr = env.getProperty("spring.redis.shard.monitor.hostList"); int timeOut = Integer.parseInt(env.getProperty("spring.redis.shard.monitor.timeOut")); if(!StringUtils.isEmpty(hostStr)){ String[] hosts = hostStr.split(","); for(int i = 0; i < hosts.length; i++){ String[] temp = hosts[i].split(":"); JedisShardInfo info = new JedisShardInfo(temp[0], Integer.valueOf(temp[1]), timeOut); shardList.add(info); } } if(shardList.size() == 0){ //无法加载redis throw new IOException(); } return new ShardedJedisPool(getJedisPoolConfig(), shardList); } catch (Exception e) { throw new RuntimeException("无法加载资源文件!"); } }
@Override public void afterPropertiesSet() throws Exception { final JedisPoolConfig config = JedisPoolConfigFactory.createJedisPoolConfig(); final Set<JedisShardInfo> shardInfos = new HashSet<JedisShardInfo>(); final HostInfo[] hostInfos = HostInfoFactory.split(hosts); for (final HostInfo hostInfo : hostInfos) { shardInfos.add(hostInfo.createJedisShardInfo()); } if (redisService == null) { final ShardedJedisPool jedisPool = new ShardedJedisPool(config, new ArrayList<JedisShardInfo>(shardInfos)); redisService = new RedisServiceImpl(jedisPool); } final RedisServiceProxy redisServiceProxy = new RedisServiceProxy(); this.redisService = redisServiceProxy.bind(redisService); }
public JU_Puts() { // ���÷ֲ�ʽ��Ⱥ���� List<JedisShardInfo> v_JSIs = new ArrayList<JedisShardInfo>(); v_JSIs.add(new JedisShardInfo("192.168.105.105" ,6379)); // ��Ⱥ1�е��������� // v_JSIs.add(new JedisShardInfo("192.168.105.109" ,6379)); // ��Ⱥ2�е��������� // �������� for (JedisShardInfo v_Shard : v_JSIs) { v_Shard.setPassword("redis"); } redis = new Redis(v_JSIs); redis.setRunMode(RunMode.$Backup); }
/** * 初始化切片池 */ private void initialShardedPool() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(20); config.setMaxIdle(5); config.setMaxWait(1000l); config.setTestOnBorrow(false); // slave链接 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo("127.0.0.1", 6379, "master")); // 构造池 shardedJedisPool = new ShardedJedisPool(config, shards); }
@Before public void startUp() { shards = new ArrayList<JedisShardInfo>(); shards.add(new JedisShardInfo(redis1.getHost(), redis1.getPort())); shards.add(new JedisShardInfo(redis2.getHost(), redis2.getPort())); shards.get(0).setPassword("foobared"); shards.get(1).setPassword("foobared"); Jedis j = new Jedis(shards.get(0)); j.connect(); j.flushAll(); j.disconnect(); j = new Jedis(shards.get(1)); j.connect(); j.flushAll(); j.disconnect(); }
@Before public void setUp() throws Exception { Jedis jedis = new Jedis(redis1.getHost(), redis1.getPort()); jedis.auth("foobared"); jedis.flushAll(); jedis.disconnect(); jedis = new Jedis(redis2.getHost(), redis2.getPort()); jedis.auth("foobared"); jedis.flushAll(); jedis.disconnect(); JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(), redis1.getPort()); JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(), redis2.getPort()); shardInfo1.setPassword("foobared"); shardInfo2.setPassword("foobared"); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(shardInfo1); shards.add(shardInfo2); this.jedis = new ShardedJedis(shards); }
@Test public void testSyncWithNoCommandQueued() { JedisShardInfo shardInfo1 = new JedisShardInfo(redis1.getHost(), redis1.getPort()); JedisShardInfo shardInfo2 = new JedisShardInfo(redis2.getHost(), redis2.getPort()); shardInfo1.setPassword("foobared"); shardInfo2.setPassword("foobared"); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); shards.add(shardInfo1); shards.add(shardInfo2); ShardedJedis jedis2 = new ShardedJedis(shards); ShardedJedisPipeline pipeline = jedis2.pipelined(); pipeline.sync(); jedis2.close(); jedis2 = new ShardedJedis(shards); pipeline = jedis2.pipelined(); List<Object> resp = pipeline.syncAndReturnAll(); assertTrue(resp.isEmpty()); jedis2.close(); }
/** * * @return */ public static JedisManager createJedisManager() { Config conf = Config.newInstance(); // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(conf.getInt("redis.maxIdle")); config.setMinIdle(conf.getInt("redis.minIdle")); config.setMaxWaitMillis(conf.getLong("redis.maxWaitMillis")); config.setTestOnBorrow(conf.getBoolean("redis.testOnBorrow")); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); // slave链接 shards.add(new JedisShardInfo(conf.getString("redis.slave.host"), conf .getInt("redis.slave.port"), conf.getInt("redis.timeout"))); // master链接 shards.add(new JedisShardInfo(conf.getString("redis.master.host"), conf .getInt("redis.master.port"), conf.getInt("redis.timeout"))); // 构造池 ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shards); return new JedisManager(shardedJedisPool); }
/** * 根据配置创建 * * @return */ private static JedisManager createJedisManager() { // 池基本配置 JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(Conf.getInt("redis.maxIdle")); config.setMinIdle(Conf.getInt("redis.minIdle")); config.setMaxWaitMillis(Conf.getLong("redis.maxWaitMillis")); config.setTestOnBorrow(Conf.getBoolean("redis.testOnBorrow")); List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); // 链接 shards.add(new JedisShardInfo(Conf.getString("redis.host"), Conf.getInt("redis.port"), Conf .getInt("redis.timeout"))); // 构造池 ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config, shards); return new JedisManager(shardedJedisPool); }
public void testShardNormal() {// 13.619秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedis sharding = new ShardedJedis(shards); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { sharding.set("n" + i, "n" + i); System.out.println(i); } long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); try { Closeables.close(sharding, true); } catch (IOException e) { e.printStackTrace(); } }
public void testShardPipelined() {// 0.127秒 JedisShardInfo jedis = new JedisShardInfo("120.25.241.144", 6379); jedis.setPassword("b840fc02d52404542994"); List<JedisShardInfo> shards = Arrays.asList(jedis); ShardedJedis sharding = new ShardedJedis(shards); ShardedJedisPipeline pipeline = sharding.pipelined(); long start = System.currentTimeMillis(); for (int i = 0; i < 1000; i++) { pipeline.set("n" + i, "n" + i); System.out.println(i); } pipeline.syncAndReturnAll(); long end = System.currentTimeMillis(); System.out.println("共花费:" + (end - start) / 1000.0 + "秒"); sharding.disconnect(); try { Closeables.close(sharding, true); } catch (IOException e) { e.printStackTrace(); } }
/** * @param args */ public static void main(String[] args) { List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>(); JedisShardInfo shard = new JedisShardInfo("192.168.1.250", 6379); shard.setPassword("redisadmin"); shards.add( shard ); ShardRedisPlugin redis = new ShardRedisPlugin(shards); redis.start(); ShardCache cache = ShardRedis.userShard(); cache.set("zcq", "--BruceZCQ---"); cache.set("zcq1", "--BruceZCQ---"); Map<Object, Object> map = new HashMap<Object, Object>(); map.put("name", "朱丛启"); map.put("addr", "北京市"); cache.hmset("map1", map); System.out.println(cache.get("zcq1")); System.out.println(cache.hvals("map1")); }
/** * 创建一个"Redis服务器状态检测"定时任务对象。 * * @param jedisShards Jedis实现的Redis分片节点信息列表 * @param pingRetryTimes PING命令的失败重试次数 */ public JedisServerStateCheckTimerTask(List<JedisShardInfo> jedisShards, int pingRetryTimes){ AssertUtils.notEmpty(jedisShards, "'jedisShards' must not be null and empty"); logger.debug("Initial Shard List: {}", jedisShards); jedisShardSet = new HashSet<JedisShardInfo>(jedisShards.size()); jedisShardSet.addAll(jedisShards); this.pingRetryTimes = pingRetryTimes; activeShardMap = new ConcurrentHashMap<Jedis, JedisShardInfo>(jedisShards.size()); for (JedisShardInfo jedisShard : jedisShards) { Jedis jedis = jedisShard.createResource(); activeShardMap.put(jedis, jedisShard); } logger.debug("Initial active Shard map: {}", activeShardMap.values()); brokenShardMap = new ConcurrentHashMap<Jedis, JedisShardInfo>(4); activeShardListUpdated = new AtomicBoolean(false); }
@BeforeClass public void init() { // 设置后端Redis服务器信息 HostAndPort hostInfo = this.getHostInfo(); JedisShardInfo shardInfo = new JedisShardInfo(hostInfo.getHost(), hostInfo.getPort(), DEFAULT_TIMEOUT); this.connFactory = new JedisConnectionFactory(shardInfo); // 设置Redis客户端连接池信息 JedisPoolConfig poolConfig = new JedisPoolConfig(); poolConfig.setMaxTotal(10); poolConfig.setMinIdle(1); poolConfig.setTestOnBorrow(true); this.connFactory.setPoolConfig(poolConfig); // 设置连接超时时间 this.connFactory.setTimeout(DEFAULT_TIMEOUT); // 初始化连接工厂(必须手动触发,因为未基于IoC容器生命周期实现) this.connFactory.afterPropertiesSet(); // 初始化Redis模板 this.template = new RedisTemplate<String, String>(); this.template.setConnectionFactory(this.connFactory); this.template.afterPropertiesSet(); }
public RedisShardedPool(){ listServer = new ArrayList<JedisShardInfo>(); poolConfig = new RedisPoolConfig(); //控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取; //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。 poolConfig.setMaxTotal(50); //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。 poolConfig.setMaxIdle(10); //表示当borrow(引入)一个jedis实例时,最大的等待时间,如果超过等待时间,则直接抛出JedisConnectionException; poolConfig.setMaxWaitMillis(500); //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的; poolConfig.setTestOnBorrow(true); //在return给pool时,是否提前进行validate操作; poolConfig.setTestOnReturn(true); //borrowObject返回对象时,是采用DEFAULT_LIFO(last in first out,即类似cache的最频繁使用队列),如果为False,则表示FIFO队列; poolConfig.setLifo(true); }