相比网上的,新版本好像有些字段更改了名字,不知是否正确,欢迎指正
application.xml
<!-- spring data redis -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="usePool" value="true"></property>
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}" />
<property name="database" value="${redis.default.db}"></property>
<constructor-arg index="0" ref="jedisPoolConfig" />
</bean>
<!-- jedis pool配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<!--
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
-->
</bean>
<!-- Redis Template -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
相比网上搜索的结果,其中有两处更改,均在jedisPoolConfig
中
- maxActive更换成了maxTotal
- maxWait更换成了maxWaitMillis
redis.properties
#redis的服务器地址 redis.host=192.168.1.105 #redis的服务端口 redis.port=6379 #密码 redis.pass=1234xxxxx #链接数据库 redis.default.db=0 #客户端超时时间单位是毫秒 redis.timeout=100000 #最大连接数 redis.maxActive=300 #最大空闲数 redis.maxIdle=100 #最大建立连接等待时间 redis.maxWait=1000 #指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 #DBSync.testOnBorrow=true
package jedis; import java.util.ArrayList; import java.util.List; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; import redis.clients.util.Hashing; import redis.clients.util.Sharded; public class RedisShardPoolTest { static ShardedJedisPool pool; static { JedisPoolConfig config = new JedisPoolConfig();// Jedis池配置 config.setTestOnBorrow(true); String hostA = "192.168.1.55"; int portA = 6379; String hostB = "192.168.1.63"; int portB = 6379; List<JedisShardInfo> jdsInfoList = new ArrayList<JedisShardInfo>(2); JedisShardInfo infoA = new JedisShardInfo(hostA, portA); //infoA.setPassword("redis.360buy"); JedisShardInfo infoB = new JedisShardInfo(hostB, portB); //infoB.setPassword("redis.360buy"); jdsInfoList.add(infoA); jdsInfoList.add(infoB); pool = new ShardedJedisPool(config, jdsInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN); } /** * * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { String key = generateKey(); // key += "{aaa}"; ShardedJedis jds = null; try { jds = pool.getResource(); jds.getShard(key).getClient().select(8); System.out.println(key + ":" + jds.getShard(key).getClient().getHost()); System.out.println(jds.setex(key,100, "1111111111111111111111111111111")); } catch (Exception e) { e.printStackTrace(); } finally { pool.returnResource(jds); } } } private static int index = 1; public static String generateKey() { return String.valueOf(Thread.currentThread().getId()) + "_" + (index++); } }
http://my.oschina.net/u/1383439/blog/220871