spring整合单机版redis
1 在applicationContext-service.xml配置文件中配置JedisPool
<!-- 配置redis客户端单机版 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="182.61.41.84"></constructor-arg>
<constructor-arg name="port" value ="6379"></constructor-arg>
</bean>
2 使用
定义一个java接口和JedisClientSingle实现类,实现类中注入jedisPool
JedisClient.java
public interface JedisClient {
String set(String key, String value);
String get(String key);
// hash操作
Long hset(String key, String item, String value);
String hget(String key, String item);
Long hdel(String key, String item);
// 自增
Long incr(String key);
Long decr(String key);
// 时间
Long expire(String key, int second);
Long ttl(String key);
}
JedisClientSingle.java
实现类
public class JedisClientSingle implements JedisClient {
@Autowired
public JedisPool jedisPool;
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
}
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
}
@Override
public Long hset(String key, String item, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, item, value);
jedis.close();
return result;
}
@Override
public String hget(String key, String item) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, item);
jedis.close();
return result;
}
@Override
public Long hdel(String key, String item) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(key, item);
jedis.close();
return result;
}
@Override
public Long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public Long decr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.decr(key);
jedis.close();
return result;
}
@Override
public Long expire(String key, int second) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, second);
jedis.close();
return result;
}
@Override
public Long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
return result;
}
}
3 在applicationContext-service.xml配置文件中,添加对 JedisClientSingle
的扫描。
<!-- 配置redis客户端实现类(注意redis单机版本和集群版不要共存) -->
<bean id ="jedisClientSingle" class="com.taotao.rest.component.impl.JedisClientSingle"></bean>
spring中整合 redis集群
1 在applicationContext-service.xml配置文件中配置JedisCluster。
<bean id= "jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7001"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7002"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7003"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7004"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7005"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="182.61.41.84"/>
<constructor-arg name="port" value="7006"/>
</bean>
</set>
</constructor-arg>
</bean>
2 实现JedisClientCluster,在该类中注入jedisCluster。
public class JedisClientCluster implements JedisClient {
@Autowired
public JedisCluster jedisCluster;
@Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
}
@Override
public String get(String key) {
return jedisCluster.get(key);
}
@Override
public Long hset(String key, String item, String value) {
return jedisCluster.hset(key, item, value);
}
@Override
public String hget(String key, String item) {
return jedisCluster.hget(key, item);
}
@Override
public Long hdel(String key, String item) {
return jedisCluster.hdel(key, item);
}
@Override
public Long incr(String key) {
return jedisCluster.incr(key);
}
@Override
public Long decr(String key) {
return jedisCluster.decr(key);
}
@Override
public Long expire(String key, int second) {
return jedisCluster.expire(key, second);
}
@Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
}
}
3 在applicationContext-service.xml配置文件中,添加对 JedisClientSingle
的扫描。
<!-- 配置redis客户端实现类(注意redis单机版本和集群版不要共存) -->
<bean id="jedisClientCluster" class="com.taotao.rest.component.impl.JedisClientCluster" />
注意:采用这种方法配置时,redis单机版和集群版不能共存。一般测试环境使用单机版,生成环境使用集群版。