spring+redis 集群下的操作

摘要:
本文将记录备份工作中使用的点和测试方法,这些方法将不断更新--Redis序列化策略。通常,键值采用字符串序列化策略,--˃˂-如果未指定序列化策略,StringRedisTemplate

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

配置文件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 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/redis.properties</value>
</list>
</property>
</bean>
<!-- xml方式配置cluster-->
<bean >
<constructor-arg value="${redis.host1}" />
<constructor-arg value="${redis.port1}" type="int" />
</bean>
<bean >
<constructor-arg value="${redis.host2}" />
<constructor-arg value="${redis.port2}" type="int" />
</bean>
<bean >
<constructor-arg value="${redis.host3}" />
<constructor-arg value="${redis.port3}" type="int" />
</bean>
<bean >
<constructor-arg value="${redis.host4}" />
<constructor-arg value="${redis.port4}" type="int" />
</bean>
<bean >
<constructor-arg value="${redis.host5}" />
<constructor-arg value="${redis.port5}" type="int" />
</bean>
<bean >
<constructor-arg value="${redis.host6}" />
<constructor-arg value="${redis.port6}" type="int" />
</bean>

<bean >
<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 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"
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 />

<!--<bean 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 />-->
<!--</property>-->
<!--<property name="hashKeySerializer">-->
<!--<bean />-->
<!--</property>-->
<!--</bean>-->

<bean 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);
}

}


免责声明:文章转载自《spring+redis 集群下的操作》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇logstash开机启动第86章、系统服务之TELEPHONY_SERVICE(从零开始学Android)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

交易所如何对接狗狗币(DOGE)钱包?这点不可忽视

一个大家非常熟悉的 meme 头像 doge 在一夜之间成为了全球热搜。在国内,「狗狗币一天暴涨逾 250%」登上了微博热搜第七位,在国外,「DOGE」或者「DOGECOIN」站上了多个地区的「推特趋势榜」。比特币似乎都没有如此高规格的待遇。   对于新入币圈的朋友们可能对狗狗币还不太熟悉。这个币是个很早期的山寨币,它基本上是比特币的翻版,但是在比特币的基...

spring注解(Component、依赖注入、生命周期、作用域)

1、注解 注解就是一个类,使用@加上注解名称,开发中可以使用注解取代配置文件 2、@Component 取代<bean  class="">,@Component 取代<bean id="" class=""> (1)创建一个类(该类与dao层无联系,是一个单独的类) @Component("studentService") pub...

【决策树】— C4.5算法建立决策树JAVA练习

以下程序是我练习写的,不一定正确也没做存储优化。有问题请留言交流。转载请挂连接。 当前的属性为:age income student credit_rating 当前的数据集为(最后一列是TARGET_VALUE): --------------------------------- youth     high   no   fair      no y...

【Java】String字符串格式化

一、前言 String.format() 作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,为了不止步于简单调用 String.format("Hello %s", "John");,下面将笔记整理并记录下来。 其实各个语言的字符串格式化方法都是相通的,你可以在其中找到你熟悉的语言的影子,如C语言等。 二、重载方法 // 使用当前本地区域对象(Lo...

java property 配置文件管理工具框架,避免写入 property 乱序

property property 是 java 实现的 property 框架。 特点 优雅地进行属性文件的读取和更新 写入属性文件后属性不乱序 灵活定义编码信息 使用 OO 的方式操作 property 文件 支持多级对象引用 变更日志 ChangeLog 快速开始 环境依赖 Maven 3.x Jdk 1.7+ Maven 引入依赖 <de...

谈jdbcTemplate与mybatis

为什么会产生 Hibernate Mybatis 这类的dao层框架 传统的jdbc 虽然执行速度很快,但是开发效率很低,随着面向对象开发的设计思想,在面向对象编程中 将对象 进行持久化,存入关系型的数据库时,由于关系型数据库的设计思想是数学思维,在持久化时,必须要对象拆分各个属性值,才可存入数据库;传统的jdbc 持久化时 对象持久化时 ,取出对象的一个...