网上看到一篇帖子,粘贴过来,以作查看,因为没有多余时间,没做测试,只供参考。如果你有时间的话,可以测试下。
application-redis.xml :利用spring的构造注入,把集群参数传入RedisInitBean中,并且在项目启动的时候加载RedisInitBean的有参构造方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="RedisInitBean" class="com.test.test.RedisInitBean" >
<!-- IP:Port -->
<constructor-arg index="0" type="List">
<list>
<value>127.0.0.1:6379</value>
<value>192.168.3.27:6380</value>
</list>
</constructor-arg>
<!-- maxWaitMillis -->
<constructor-arg index="1" type="long">
<value>1000</value>
</constructor-arg>
<!-- MaxIdle -->
<constructor-arg index="2" type="int">
<value>200</value>
</constructor-arg>
<!-- testOnBorrow -->
<constructor-arg index="3" type="Boolean">
<value>true</value>
</constructor-arg>
</bean>
</beans>
RedisInitBean.java
这里面要说一下,使用的是 分布式连接池 异步调用!
package com.test.test; import java.util.Arrays; import java.util.List; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.ShardedJedis; import redis.clients.jedis.ShardedJedisPool; public class RedisInitBean { private List Host; private long maxWaitMillis; private int MaxIdle; private Boolean testOnBorrow; private static List<JedisShardInfo> shards ; private static ShardedJedisPool pool; private static ShardedJedis jedis; public RedisInitBean(List host, long maxWaitMillis, int maxIdle, Boolean testOnBorrow) { super(); Host = host; this.maxWaitMillis = maxWaitMillis; MaxIdle = maxIdle; this.testOnBorrow = testOnBorrow; if(host.size()!=0){ for (int i = 0; i < host.size(); i++) { String h[] = ((String) host.get(i)).split(":"); shards = Arrays.asList(new JedisShardInfo(h[0].trim(),Integer.parseInt(h[1].trim()))); System.out.println(shards); } }else{ System.out.println("请检查Redis配置,host项为必填项!格式[IP:PORT]"); } pool = new ShardedJedisPool(new JedisPoolConfig(), shards); jedis = pool.getResource(); } public synchronized ShardedJedis getSingletonInstance(){ return jedis; } public synchronized static void returnResource(){ pool.returnResource(jedis); } public synchronized static void destroy(){ pool.destroy(); } }
java调用,注意粗体代码部分
package com.test.web;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPipeline;
import com.test.test.RedisInitBean;
@Controller
@RequestMapping("/TestRequest")
public class TestRequest {
@Autowired
private RedisInitBean rib;
@RequestMapping("/test")
public void test(){
ShardedJedis jedis = rib.getSingletonInstance();
ShardedJedisPipeline pipeline = jedis.pipelined();
long start = System.currentTimeMillis();
for (int i = 0; i < 99999; i++) {
pipeline.set("zhenbn" + i, "n" + i);
}
List<Object> results = pipeline.syncAndReturnAll();
long end = System.currentTimeMillis();
rib.returnResource();
rib.destroy();
System.out.println("分布式连接池异步调用耗时: " + ((end - start)/1000.0) + " 秒");
try {
Thread.sleep(5000);//睡5秒,然后打印jedis返回的结果
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("返回结果:"+results);
}
}