说明:Spring Boot简化了Spring Data Redis的引入,只要引入spring-boot-starter-data-redis之后会自动下载相应的Spring Data Redis和Jedis客户端,可以减少版本这块的冲突,当然,如果要引入别的版本也是可以的。版本控制全部交由Parent引入的Spring Boot节点进行管理!,建议不要引入最新版本的spring-boot-starter-data-redis,避免造成其它冲突。

有个假设:如果在使用Spring/Spring MVC项目时引入的Spring Data Redis和Jedis客户端时如果存在版本问题,出现莫名奇怪的问题,那么可以使用Spring Boot每个版本对应使用的Spring Data Redis和Jedis。

Spring Boot下面使用Spring Data Redis相当的简单,只需要引入Spring Data Redis和在配置文件application.properties中配置地址即可,因为它有spring-boot-autoconfigure来实现了自动注入。

下面是实际项目例子:

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.springboottest</groupId>
<artifactId>springboottest1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>springboottest1</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency> <!-- Add typical dependencies for a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- 热部署模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
</dependency> <!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies> <!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

application.properties:

# REDIS(RedisProperties)
# (普通集群,不使用则不用开启)在群集中执行命令时要遵循的最大重定向数目。
# spring.redis.cluster.max-redirects=
# (普通集群,不使用则不用开启)以逗号分隔的“主机:端口”对列表进行引导。
# spring.redis.cluster.nodes=
# 连接工厂使用的数据库索引。
spring.redis.database=0
# 连接URL,将覆盖主机,端口和密码(用户将被忽略),例如:redis://user:password@example.com:6379
spring.redis.url=
# Redis服务器主机。
spring.redis.host=localhost
# 登录redis服务器的密码。
spring.redis.password=
# 启用SSL支持。
spring.redis.ssl=false
# 池在给定时间可以分配的最大连接数。使用负值无限制。
spring.redis.pool.max-active=8
# 池中“空闲”连接的最大数量。使用负值表示无限数量的空闲连接。
spring.redis.pool.max-idle=8
# 连接分配在池被耗尽时抛出异常之前应该阻塞的最长时间量(以毫秒为单位)。使用负值可以无限期地阻止。
spring.redis.pool.max-wait=-1
# 目标为保持在池中的最小空闲连接数。这个设置只有在正面的情况下才有效果。
spring.redis.pool.min-idle=0
# Redis服务器端口。
spring.redis.port=6379
# (哨兵模式,不使用则不用开启)Redis服务器的名称。
# spring.redis.sentinel.master=
# (哨兵模式,不使用则不用开启)主机:端口对的逗号分隔列表。
# spring.redis.sentinel.nodes=
# 以毫秒为单位的连接超时。
spring.redis.timeout=0

说明:以上是单机版本的,如果是集群的只需要开启这两项:

# (普通集群,不使用则不用开启)在群集中执行命令时要遵循的最大重定向数目。
spring.redis.cluster.max-redirects=
# (普通集群,不使用则不用开启)以逗号分隔的“主机:端口”对列表进行引导。
spring.redis.cluster.nodes=127.0.0.1:1001,127.0.0.1:1002

注意:一旦开启了集群模式,那么基于单机的配置就会覆盖。

提示:可以这么说,上面的配置应该是最全的了。当然上面针对客户端的操作估计会比较少,比如哨兵模式,分片等等的,因为这些高可用在服务已经做了,如果想要在客户端实现这些,那么可以重新注入想要实现Bean即可。比如注入建立工厂,实现自己的Session。

使用:

package com.jsoft.springboottest.springboottest1.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Autowired
RedisTemplate redisTemplate; @Autowired
StringRedisTemplate stringRedisTemplate; @RequestMapping("/set")
public void set() {
redisTemplate.opsForValue().set("test", "4321");
} @RequestMapping("/show")
public String show(){ logger.info(redisTemplate.opsForValue().get("test").toString());
return "Hello World";
}
}

说明:只需要注入RedisTemplate即可。

使用技巧:

在市面上可能存在两种个用法,1中是针对opsForValue,另一种是execute的,那么这两种的使用区别如下:

1、在redistemplate中配置Serializer

ValueOperations<String, User> valueops = redisTemplate.opsForValue();
valueops.set(user.getId(), user);

2、不在redistemplate中配置Serializer,而是在Service的实现类中单独指定Serializer。

boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
RedisSerializer<String> redisSerializer = redisTemplate .getStringSerializer();
byte[] key = redisSerializer.serialize(user.getId());
byte[] value = redisSerializer.serialize(user.getName());
return redisConnection.setNX(key, value); } });
return result;
}

也就是说这两者的区别的序列化是自己实现的。

示例项目:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest6

参考:

https://docs.spring.io/spring-boot/docs/1.5.7.RELEASE/reference/htmlsingle/(官方文档,搜索spring.redis)

http://blog.csdn.net/i_vic/article/details/53081241

http://www.cnblogs.com/ityouknow/p/5748830.html

https://www.cnblogs.com/edwinchen/p/3816938.html

Spring Boot使用Spring Data Redis操作Redis(单机/集群)的更多相关文章

  1. 使用Spring Data Redis操作Redis(集群版)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  2. spring boot通过Spring Data Redis集成redis

    在spring boot中,默认集成的redis是Spring Data Redis,Spring Data Redis针对redis提供了非常方便的操作模版RedisTemplate idea中新建 ...

  3. 【spring boot】spring boot 基于redis pipeline 管道,批量操作redis命令

    spring boot 2.x 使用RedisTemplate 操作 =================================== 1.pom.xml <!--spring2.0集成r ...

  4. Spring Boot(十三):整合Redis哨兵,集群模式实践

    前面的两篇文章(Redis的持久化方案, 一文掌握Redis的三种集群方案)分别介绍了Redis的持久化与集群方案 -- 包括主从复制模式.哨兵模式.Cluster模式,其中主从复制模式由于不能自动做 ...

  5. Spring Boot集成Spring Data Reids和Spring Session实现Session共享

    首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...

  6. redis + 主从 + 持久化 + 分片 + 集群 + spring集成

    Redis是一个基于内存的数据库,其不仅读写速度快,每秒可以执行大约110000的写操作,81000的读取操作,而且其支持存储字符串,哈希结构,链表,集合丰富的数据类型.所以得到很多开发者的青睐.加之 ...

  7. Spring Boot 结合Spring Data结合小项目(增,删,查,模糊查询,分页,排序)

    本次做的小项目是类似于,公司发布招聘信息,因此有俩个表,一个公司表,一个招聘信息表,俩个表是一对多的关系 项目整体结构: Spring Boot和Spring Data结合的资源文件 applicat ...

  8. spring boot系列(五)spring boot 配置spring data jpa (查询方法)

    接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...

  9. Spring Boot 整合Spring Data JPA

    Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...

  10. Spring Boot中Spring data注解的使用

    文章目录 Spring Data Annotations @Transactional @NoRepositoryBean @Param @Id @Transient @CreatedBy, @Las ...

随机推荐

  1. Dockerfile 中的 CMD和ENTRYPOINT 两兄弟

    CMD 先说老大 CMD 当一个容器准备好运行之后,需要找一个指定命令来创建一个初始进程并运行. 一,/bin/sh -c 因为某种意义上一个Dockerfile其实可以理解是一个简化版bash 脚本 ...

  2. uva1615 Highway

    画图,每个给出点都有对应区间:先sort,再尽量靠右选:很常见的套路了..//注意不要越界(0,L) struct Q //复习结构{ double l,r; Q(double _l,double _ ...

  3. Open Cascade:如何从AIS_Shape导出TopoDS_Shape?

    Open Cascade:如何从AIS_Shape导出TopoDS_Shape? 实现代码如下: if( !myAISContext->HasOpenedContext()) { wxMessa ...

  4. JS第三方中间件的延伸

    js中间件 当我们在编写业务代码时候,我们无法避免有些业务逻辑复杂而导致业务代码写得又长又乱,如果再加上时间紧凑情况下写出来的代码估计会更让人抓狂.以至于我们一直在寻求更好的架构设计和更好的代码设计, ...

  5. 错误的语法:"create view必须是批处理中仅有的语句"

    编写脚本提示: 错误的语法:"create view必须是批处理中仅有的语句" FROM sys.views WHERE name = 'v_CS_UserRoleNames' ) ...

  6. Android UI: LinearLayout中layout_weight 属性的使用规则

    首先来查看android sdk文档,有这么一段话 LinearLayout also supports assigning a weight to individual children with ...

  7. svn搭建脚本

    1.yum install subversion 2.输入rpm -ql subversion查看安装位置 我们知道svn在bin目录下生成了几个二进制文件. 输入 svn --help可以查看svn ...

  8. webpack之source map

    先来一个webpack小例子,项目结构如下: // greeter.js module.exports = function() { var greet = document.createElemen ...

  9. js事件默认行为

    事件默认行为: 当一个事件发生的时候浏览器自己默认做的事情 怎么阻止? 当前这个行为是什么事件触发的,然后在这个事件的处理函数中使用 return false; 但是return false 阻止的是 ...

  10. Windows Server定时执行bat

    大家应该知道是在window服务器下使用bat批处理脚本文件,如果是Linux操作系统则是使用xshell脚本文件.由于自己是在做项目的时候对于文件系统中的日志进行定期删除对bat和xshell进行简 ...