本文是紧接着上文安装好单机版的redis和rediscluster的之后需要去验证是否安装成功,以及如何和spring集成到一起。
接着这上文,我们先简单的写一个测试类,测试一下单机版的redis是否已经可用
@test
public void testjedissingle() {
// 创建一个jedis的对象。
jedisjedis= newjedis("192.168.21.225", 6379);
// 调用jedis对象的方法,方法名称和redis的命令一致。
jedis.set("key1", "jedis test");
stringstring= jedis.get("key1");
system.out.println(string);
// 关闭jedis。每次用完之后都应该关闭jedis
jedis.close();
}
测试结果入下:
在实际的运用过程中,我们基本上不会这样每连接一次redis就去创建一个jedis对象,而是会在程序加载的时候直接创建一个连接池。测试方法如下
/**
* 使用连接池
*/
@test
public void testjedispool() {
// 创建jedis连接池
jedispoolpool= newjedispool("192.168.21.225", 6379);
// 从连接池中获得jedis对象
jedisjedis= pool.getresource();
stringstring= jedis.get("key1");
system.out.println(string);
// 关闭jedis对象
jedis.close();
pool.close();
}
运行结果入下:
测试完单机版之后,我们接下来看看如何使用jedis操作redis集群(本文使用的是伪集群)
public void testjediscluster() {
hashset<hostandport>nodes= newhashset<>();
nodes.add(new hostandport("192.168.21.225",7001));
nodes.add(new hostandport("192.168.21.225",7002));
nodes.add(new hostandport("192.168.21.225",7003));
nodes.add(new hostandport("192.168.21.225",7004));
nodes.add(new hostandport("192.168.21.225",7005));
nodes.add(new hostandport("192.168.21.225",7006));
jedisclustercluster= newjediscluster(nodes);
cluster.set("key1", "1000");
stringstring= cluster.get("key1");
cluster.close();
通过上面的测试一方面我们知道了如何简单的去操作redis,另一方面也验证在之前介绍的redis的按照是没有问题的。
下面我们看看在spring的配置文件中如何配置redis的参数和jedis相关的东西。
首先我们看看redis连接池的配置
<!-- 连接池配置 -->
<bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
<!-- 最大连接数 -->
<property name="maxtotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxidle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numtestsperevictionrun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timebetweenevictionrunsmillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minevictableidletimemillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softminevictableidletimemillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxwaitmillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testonborrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testwhileidle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockwhenexhausted" value="false" />
</bean>
<bean id="redisclient" class="redis.clients.jedis.jedispool">
<constructor-arg name="host" value="192.168.21.225"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="poolconfig" ref="jedispoolconfig"></constructor-arg>
<bean id="jedisclient" class="com.taotao.rest.dao.impl.jedisclientsingle" />
<bean id="redisclient" class="redis.clients.jedis.jediscluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.hostandport">
<constructor-arg name="host" value="192.168.21.225"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean>
<constructor-arg name="port" value="7002"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</set>
</constructor-arg>
<bean id="jedisclientcluster"class="com.taotao.rest.dao.impl.jedisclientcluster"></bean>
为了方便笔者直接调用jedis去操作redis,所以笔者写了一个jedisclient工具类,如下
public interfacejedisclient {
stringget(string key);
stringset(string key,string value);
stringhget(string hkey,string key);
long hset(string hkey, string key, string value);
long incr(string key);
long expire(string key, int second);
long ttl(string key);
long del(string key);
long hdel(string hkey, string key);
}
由于对单机和集群的redis操作时不一样的,所以实现工具类的时候单机和集群的实现方式是不一样的。
public classjedisclientsingle implements jedisclient{
@autowired
private jedispool jedispool;
@override
public string get(string key) {
jedisjedis= jedispool.getresource();
stringstring= jedis.get(key);
return string;
public string set(string key, string value) {
stringstring= jedis.set(key, value);
public string hget(string hkey, string key) {
stringstring= jedis.hget(hkey, key);
public long hset(string hkey, string key, string value) {
longresult= jedis.hset(hkey, key, value);
return result;
public long incr(string key) {
longresult= jedis.incr(key);
public long expire(string key, int second) {
longresult= jedis.expire(key, second);
public long ttl(string key) {
longresult= jedis.ttl(key);
public long del(string key) {
jedis jedis = jedispool.getresource();
longresult= jedis.del(key);
public long hdel(string hkey, string key) {
longresult= jedis.hdel(hkey, key);
public classjedisclientcluster implements jedisclient {
private jediscluster jediscluster;
return jediscluster.get(key);
return jediscluster.set(key, value);
return jediscluster.hget(hkey, key);
return jediscluster.hset(hkey, key, value);
return jediscluster.incr(key);
return jediscluster.expire(key, second);
return jediscluster.ttl(key);
return jediscluster.del(key);
return jediscluster.hdel(hkey, key);
下面是测试配置以及工具类的方法
public void testspringjedissingle(){
applicationcontextapplicationcontext = newclasspathxmlapplicationcontext(
"classpath:spring/applicationcontext-*.xml");
jedispoolpool= (jedispool) applicationcontext.getbean("redisclient");
jedisjedis= pool.getresource();
pool.close();
public voidtestspringjediscluster() {
jedisclusterjediscluster= (jediscluster) applicationcontext
.getbean("redisclient");
stringstring= jediscluster.get("key1");
jediscluster.close();
至此,本文的的写作目的——测试集群,以及redis配置与spring集成就达到了。其实在笔者在写本文的时候,一方面是对之前项目中redis运用的优化,另一方面在实际的其实本文在实现jedisclient的时候,所有的方法中在笔者的项目中是添加有日志的。至于为什么添加日志本文就不介绍了。