使用jedisCluster连接Redis集群(xml和代码配置)
1.JedisCluster简介
一种优雅的连接Redis集群的工具,配置连接池之后自动获取和释放连接.
2.代码配置方式
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(100);
jedisPoolConfig.setMaxIdle(30);
jedisPoolConfig.setMaxWaitMillis(200);
jedisPoolConfig.setTestOnBorrow(true);
pool = new JedisPool(jedisPoolConfig, "IP", 7000, 2000, 10000, null, Protocol.DEFAULT_DATABASE, null);
HostAndPort hostAndPort = new HostAndPort("IP", 7000);
HostAndPort hostAndPort1 = new HostAndPort("IP", 7001);
HostAndPort hostAndPort2 = new HostAndPort("IP", 7002);
HostAndPort hostAndPort3 = new HostAndPort("IP", 7003);
HostAndPort hostAndPort4 = new HostAndPort("IP", 7004);
HostAndPort hostAndPort5 = new HostAndPort("IP", 7005);
Set<HostAndPort> hostAndPortSet = new HashSet<>();
hostAndPortSet.add(hostAndPort);
hostAndPortSet.add(hostAndPort1);
hostAndPortSet.add(hostAndPort2);
hostAndPortSet.add(hostAndPort3);
hostAndPortSet.add(hostAndPort4);
hostAndPortSet.add(hostAndPort5);
JedisCluster jedis = new JedisCluster(hostAndPortSet,jedisPoolConfig);
jedis.set(k,v);
jedis.get(k);
3.XML配置方式
配置完成后可以自动注入,使用比较方便.
@Autowired
private JedisCluster jedis;
3.1 加载配置文件
<!--加载数据库文件,读取数据库相关信息,加载redis配置信息 -->
<context:property-placeholder location="classpath:frontier-trade-business/redis.properties" />
<import resource="classpath:frontier-trade-business/applicationContext-redis.xml" />
3.2 redis.properties
#JedisPoolConfig的参数
#最大连接数
redis.maxTotal=30
#最大空闲时间
redis.maxIdle=100
redis.maxWait=2000
redis.testOnBorrow=true
#主机和端口号
redis.host1=IP
redis.port1=7000
redis.host2=IP
redis.port2=7001
redis.host3=IP
redis.port3=7002
redis.host4=IP
redis.port4=7003
redis.host5=IP
redis.port5=7004
redis.host6=IP
redis.port6=7005
3.3 applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<!-- jedis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<bean id="jedis.shardInfo1" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host1}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port1}" type="int"/>
</bean>
<bean id="jedis.shardInfo2" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host2}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port2}" type="int"/>
</bean>
<bean id="jedis.shardInfo3" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host3}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port3}" type="int"/>
</bean>
<bean id="jedis.shardInfo4" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host4}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port4}" type="int"/>
</bean>
<bean id="jedis.shardInfo5" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host5}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port5}" type="int"/>
</bean>
<bean id="jedis.shardInfo6" class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="${redis.host6}" type="java.lang.String"/>
<constructor-arg index="1" value="${redis.port6}" type="int"/>
</bean>
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0">
<set>
<ref bean="jedis.shardInfo1"/>
<ref bean="jedis.shardInfo2"/>
<ref bean="jedis.shardInfo3"/>
<ref bean="jedis.shardInfo4"/>
<ref bean="jedis.shardInfo5"/>
<ref bean="jedis.shardInfo6"/>
</set>
</constructor-arg>
<constructor-arg index="1" ref="jedisPoolConfig"/>
</bean>
</beans>
网友评论