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

配置文件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);
} }

												

spring+redis 集群下的操作的更多相关文章

  1. redis入门(14)redis集群下的数据分区存储

    redis入门(10)redis集群下的数据分区存储

  2. Redis集群下过期key监听

    1. 前言 在使用redis集群时,发现过期key始终监听不到.网上也没有现成的解决方案.于是想,既然不能监听集群,那我可以建立多个redis连接,分别对每个redis的key过期进行监听.以上做法可 ...

  3. 使用DBeaver Enterprise连接redis集群的一些操作记录

    要点总结: 使用DBeaver Enterprise连接redis集群可以通过SQL语句查看key对应的value,但是没法查看key. 使用RedisDesktopManager连接redis集群可 ...

  4. 序列化人人网框架下的DAO?也就是在Spring下序列化DAO的问题(spring+quartz集群下)

    人人网框架地址:http://code.google.com/p/paoding-rose/ 问题发生: 用Quartz作集群时用JobDataMap传递DAO,提示DAO未序列化,可框架的DAO为接 ...

  5. redis集群学习

    转载: http://arganzheng.life/redis-cluster.html Redis3.0版本加入了cluster功能,解决了Redis单点无法横向扩展的问题. 分布式系统要解决的不 ...

  6. redis集群搭建手册

    搭建集群需要用到安装后的redis单机版的bin目录,所以我们先搭建redis单机版 Redis单机版搭建: 因为需要安装redis源码包,所以我们需要gcc环境支持 : 使用FTP工具将压缩包上传至 ...

  7. 认识Redis集群——Redis Cluster

    前言 Redis集群分三种模式:主从模式.sentinel模式.Redis Cluster.之前没有好好的全面理解Redis集群,特别是Redis Cluster,以为这就是redis集群的英文表达啊 ...

  8. redis集群搭建+lua脚本的使用

    详细参考这篇文章(windows) https://blog.csdn.net/qiuyufeng/article/details/70474001 一.使用JAVA代码操作redis集群 publi ...

  9. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

随机推荐

  1. 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序。解决办法

    在64位服务器系统上,默认不支持Microsoft.Jet.OLEDB.4.0的驱动程序,系统默认会提示未在本地计算机上注册"Microsoft.Jet.OLEDB.4.0"的错误 ...

  2. 关于C++默认初始化的总结——开个坑

    关于C++初始化总结的博客,其实以前在我的独立博客上写过相关的内容,可惜呀,没有续费,腾讯回收了我的空间, 到现在,关于C++初始化的内容,一直是我的心头病,现在准备开个坑,慢慢的总结进来吧. 1.关 ...

  3. WEB – Architecture

  4. 修改pip更新源

    修改pip更新源 pip安装时默认访问pypi的,但是pypi的速度对于国内来说有点慢,还在国内也有一些pip的镜像源,造福广大程序员 pipy国内镜像目前有: http://pypi.douban. ...

  5. Cookie和Session的区别

    前言 HTTP是一种无状态的协议,为了分辨链接是谁发起的,就需要我们自己去解决这个问题.不然有些情况下即使是同一个网站我们每打开一个页面也都要登录一下.而Session和Cookie就是为解决这个问题 ...

  6. Ubuntu学习总结-09 安装 Pycharm

    一 下载 PyCharm 从以下网址下载Linux版本的Pycharm,这里使用的版本是pycharm-professional-2016.2.3.tar.gz . http://www.jetbra ...

  7. CSS-各种cs样式之浏览器兼容处理方式汇总大全(更新中...)

    页面模板 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 ...

  8. ubuntu竖屏显示

    xrandr -o left 向左旋转90度 xrandr -o right 向右旋转90度 xrandr -o inverted 上下翻转 xrandr -o normal 回到正常角

  9. 移动端页面调试神器-browser-sync

    最近公司赶一个项目,是mobile端,之前没怎么做过移动端的开发,这个项目算是个小尝试. 在做项目的过程中,用到了一个神器--browser-sync,在这里分享给大家. 1.静态页面调试 作为前端, ...

  10. TCP学习之一:TCP网络编程概念

    参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 一.TCP 面向连接的(UDP是无连接的) 全双工,建立连接之后 ...