spring整合單機(jī)版redis
1 在applicationContext-service.xml配置文件中配置JedisPool
<!-- 配置redis客戶(hù)端單機(jī)版 -->
<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 使用
定義一個(gè)java接口和JedisClientSingle實(shí)現(xiàn)類(lèi)钾虐,實(shí)現(xiàn)類(lèi)中注入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);
// 時(shí)間
Long expire(String key, int second);
Long ttl(String key);
}
JedisClientSingle.java
實(shí)現(xiàn)類(lèi)
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配置文件中笙纤,添加對(duì) JedisClientSingle
的掃描奥喻。
<!-- 配置redis客戶(hù)端實(shí)現(xiàn)類(lèi)(注意redis單機(jī)版本和集群版不要共存) -->
<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 實(shí)現(xiàn)JedisClientCluster删豺,在該類(lèi)中注入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配置文件中,添加對(duì) JedisClientSingle
的掃描久窟。
<!-- 配置redis客戶(hù)端實(shí)現(xiàn)類(lèi)(注意redis單機(jī)版本和集群版不要共存) -->
<bean id="jedisClientCluster" class="com.taotao.rest.component.impl.JedisClientCluster" />
注意:采用這種方法配置時(shí)东抹,redis單機(jī)版和集群版不能共存蚂子。一般測(cè)試環(huán)境使用單機(jī)版,生成環(huán)境使用集群版缭黔。