说明: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. Database coalesce

    coalesce 语法 注意:连接操作符“||”是一个值得注意的例外. 例如,空值加任何值都是空值,空值 乘任何值也都是空值,依此类推. 参数 expression 任何类型的表达式. n 表示可以指 ...

  2. codevs 3070 寻找somebody4(水题日常)

     时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold   题目描述 Description 有一天.....sb不见了,有个人要去找他..他发现sb在一个杨辉三角里.. ...

  3. Oracle数据库升级前必要的准备工作

    Oracle数据库升级向来是一门纷繁复杂的工程,DBA需要为产品数据库的升级耗费大量时间精力在准备工作上:因为其升级复杂度高,所以即便做了较为充分的准备仍可能在升级过程中遇到意想不到的问题,为了更高效 ...

  4. Hadoop伪集群部署

    环境准备 [root@jiagoushi ~]# yum -y install lrzsz 已加载插件:fastestmirror Repository 'saltstack-repo': Error ...

  5. Python matlab octave 矩阵运算基础

    基础总结,分别在三种软件下,计算 求逆矩阵 矩阵转置 等运算,比较异同 例子:正规方程法求多元线性回归的最优解 θ=(XTX)-1XTY octave: pwd()当前目录 ones() zeros( ...

  6. PHP03 移动互联网和PHP

    学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建   移动互联网 定义 移动互联网,就是 ...

  7. 苹果平台上的媒体流播放技术HLS

    近日在和朋友聊起媒体流的服务器端实时转码技术的时候,发现苹果的各种终端上的视频播放并未使用常见的基于UDP的RTSP/RTP,而强制使用了Http Live Stream技术,这里稍稍总结了如下. 苹 ...

  8. 洛谷 P1518 两只塔姆沃斯牛

    P1518 两只塔姆沃斯牛 The Tamworth Two 简单的模拟题,代码量不大. 他们走的路线取决于障碍物,可以把边界也看成障碍物,遇到就转,枚举次,因为100 * 100 * 4,只有4个可 ...

  9. 高逼格关闭Win10防火墙

    作为一个开发人员,你还需要进入这个界面来关闭防火墙么? 如果是,那么现在,我将为大家介绍一种高逼格的方式: 第一步: 打开Windows PowerShell(管理员) 第二步:查看当前防火墙状态:n ...

  10. LinkedList集合(JDK1.8)

    简述 按照上篇笔记ArrayList集合继续进行介绍list的另一个常见子类LinkedList ?LinkedList介绍 1.数据结构 说明:linkedlist的底层数据结构是个双向链表结构,也 ...