redis.clients.jedis.JedisSentinelPool Java Examples

The following examples show how to use redis.clients.jedis.JedisSentinelPool. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedisConfig.java    From springboot-learn with MIT License 6 votes vote down vote up
@Bean(name = "jedis.pool")
@Autowired
public JedisSentinelPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
                                   @Value("${spring.redis.sentinel.master}") String clusterName,
                                   @Value("${spring.redis.sentinel.nodes}") String sentinelNodes,
                                   @Value("${spring.redis.timeout}") int timeout,
                                   @Value("${spring.redis.password}") String password) {
    logger.info("缓存服务器的主服务名称:" + clusterName + ", 主从服务ip&port:" + sentinelNodes);
    Assert.isTrue(StringUtils.isNotEmpty(clusterName), "主服务名称配置为空");
    Assert.isTrue(StringUtils.isNotEmpty(sentinelNodes), "主从服务地址配置为空");

    Set<String> sentinels = Sets.newHashSet(StringUtils.split(sentinelNodes, ","));

    JedisSentinelPool sentinelJedisPool = new JedisSentinelPool(clusterName, sentinels, config, Protocol.DEFAULT_TIMEOUT, password);

    return sentinelJedisPool;
}
 
Example #2
Source File: RedisSentinelTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void testSentinel() {
    JedisSentinelPool sentinelPool = ClientBuilder.redisSentinel(appId)
            .setConnectionTimeout(2000)
            .setSoTimeout(1000)
            .build();
    HostAndPort currentHostMaster = sentinelPool.getCurrentHostMaster();
    logger.info("current master: {}", currentHostMaster.toString());

    Jedis jedis = sentinelPool.getResource();
    for (int i = 0; i < 10; i++) {
        jedis.lpush("mylist", "list-" + i);
    }
    jedis.close();
    sentinelPool.destroy();
}
 
Example #3
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example #4
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testClosingTracedJedisClosesUnderlyingJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis resource = pool.getResource();
  assertEquals(1, pool.getNumActive());

  resource.close();
  assertEquals(0, pool.getNumActive());
  assertEquals(1, pool.getNumIdle());

  // ensure that resource is reused
  Jedis nextResource = pool.getResource();
  assertEquals(1, pool.getNumActive());
  assertEquals(0, pool.getNumIdle());
  nextResource.close();

  pool.destroy();
}
 
Example #5
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void checkResourceIsCloseable() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
      "foobared", 2);

  Jedis jedis = pool.getResource();
  try {
    jedis.set("hello", "jedis");
  } finally {
    jedis.close();
  }

  Jedis jedis2 = pool.getResource();
  try {
    assertEquals(jedis, jedis2);
  } finally {
    jedis2.close();
  }
}
 
Example #6
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
@Test
public void customClientName() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
      "foobared", 0, "my_shiny_client_name");

  Jedis jedis = pool.getResource();

  try {
    assertEquals("my_shiny_client_name", jedis.clientGetname());
  } finally {
    jedis.close();
    pool.destroy();
  }

  assertTrue(pool.isClosed());
}
 
Example #7
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 6 votes vote down vote up
private void forceFailover(JedisSentinelPool pool) throws InterruptedException {
  HostAndPort oldMaster = pool.getCurrentHostMaster();

  // jedis connection should be master
  Jedis beforeFailoverJedis = pool.getResource();
  assertEquals("PONG", beforeFailoverJedis.ping());

  waitForFailover(pool, oldMaster);

  Jedis afterFailoverJedis = pool.getResource();
  assertEquals("PONG", afterFailoverJedis.ping());
  assertEquals("foobared", afterFailoverJedis.configGet("requirepass").get(1));
  assertEquals(2, afterFailoverJedis.getDB());

  // returning both connections to the pool should not throw
  beforeFailoverJedis.close();
  afterFailoverJedis.close();
}
 
Example #8
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
private void waitForFailover(JedisSentinelPool pool, HostAndPort oldMaster)
    throws InterruptedException {
  HostAndPort newMaster = JedisSentinelTestUtil.waitForNewPromotedMaster(MASTER_NAME,
    sentinelJedis1, sentinelJedis2);

  waitForJedisSentinelPoolRecognizeNewMaster(pool, newMaster);
}
 
Example #9
Source File: RedisDeployCenterImpl.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Override
public boolean addSentinel(long appId, String sentinelHost) {
    AppDesc appDesc = appDao.getAppDescById(appId);
    JedisSentinelPool jedisSentinelPool = redisCenter.getJedisSentinelPool(appDesc);
    if (jedisSentinelPool == null) {
        return false;
    }
    List<InstanceInfo> instanceInfos = instanceDao.getInstListByAppId(appId);
    String masterName = null;
    for (Iterator<InstanceInfo> i = instanceInfos.iterator(); i.hasNext(); ) {
        InstanceInfo instanceInfo = i.next();
        if (instanceInfo.getType() != ConstUtils.CACHE_REDIS_SENTINEL) {
            i.remove();
            continue;
        }
        if (masterName == null && StringUtils.isNotBlank(instanceInfo.getCmd())) {
            masterName = instanceInfo.getCmd();
        }
    }
    Jedis jedis = null;
    String masterHost = null;
    Integer masterPort = null;
    try {
        jedis = jedisSentinelPool.getResource();
        masterHost = jedis.getClient().getHost();
        masterPort = jedis.getClient().getPort();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        jedis.close();
        jedisSentinelPool.destroy();
    }
    boolean isRun = runSentinel(appDesc, sentinelHost, masterName, masterHost, masterPort);
    if (!isRun) {
        return false;
    }
    return true;
}
 
Example #10
Source File: GenericRedisDAOImpl.java    From OpenIoE with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void init() {
   	CommunicationFacade communicationFacade = new CommunicationFacadeBean();
	Set<String> sentinels = communicationFacade.requestRedisSentinelsHosts();
   	version = (String) communicationFacade.requestNodeConfigs().get(CommunicationKeysConstants.KEY_VERSION);
	JedisPoolConfig poolConfig = new JedisPoolConfig();
       poolConfig.setMaxTotal(1);
       pool = new JedisSentinelPool(CryptoIntegrationConstants.REDIS_CLUSTER_MASTER_NAME, sentinels, poolConfig);
       jedis = pool.getResource();
   }
 
Example #11
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void returnResourceShouldResetState() {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();
  config.setMaxTotal(1);
  config.setBlockWhenExhausted(false);
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
      "foobared", 2);

  Jedis jedis = pool.getResource();
  Jedis jedis2 = null;

  try {
    jedis.set("hello", "jedis");
    Transaction t = jedis.multi();
    t.set("hello", "world");
    jedis.close();

    jedis2 = pool.getResource();

    assertTrue(jedis == jedis2);
    assertEquals("jedis", jedis2.get("hello"));
  } catch (JedisConnectionException e) {
    if (jedis2 != null) {
      jedis2 = null;
    }
  } finally {
    jedis2.close();

    pool.destroy();
  }
}
 
Example #12
Source File: RedisClient.java    From heisenberg with Apache License 2.0 5 votes vote down vote up
public RedisClient(Set<String> masterSet, Set<String> sentinelSet, String pwd) {
    logger.info("masterSet:" + masterSet + ",sentinelSet:" + sentinelSet + ",pwd:" + pwd);
    for (String masterName : masterSet) {
        JedisSentinelPool jsPool = new JedisSentinelPool(masterName, sentinelSet, getConfig(), 2000, pwd);
        hash.add(masterName, jsPool);
    }
}
 
Example #13
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void ensureSafeTwiceFailover() throws InterruptedException {
  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels,
      new GenericObjectPoolConfig(), 1000, "foobared", 2);

  forceFailover(pool);
  // after failover sentinel needs a bit of time to stabilize before a new
  // failover
  Thread.sleep(100);
  forceFailover(pool);

  // you can test failover as much as possible
}
 
Example #14
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
public void checkCloseableConnections() throws Exception {
  GenericObjectPoolConfig config = new GenericObjectPoolConfig();

  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
      "foobared", 2);
  Jedis jedis = pool.getResource();
  jedis.auth("foobared");
  jedis.set("foo", "bar");
  assertEquals("bar", jedis.get("foo"));
  jedis.close();
  pool.close();
  assertTrue(pool.isClosed());
}
 
Example #15
Source File: RedisSentinelSinkFunction.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Override
protected Jedis create(RedisProperties redisProperties) {
    Set<String> sentinelset = new HashSet<>(Arrays.asList(redisProperties.getSentinel().getSentinels().split(",")));
    this.pool
            = new JedisSentinelPool(redisProperties.getSentinel().getMaster(), sentinelset, redisProperties.getConfig(),
            redisProperties.getTimeout() == null ? Protocol.DEFAULT_TIMEOUT : redisProperties.getTimeout(),
            redisProperties.getPassword(), redisProperties.getDatabase());
    return this.pool.getResource();
}
 
Example #16
Source File: RedisSentinelTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test
    public void testSentinelExample() {
        JedisSentinelPool sentinelPool = null;

        // 使用默认配置
//        sentinelPool = ClientBuilder.redisSentinel(appId).build();

        /**
         * 自定义配置
         */
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(GenericObjectPoolConfig.DEFAULT_MAX_IDLE * 3);
        poolConfig.setMinIdle(GenericObjectPoolConfig.DEFAULT_MIN_IDLE * 2);
        poolConfig.setJmxEnabled(true);
        poolConfig.setMaxWaitMillis(3000);

        sentinelPool = ClientBuilder.redisSentinel(appId)
                .setPoolConfig(poolConfig)
                .setConnectionTimeout(2000)
                .setSoTimeout(1000)
                .build();

        Jedis jedis = sentinelPool.getResource();
        jedis.set("key1", "1");
        assertEquals("2", jedis.incr("key1"));
        jedis.close();
    }
 
Example #17
Source File: JedisSentinelPoolTest.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Test(expected = JedisConnectionException.class)
public void initializeWithNotAvailableSentinelsShouldThrowException() {
  Set<String> wrongSentinels = new HashSet<String>();
  wrongSentinels.add(new HostAndPort("localhost", 65432).toString());
  wrongSentinels.add(new HostAndPort("localhost", 65431).toString());

  JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
  pool.destroy();
}
 
Example #18
Source File: RedisSentinelClusterTest.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    Set<String> hosts = JedisUtil.sentinelHosts(cluster);
    jedisSentinelConfig = new FlinkJedisSentinelConfig.Builder().setMasterName(REDIS_MASTER)
        .setSentinels(hosts).build();
    jedisSentinelPool = new JedisSentinelPool(jedisSentinelConfig.getMasterName(),
        jedisSentinelConfig.getSentinels());
}
 
Example #19
Source File: RedisCommandsContainerBuilder.java    From bahir-flink with Apache License 2.0 5 votes vote down vote up
/**
 * Builds container for Redis Sentinel environment.
 *
 * @param jedisSentinelConfig configuration for JedisSentinel
 * @return container for Redis sentinel environment
 * @throws NullPointerException if jedisSentinelConfig is null
 */
public static RedisCommandsContainer build(FlinkJedisSentinelConfig jedisSentinelConfig) {
    Objects.requireNonNull(jedisSentinelConfig, "Redis sentinel config should not be Null");

    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMaxIdle(jedisSentinelConfig.getMaxIdle());
    genericObjectPoolConfig.setMaxTotal(jedisSentinelConfig.getMaxTotal());
    genericObjectPoolConfig.setMinIdle(jedisSentinelConfig.getMinIdle());

    JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(jedisSentinelConfig.getMasterName(),
        jedisSentinelConfig.getSentinels(), genericObjectPoolConfig,
        jedisSentinelConfig.getConnectionTimeout(), jedisSentinelConfig.getSoTimeout(),
        jedisSentinelConfig.getPassword(), jedisSentinelConfig.getDatabase());
    return new RedisContainer(jedisSentinelPool);
}
 
Example #20
Source File: RedisSentinelJedisProvider.java    From conductor with Apache License 2.0 5 votes vote down vote up
@Inject
public RedisSentinelJedisProvider(HostSupplier hostSupplier, DynomiteConfiguration configuration) {
    GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    genericObjectPoolConfig.setMinIdle(5);
    genericObjectPoolConfig.setMaxTotal(configuration.getMaxConnectionsPerHost());
    logger.info("Starting conductor server using redis_sentinel and cluster " + configuration.getClusterName());
    Set<String> sentinels = new HashSet<>();
    for (Host host : hostSupplier.getHosts()) {
        sentinels.add(host.getHostName() + ":" + host.getPort());
    }
    this.jedisSentinel = new JedisSentinel(new JedisSentinelPool(configuration.getClusterName(), sentinels, genericObjectPoolConfig));
}
 
Example #21
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example #22
Source File: TracingJedisSentinelPoolTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinelPoolReturnsTracedJedis() {
  JedisSentinelPool pool = new TracingJedisSentinelPool(
      new TracingConfiguration.Builder(mockTracer).build(), MASTER_NAME, sentinels,
      new GenericObjectPoolConfig());
  Jedis jedis = pool.getResource();
  assertEquals("OK", jedis.set("key", "value"));
  assertEquals("value", jedis.get("key"));

  jedis.close();
  pool.destroy();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example #23
Source File: RedisCacheTest.java    From jetcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testSentinel() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);

    Set<String> sentinels = new HashSet<>();
    sentinels.add("127.0.0.1:26379");
    sentinels.add("127.0.0.1:26380");
    sentinels.add("127.0.0.1:26381");
    JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, pc);

    testWithPool(pool);
}
 
Example #24
Source File: RedisSentinelCacheClient.java    From AsuraFramework with Apache License 2.0 5 votes vote down vote up
@Override
public JedisSentinelPool initPool() throws Exception {
    if (Check.isNullOrEmpty(getServers())) {
        throw new IllegalArgumentException("未指定redis服务器地址");
    }
    String[] hosts = getServers().split("\\|");
    Set<String> hostAndPorts = new HashSet<>();
    for (String host : hosts) {
        hostAndPorts.add(host);
    }
    JedisPoolConfig poolConfig = super.getPoolConfig();
    return new JedisSentinelPool(getMasterName(), hostAndPorts, poolConfig);
}
 
Example #25
Source File: RedisCacheProviderTest.java    From GraceKelly with Apache License 2.0 4 votes vote down vote up
private void setupJedis() {
  jedis = mock(Jedis.class);
  pool = mock(JedisSentinelPool.class);
  when(pool.getResource()).thenReturn(jedis);
}
 
Example #26
Source File: RedisContainer.java    From bahir-flink with Apache License 2.0 4 votes vote down vote up
/**
 * Use this constructor if Redis environment is clustered with sentinels.
 *
 * @param sentinelPool SentinelPool which actually manages Jedis instances
 */
public RedisContainer(final JedisSentinelPool sentinelPool) {
    Objects.requireNonNull(sentinelPool, "Jedis Sentinel Pool can not be null");
    this.jedisPool = null;
    this.jedisSentinelPool = sentinelPool;
}
 
Example #27
Source File: RedisCacheProvider.java    From GraceKelly with Apache License 2.0 4 votes vote down vote up
public RedisCacheProvider(JedisSentinelPool pool, ObjectMapper mapper) {
  this.pool = pool;
  this.mapper = mapper;
}
 
Example #28
Source File: JedisClientSentinel.java    From Raincat with GNU Lesser General Public License v3.0 4 votes vote down vote up
public JedisClientSentinel(final JedisSentinelPool jedisSentinelPool) {
    this.jedisSentinelPool = jedisSentinelPool;
}
 
Example #29
Source File: RedisSentinelManager.java    From shiro-redis with MIT License 4 votes vote down vote up
public void setJedisPool(JedisSentinelPool jedisPool) {
	this.jedisPool = jedisPool;
}
 
Example #30
Source File: RedisSentinelManager.java    From shiro-redis with MIT License 4 votes vote down vote up
public JedisSentinelPool getJedisPool() {
	return jedisPool;
}