008-spring cache-缓存实现-03-springboot redis实现
一、概述
1.1、安装以及简介
window下redis安装:https://www.cnblogs.com/bjlhx/p/7429811.html
mac上docker集群使用:009-docker-安装-redis:5.0.3-单点配置、集群配置
更多介绍:https://www.cnblogs.com/bjlhx/category/1066467.html
1.2、Redis 优势
- 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s 。
- 丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
- 原子 – Redis的所有操作都是原子性的,意思就是要么成功执行要么失败完全不执行。单个操作是原子性的。多个操作也支持事务,即原子性,通过MULTI和EXEC指令包起来。
- 丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性
二、使用
2.1、redis单机测试
代码地址:https://github.com/bjlhx15/common.git spring-cache/springboot2-cache-redis-single
1、导入依赖pom
<!--添加redis缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
就只需要这一个依赖,不需要spring-boot-starter-cache..
当你导入这一个依赖时,SpringBoot的CacheManager就会使用RedisCache。
如果你的Redis使用默认配置,这时候已经可以启动程序了。
2、在application.properties文件中写下连接redis所需要的信息。
# Redis数据库索引(默认为0),如果设置为1,那么存入的key-value都存放在select 1中
spring.redis.database=1
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
3、spring redis其他用法
除了使用注解,我们还可以使用Redis模板。
Spring boot集成 Redis 客户端jedis。封装Redis 连接池,以及操作模板。
@Autowired
private StringRedisTemplate stringRedisTemplate;//操作key-value都是字符串 @Autowired
private RedisTemplate redisTemplate;//操作key-value都是对象 /**
* Redis常见的五大数据类型:
* stringRedisTemplate.opsForValue();[String(字符串)]
* stringRedisTemplate.opsForList();[List(列表)]
* stringRedisTemplate.opsForSet();[Set(集合)]
* stringRedisTemplate.opsForHash();[Hash(散列)]
* stringRedisTemplate.opsForZSet();[ZSet(有序集合)]
*/
public void test(){
stringRedisTemplate.opsForValue().append("msg","hello");
}
2.2、集群配置
配置redis连接,两个版本的redis客户端连接池使用有所不同。
| spring-boot版本 | 默认客户端类型 |
|---|---|
| 1.5.x | jedis |
| 2.x | lettuce |
Lettuce 和 Jedis 的定位都是Redis的client
Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,因为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。
springboot2之前redis的连接池为jedis,2.0以后redis的连接池改为了lettuce,lettuce能够支持redis4,需要java8及以上。lettuce是基于netty实现的与redis进行同步和异步的通信,之前看到spring-session-data-redis里的samples已经改为使用LettuceConnectionFactory
查看:spring-boot-starter-data-redis, 通过看RedisAutoConfiguration的源码
@Import({LettuceConnectionConfiguration.class, JedisConnectionConfiguration.class})
可以知道RedisAutoConfiguration完成了对JedisConnectionFactory和LettuceConnectionFactory的自动配置。
同时RedisProperties源码中封装了对redis配置,包括jedis和lettuce
@ConfigurationProperties(
prefix = "spring.redis"
) private final RedisProperties.Jedis jedis = new RedisProperties.Jedis();
private final RedisProperties.Lettuce lettuce = new RedisProperties.Lettuce();
因此我们在使用时直接通过yml配置即可使用Lettuce来连接Redis,对于单机配置
spring:
redis:
host: ip
port: port
password: password
timeout: 2000
lettuce:
pool:
max-active: 8
max-wait: 1
max-idle: 8
min-idle: 0
2.2.1、lettuce链接池集群配置
代码地址:https://github.com/bjlhx15/common.git spring-cache/springboot2-cache-redis-cluster-lettuce
1、pom依赖
<!--添加redis缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
查看 应该包含了common-pool2、lettuce-core等jar
2、直接使用xml配置
<!--redis-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<constructor-arg name="clusterNodes"
value="192.168.199.220:7000,192.168.199.220:7001,192.168.199.220:7002,192.168.199.220:7003,192.168.199.220:7004,192.168.199.220:7005"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory">
<constructor-arg name="clusterConfiguration" ref="redisClusterConfiguration"/>
</bean>
其他不做任何处理,使用即可
2.2.2、jedis连接池集群配置
代码地址:https://github.com/bjlhx15/common.git spring-cache/springboot2-cache-redis-cluster-jedis
1、pom【boot2.0.4版本】
<!--添加redis缓存依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
Spring Boot 2.0中spring-boot-starter-data-redis默认使用Lettuce方式替代了Jedis。使用Jedis的话先排除掉Lettuce的依赖,然后手动引入Jedis的依赖。不要添加版本号
2、连接池配置
<!--redis-->
<bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
<constructor-arg name="clusterNodes"
value="192.168.199.220:7000,192.168.199.220:7001,192.168.199.220:7002,192.168.199.220:7003,192.168.199.220:7004,192.168.199.220:7005"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="clusterConfig" ref="redisClusterConfiguration"/>
</bean>
返回
008-spring cache-缓存实现-03-springboot redis实现的更多相关文章
- 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架
前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- 使用Spring Cache缓存出现的小失误
前文:今天在使用Spring Boot项目使用Cache中出现的小失误,那先将自己创建项目的过程摆出来 1.首先创建一个Spring Boot的项目(我这里使用的开发工具是Intellij IDEA) ...
- Spring Cache缓存注解
目录 Spring Cache缓存注解 @Cacheable 键生成器 @CachePut @CacheEvict @Caching @CacheConfig Spring Cache缓存注解 本篇文 ...
- Spring Cache缓存技术,Cacheable、CachePut、CacheEvict、Caching、CacheConfig注解的使用
前置知识: 在Spring Cache缓存中有两大组件CacheManager和Cache.在整个缓存中可以有多个CacheManager,他们负责管理他们里边的Cache.一个CacheManage ...
- 注释驱动的 Spring cache 缓存介绍
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- [转]注释驱动的 Spring cache 缓存介绍
原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...
- 注释驱动的 Spring cache 缓存介绍--转载
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- Spring cache 缓存
概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
随机推荐
- ASP.NET MVC 4 (十三) 基于表单的身份验证
在前面的章节中我们知道可以在MVC应用程序中使用[Authorize]特性来限制用户对某些网址(控制器/控制器方法)的访问,但这都是在对用户认证之后,而用户的认证则依然是使用ASP.NET平台的认证机 ...
- 【NLP】分词 新词
基于大规模语料的新词发现算法 https://blog.csdn.net/xgjianstart/article/details/52193258 互联网时代的社会语言学:基于SNS的文本数据挖掘 h ...
- django通用视图(类方法)
这周是我入职的第一周,入职第一天看到嘉兴大佬的项目代码.视图中有类方法,我感到很困惑. 联想到之前北京融360的电话面试,问我有无写过类方法……看来有必要了解下视图的类方法,上网搜了很多,原来这就是所 ...
- trace/trace2命令
send REPLICAT REP_1B trace2 /home/oracle/trace2.log send REPLICAT REP_1B trace /home/oracle/trace.lo ...
- [原]openstack-kilo--issue(十六) instance can't get ip 虚拟机不能得到ip(1)
=====问题点:vm instance不能正常获取ip地址(此时用户是:admin) =======不一样的点:如果使用用户demo用户,启动一个vm,同样的image这个时候就能正确获取ip == ...
- JS中判断一个对象是否为null、undefined、0
1.判断undefined: var tmp = undefined; if (typeof(tmp) == "undefined"){ alert("undefined ...
- Unity3D Shader 半兰伯特光照模型
//效果预览 //Shader代码 Shader "Unlit/HalfLambert" { Properties { _MainTex ("Texture", ...
- redis(一)--认识redis
Redis官网对redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...
- maven安装与创建多模块项目
最新版已同步至 http://yywang.info/2014/05/31/maven-install-and-create-project/ maven是一个比较流行的项目管理工具,在最近参与的项目 ...
- p740+5802+外置磁带机连线
扩展柜型号5802 主柜型号p740 下图是连接线说明 需要连12X 和 SPCN 单个磁带机SAS线两根连接单个PCI否则,磁带无法提供多个分区使用 通过HMC或者查看资源情况 HMC配置Lpar分 ...