spring+redis 集群下的操作

文章就是记录一下工作当中的用到的点,与测试方法以备用,会不断更新。

配置文件spring-redis.xml:

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/redis.properties</value>
</list>
</property>
</bean>
<!-- xml方式配置cluster-->
<bean id= "clusterRedisNodes1" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host1}" />
<constructor-arg value="${redis.port1}" type="int" />
</bean>
<bean id= "clusterRedisNodes2" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host2}" />
<constructor-arg value="${redis.port2}" type="int" />
</bean>
<bean id= "clusterRedisNodes3" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host3}" />
<constructor-arg value="${redis.port3}" type="int" />
</bean>
<bean id= "clusterRedisNodes4" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host4}" />
<constructor-arg value="${redis.port4}" type="int" />
</bean>
<bean id= "clusterRedisNodes5" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host5}" />
<constructor-arg value="${redis.port5}" type="int" />
</bean>
<bean id= "clusterRedisNodes6" class="org.springframework.data.redis.connection.RedisNode" >
<constructor-arg value="${redis.host6}" />
<constructor-arg value="${redis.port6}" type="int" />
</bean>

<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration" >
<property name="maxRedirects" value="${spring.redis.cluster.max-redirects}" >
</property>
<property name="clusterNodes" >
<set>
<ref bean="clusterRedisNodes1" />
<ref bean="clusterRedisNodes2" />
<ref bean="clusterRedisNodes3" />
<ref bean="clusterRedisNodes4" />
<ref bean="clusterRedisNodes5" />
<ref bean="clusterRedisNodes6" />
</set>
</property>
</bean>
<bean id= "poolConfig" class ="redis.clients.jedis.JedisPoolConfig">
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean >
<!-- 集群 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:usePool="true">
<constructor-arg index="0" ref="redisClusterConfiguration" />
<constructor-arg index="1" ref="poolConfig"></constructor-arg>
</bean >
<!-- 单机 -->
<!--<bean id="jedisConnectionFactory"-->
<!--class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"-->
<!--p:hostName="192.168.1.42" p:port="6379" p:password="gxdgroup" p:database="0"-->
<!--p:poolConfig-ref="poolConfig" p:usePool="true"/>-->
<!-- Spring Data Redis 设置 -->
<!-- redis 序列化策略 ,通常情况下key值采用String序列化策略, -->
<!-- 如果不指定序列化策略,StringRedisTemplate的key和value都将采用String序列化策略; -->
<bean id="jsonRedisSerializer" class="com.xdth.redis.JsonUtil.JsonRedisSeriaziler"/>

<!--<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">-->
<!--&lt;!&ndash;如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! &ndash;&gt;-->
<!--<property name="connectionFactory" ref="jedisConnectionFactory" />-->
<!--<property name="keySerializer" >-->
<!--<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />-->
<!--</property>-->
<!--<property name="hashKeySerializer">-->
<!--<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>-->
<!--</property>-->
<!--</bean>-->

<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean>
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.xdth.redis.*" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
</beans>

 


属性文件redis.properties:自己redis的ip与端口

redis.host1=127.0.0.1
redis.port1=7001

redis.host2=127.0.0.1
redis.port2=7002

redis.host3=127.0.0.1
redis.port3=7003

redis.host4=127.0.0.1
redis.port4=7004

redis.host5=127.0.0.1
redis.port5=7005

redis.host6=127.0.0.1
redis.port6=7006

redis.minIdle=1
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=10000
redis.testOnBorrow=true
spring.redis.cluster.max-redirects= 3

StringRedisTemplate (key与value的默认序列化是stringSerializer)继承自 RedisTemplate (JdkSerializationRedisSerializer默认的序列化机制)

操作具体

redis的string类型操作

 
  public static void testString(){
ApplicationContext ac = new ClassPathXmlApplicationContext("spring-redis.xml");
StringRedisTemplate template = (StringRedisTemplate) ac.getBean("stringRedisTemplate");
ValueOperations<String, String> stringOpe = template.opsForValue();
//插入 第三个参数为指定过期时间
stringOpe.set("mingren","01");
stringOpe.set("zuozhu","02",1);
stringOpe.set("xiaoying","03",1, TimeUnit.MINUTES);
//重新设置值追加
stringOpe.append("mingren","hero");
//获取值
String mingren01 = stringOpe.get("mingren");
System.out.println("mingren :"+mingren01);
//获取原来的值 并设置为新的值
String mingrenGetAndSet = stringOpe.getAndSet("mingren", "01heros");
System.out.println("mingrenGetAndSet :"+mingrenGetAndSet);
String mingren02 = stringOpe.get("mingren");
System.out.println("mingren :"+mingren02);
//截取value值得一部分 0- -1 位全部
String mingrenLL = stringOpe.get("mingren", 0, -1);
System.out.println("mingrenLL :"+mingrenLL);
//bit的使用 bit 的位图 第5位设置为1 其他的位置为0
Boolean bit = stringOpe.setBit("bit", 5, true);
System.out.println("bit :"+bit);
Boolean bit1 = stringOpe.getBit("bit", 5);
// BitSet java.util包下 位可以用来统计用户访问量 统计有多少位为1
BitSet bitSet= BitSet.valueOf(stringOpe.get("bit").getBytes());
System.out.println(" bitSet.cardinality() :"+bitSet.cardinality());

System.out.println("插入完成");

}

redis实现队列

队列实现

@Component("redisMQ")
public class RedisMQ implements IRedisMQ {
@Resource
private StringRedisTemplate stringRedisTemplate;

@Resource
private JsonRedisSeriaziler jsonRedisSerializer;

/**
* 压栈
*
* @param key
* @param value
* @return
*/
public Long push(String key, String value) {
return stringRedisTemplate.opsForList().leftPush(key, value);
}

/**
* 出栈
*
* @param key
* @return
*/
public String pop(String key) {
return stringRedisTemplate.opsForList().leftPop(key);
}

/**
* 入队
*
* @param key
* @param value
* @return
*/
public Long in(String key, String value) {
return stringRedisTemplate.opsForList().rightPush(key, value);
}

/**
* 从队列的头,插入对象
*/
public <T> Long pushFromHead(String key,T value){
return stringRedisTemplate.opsForList().leftPush(key, this.jsonRedisSerializer.seriazileAsString(value));
}

/**
* 从对尾,插入对象
* @param value
*/
public <T> Long pushFromTail(String key,T value){
return stringRedisTemplate.opsForList().rightPush(key, this.jsonRedisSerializer.seriazileAsString(value));
}

/**
* 出队
*
* @param key
* @return
*/
public String out(String key) {
return stringRedisTemplate.opsForList().leftPop(key);
}

public String out(String key,int timeout){
return stringRedisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS);
}

/**
* 出队,从对头出队
* noblocking
* @return null if no item in queue
*/
public <T> T removeFromHead(String key,Class<T> clazz) {
return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().leftPop(key).toString(),clazz);
}
/**
* 出队,从对头出队
* noblocking
* @return null if no item in queue
*/
public <T> T removeFromHead(String key, int timeout,Class<T> clazz) {
return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().leftPop(key, timeout, TimeUnit.SECONDS).toString(),clazz) ;
}

/**
* 出队,从队尾出队
* noblocking
* @return null if no item in queue
*/
public <T> T removeFromTail(String key,Class<T> clazz){
return this.jsonRedisSerializer.deserializeAsObject(stringRedisTemplate.opsForList().rightPop(key).toString(),clazz);
}
/**
* 栈/队列长
*
* @param key
* @return
*/
public Long length(String key) {
return stringRedisTemplate.opsForList().size(key);
}

/**
* 范围检索
*
* @param key
* @param start
* @param end
* @return
*/
public List<String> range(String key, int start, int end) {
return stringRedisTemplate.opsForList().range(key, start, end);
}

/**
* 移除
*
* @param key
* @param i
* @param value
*/
public void remove(String key, long i, String value) {
stringRedisTemplate.opsForList().remove(key, i, value);
}

/**
* 检索
*
* @param key
* @param index
* @return
*/
public String index(String key, long index) {
return (String) stringRedisTemplate.opsForList().index(key, index);
}

/**
* 置值
*
* @param key
* @param index
* @param value
*/
public void set(String key, long index, String value) {
stringRedisTemplate.opsForList().set(key, index, value);
}

/**
* 裁剪
*
* @param key
* @param start
* @param end
*/
public void trim(String key, long start, int end) {
stringRedisTemplate.opsForList().trim(key, start, end);
}

}



posted @ 2017-01-06 17:47  小杨羊  阅读(2658)  评论(0编辑  收藏  举报