首先不得不服Spring这个宇宙无敌的开源框架,几乎整合了所有流行的其它框架,http://projects.spring.io/spring-data/从这上面看,当下流行的redis、solr、hadoop、mongoDB、couchBase... 全都收入囊中。对于redis整合而言,主要用到的是spring-data-redis

使用步骤:

一、pom添加依赖项

        <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>

其它Spring必备组件,比如Core,Beans之类,大家自行添加吧

观察一下:

jedis、jredis等常用java的redis client已经支持了,不知道以后会不会集成Redisson,spring-data-redis提供了一个非常有用的类:StringRedisTemplate

对于大多数缓存应用场景而言,字符串是最常用的缓存项,用StringRedisTemplate可以轻松应付。

二、spring配置

<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg index="0" value="10.6.1**.**5" />
<constructor-arg index="1" value="7031" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg index="0" value="10.6.1**.**6" />
<constructor-arg index="1" value="7031" />
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg index="0" value="10.6.1**.**1" />
<constructor-arg index="1" value="7031" />
</bean>
</set>
</property>
</bean> <bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg ref="redisSentinelConfiguration" />
</bean> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnFactory" />
</bean>

这里我们使用Sentinel模式来配置redis连接,从上篇学习知道,sentinel是一种高可用架构,个人推荐在生产环境中使用sentinel模式。

注:26-28行,经试验,如果修改了默认端口,这里必须明细指定hostName及port,否则运行后,无法正确读写缓存,参考下面的配置:

    <bean id="jedisConnFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="10.6.53.xxx"/>
<property name="port" value="8830"/>
<property name="usePool" value="false"/>
<constructor-arg ref="redisSentinelConfiguration"/>
</bean>

其中hostName为当前master的IP,port为redis-server的运行端口(非sentinel端口),此外还要设置usePool为false,由于sentinel可能会自行切换master节点,如果不清楚当前的master节点是哪台机器,可以用前面提到的命令./redis-cli -p <sentinal端口号> sentinel masters查看,或者用java代码输出,参考下面的代码:

1         ApplicationContext ctx = new FileSystemXmlApplicationContext("/opt/app/spring-redis.xml");
2 StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
3 for (RedisServer m : template.getConnectionFactory().getSentinelConnection().masters()) {
4 logger.debug(m);
5 }

另外<property name="usePool" value="false"/> 这里的value值建议设置成false,如果改成true,经实际测试,发现在有些环境下会报如下错误:

redis.clients.jedis.exceptions.JedisDataException: ERR unknown command 'SET'

其它注意事项:

配置文件中的sentinels属性的Set 中的节点,并非一定要在同一个master下,也可以是归属于多个master,即:如果这里配置了10个node信息,其中1-3归属于master1,剩下的4-10属于master2,这也是允许的。

这样调用时,通过StringRedisTemplate.getConnectionFactory().getSentinelConnection().masters()可以返回一个master的列表,然后代码中根据需要,向某一个需要的master写入缓存.

三、单元测试

@Test
public void testSpringRedis() {
ConfigurableApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring.xml"); StringRedisTemplate stringRedisTemplate = ctx.getBean("stringRedisTemplate", StringRedisTemplate.class); // String读写
stringRedisTemplate.delete("myStr");
stringRedisTemplate.opsForValue().set("myStr", "http://yjmyzz.cnblogs.com/");
System.out.println(stringRedisTemplate.opsForValue().get("myStr"));
System.out.println("---------------"); // List读写
stringRedisTemplate.delete("myList");
stringRedisTemplate.opsForList().rightPush("myList", "A");
stringRedisTemplate.opsForList().rightPush("myList", "B");
stringRedisTemplate.opsForList().leftPush("myList", "0");
List<String> listCache = stringRedisTemplate.opsForList().range(
"myList", 0, -1);
for (String s : listCache) {
System.out.println(s);
}
System.out.println("---------------"); // Set读写
stringRedisTemplate.delete("mySet");
stringRedisTemplate.opsForSet().add("mySet", "A");
stringRedisTemplate.opsForSet().add("mySet", "B");
stringRedisTemplate.opsForSet().add("mySet", "C");
Set<String> setCache = stringRedisTemplate.opsForSet().members(
"mySet");
for (String s : setCache) {
System.out.println(s);
}
System.out.println("---------------"); // Hash读写
stringRedisTemplate.delete("myHash");
stringRedisTemplate.opsForHash().put("myHash", "PEK", "北京");
stringRedisTemplate.opsForHash().put("myHash", "SHA", "上海虹桥");
stringRedisTemplate.opsForHash().put("myHash", "PVG", "浦东");
Map<Object, Object> hashCache = stringRedisTemplate.opsForHash()
.entries("myHash");
for (Map.Entry<Object, Object> entry : hashCache.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
} System.out.println("---------------"); } finally {
if (ctx != null && ctx.isActive()) {
ctx.close();
}
} }

运行一下,行云流水般的输出:

...

信息: Created JedisPool to master at 10.6.144.***:7030
http://yjmyzz.cnblogs.com/
---------------
0
A
B
---------------
C
B
A
---------------
SHA - 上海虹桥
PVG - 浦东
PEK - 北京
---------------

...

注意红色标出部分,从eclipse控制台的输出,还能看出当前的master是哪台服务器

这里再补充一点小技巧:如果想遍历所有master及slave可以参考以下代码

@Test
public void testGetAllMasterAndSlaves() {
ApplicationContext ctx = new FileSystemXmlApplicationContext("D:/spring-redis.xml");
StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
RedisSentinelConnection conn = template.getConnectionFactory().getSentinelConnection();
for (RedisServer m : conn.masters()) {
System.out.println("master => " + m);//打印master信息
Collection<RedisServer> slaves = conn.slaves(m);
//打印该master下的所有slave信息
for (RedisServer s : slaves) {
System.out.println("slaves of " + m + " => " + s);
}
System.out.println("--------------");
}
((FileSystemXmlApplicationContext) ctx).close();
}

输出类似下面的结果:

master => 172.20.16.191:6379
slaves of 172.20.16.191:6379 => 172.20.16.192:6379

注:这里输出的slaves列表,经实际测试,发现只是根据redis server端的配置呆板的返回slave node列表,不管这些node是死是活,换句话说,就算某个slave已经down掉,这里依然会返回。

三、POJO对象的缓存

Spring提供的StringRedisTemplate只能对String操作,大多数情况下已经够用,但如果真需要向redis中存放POJO对象也不难,我们可以参考StringRedisTemplate的源码,扩展出ObjectRedisTemplate

package org.springframework.data.redis.core;

import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer; public class ObjectRedisTemplate<T> extends RedisTemplate<String, T> { public ObjectRedisTemplate(RedisConnectionFactory connectionFactory,
Class<T> clazz) { RedisSerializer<T> objectSerializer = new Jackson2JsonRedisSerializer<T>(
clazz); RedisSerializer<String> objectKeySerializer = new Jackson2JsonRedisSerializer<String>(
String.class); setKeySerializer(objectKeySerializer);
setValueSerializer(objectSerializer);
setHashKeySerializer(objectSerializer);
setHashValueSerializer(objectSerializer); setConnectionFactory(connectionFactory);
afterPropertiesSet();
} protected RedisConnection preProcessConnection(RedisConnection connection,
boolean existingConnection) {
return new DefaultStringRedisConnection(connection);
}
}

然后就可以这样用了:

@Test
public void testSpringRedis() {
ConfigurableApplicationContext ctx = null;
try {
ctx = new ClassPathXmlApplicationContext("spring.xml"); JedisConnectionFactory connFactory = ctx.getBean(
"jedisConnFactory", JedisConnectionFactory.class); ObjectRedisTemplate<SampleBean> template = new ObjectRedisTemplate<SampleBean>(
connFactory, SampleBean.class); template.delete("myBean");
SampleBean bean = new SampleBean("菩提树下的杨过");
template.opsForValue().set("myBean", bean); System.out.println(template.opsForValue().get("myBean")); } finally {
if (ctx != null && ctx.isActive()) {
ctx.close();
}
}
}

其中SampleBean的定义如下:

package com.cnblogs.yjmyzz;

import java.io.Serializable;

public class SampleBean implements Serializable {

    private static final long serialVersionUID = -303232410998377570L;

    private String name;

    public SampleBean() {
} public SampleBean(String name) {
this.name = name;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String toString() {
return "name:" + name;
} }

注:由于不是标准的String类型,所以在redis控制台,用./redis-cli get myBean是看不到缓存内容的,只能得到nil的输出,不要误以为set没成功!通过代码是可以正常get到缓存值的。

另外关于POJO对象的缓存,还有二个注意事项:

a) POJO类必须要有默认的无参构造函数,否则反序列化时会报错

b) ObjectRedisTemplate<T>中的T不能是接口,比如 DomainModelA继承自接口 IModelA,使用ObjectRedisTemplate时,要写成ObjectRedisTemplate<DomainModelA>而不是ObjectRedisTemplate<IModelA>,否则反序列化时也会出错

转自:http://www.cnblogs.com/yjmyzz/p/integrate-redis-with-spring.html

Redis_Spring与Jedis的集成的更多相关文章

  1. jedis spring集成

    jedis spring集成把jedis的核心对象交给spring管理.jedis核心对象:配置文件.连接池配置对象.连接池.集成方式有两种:spring-data-redis:自己封装 前提:要有一 ...

  2. redis 学习笔记(5)-Spring与Jedis的集成

    首先不得不服Spring这个宇宙无敌的开源框架,几乎整合了所有流行的其它框架,http://projects.spring.io/spring-data/从这上面看,当下流行的redis.solr.h ...

  3. redis jedis使用

    jedis就是集成了redis的一些命令操作,封装了redis的java客户端.提供了连接池管理.一般不直接使用jedis,而是在其上再封装一层,作为业务的使用.如果用spring的话,可以看看spr ...

  4. 【Redis】使用Jedis操作Redis

    Jedis介绍 jedis就是集成了redis的一些命令操作,封装了redis的java客户端. Jedis使用 使用jedis需要引入jedis的jar包,下面提供了maven依赖 jedis.ja ...

  5. Spring Boot 2.X(六):Spring Boot 集成Redis

    Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...

  6. Mybatis集成ehcache

    Mybatis集成ehcache 1.为什么需要缓存 拉高程序的性能 2. 什么样的数据需要缓存 很少被修改或根本不改的数据 业务场景比如:耗时较高的统计分析sql.电话账单查询sql等 3. ehc ...

  7. Spring系列之Redis的两种集成方式

    在工作中,我们用到分布式缓存的时候,第一选择就是Redis,今天介绍一下SpringBoot如何集成Redis的,分别使用Jedis和Spring-data-redis两种方式. 一.使用Jedis方 ...

  8. Redis 5种数据结构使用及注意事项

    1优缺点 非常非常的快,有测评说比Memcached还快(当大家都是单CPU的时候),而且是无短板的快,读写都一般的快,所有API都差不多快,也没有MySQL Cluster.MongoDB那样更新同 ...

  9. java Cache框架

    Cache框架乱炖   各类开源的缓存解决方案 JBossCache/TreeCacheJBossCache是一个复制的事务处理缓存,它允许你缓存企业级应用数据来更好的改善性能.缓存数据被自动复制,让 ...

随机推荐

  1. C#在excel中添加超链接

    1.新建一个项目 2.给项目添加引用:Microsoft Excel 12.0 Object Library (2007版本) using Excel = Microsoft.Office.Inter ...

  2. 湘潭邀请赛 Hamiltonian Path

    湘潭邀请赛的C题,哈密顿路径,边为有向且给定的所有边起点小于终点,怎么感觉是脑筋急转弯? 以后一定要牢记思维活跃一点,把复杂的事情尽量简单化而不是简单的事情复杂化. #include<cstdi ...

  3. Oracle优化 -- 关于Database Buffer Cache相关参数DB_CACHE_SIZE的优化设置

    select size_for_estimate, buffers_for_estimate ,ESTD_PHYSICAL_READ_factor,ESTD_PHYSICAL_READS from v ...

  4. SQL Server 2016将内置R语言?

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:随着大数据成为一个BuzzWord,和大数据相关的技术也变得越来越火热,其中就包括R语 ...

  5. 1080P、720P、4CIF、CIF所需要的理论带宽

    转自:http://blog.sina.com.cn/s/blog_64684bf30101hdl7.html 在视频监控系统中,对存储空间容量的大小需求是与画面质量的高低.及视频线路等都有很大关系. ...

  6. Convert XML to Object using LINQ

    Class and Xml : Please see my another article. http://www.cnblogs.com/mingmingruyuedlut/p/3436803.ht ...

  7. 在SharePoint2010中用out-of-box的方式自定制Application Pages(AccessDenied,Confirmation,Error,Login,RequestAccess,Signout,WebDeleted)

    在实际项目中需要对SharePoint2010中的AccessDenied页面进行自定制,于是乎上网搜索相关内容,经实际操作此方法可行,便以此文记录. 在SharePoint2010中,由于secur ...

  8. MATLAB中stem函数用法

    stem(Y) 将数据序列Y从x轴到数据值按照茎状形式画出,以圆圈终止.如果Y是一个矩阵,则将其每一列按照分隔方式画出. stem(X,Y)在X的指定点处画出数据序列Y.  stem(...,'fil ...

  9. Emacs 之列编辑模式

    // */ // ]]> Emacs 之 列编辑模式 Table of Contents 1. Emacs 下列编辑模式常用命令 2. 可以参考 1 Emacs 下列编辑模式常用命令 先mark ...

  10. 【转】Linux终端下 dstat 监控工具

    转自https://linux.cn/article-3215-1.html dstat 是一个可以取代vmstat,iostat,netstat和ifstat这些命令的多功能产品.dstat克服了这 ...