redis sentinel connection pool (JedisSentinelPool) - related parameters are configured into spring

applicationContext-redis.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd ">
    
	<!-- jedis pool configuration-->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="10000" />
		<property name="maxIdle" value="100" />
		<property name="maxWaitMillis" value="1000" />
		<!-- When outputting the "link" resource to the caller, check whether it is valid, if it is invalid, remove it from the connection pool, and try to get it and continue to get it. Set to true, a hang can not be used -->
		<property name="testOnBorrow" value="true" />
		<!-- Whether to check the validity of the "link" object when "returning" a link to the connection pool. -->
		<property name="testOnReturn" value="true" />
	</bean>
	
	<bean id="jedisPool" class="redis.clients.jedis.JedisSentinelPool">
		<constructor-arg index="0" value="mymaster" />
		<constructor-arg index="1">
			<set>
				<value>172.19.59.50:26379</value><!--A sentry is configured-->
			</set>
		</constructor-arg>
		<constructor-arg index="2" ref="jedisPoolConfig" />
		<constructor-arg index="3" value="30000" />
		<constructor-arg index="4">
			<null />
		</constructor-arg>
		<constructor-arg index="5" value="0" />
	</bean>
</beans>

 

Test code:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

public class TestRedisPool {

	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext-redis.xml");
		JedisSentinelPool pool = (JedisSentinelPool) ac.getBean("jedisPool");
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			jedis.set("foo", "test");
			String redisStr = jedis.get("foo");
			System.out.println(redisStr);
		} catch (JedisException je) {
			throw je;
		} finally {
			if (jedis != null)
				pool.returnResource(jedis);
		}
	}
}

 

Output result:

Aug 22, 2017 11:19:55 am redis.clients.jedis.JedisSentinelPool initSentinels
信息: Trying to find master from available Sentinels...
Aug 22, 2017 11:19:55 am redis.clients.jedis.JedisSentinelPool initSentinels
信息: Redis master running at 172.19.59.50:6381, starting Sentinel listeners...
Aug 22, 2017 11:19:55 am redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 172.19.59.50:6381
11:19:55.782 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'jedisPool' to allow for resolving potential circular references
11:19:55.782 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'jedisPool'
11:19:55.783 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@6054cd1b]
11:19:55.783 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
11:19:55.784 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'jedisPool'
test

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326308629&siteId=291194637