一、概述

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实现的更多相关文章

  1. 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架

    前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...

  2. Spring Cache缓存框架

    一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...

  3. 使用Spring Cache缓存出现的小失误

    前文:今天在使用Spring Boot项目使用Cache中出现的小失误,那先将自己创建项目的过程摆出来 1.首先创建一个Spring Boot的项目(我这里使用的开发工具是Intellij IDEA) ...

  4. Spring Cache缓存注解

    目录 Spring Cache缓存注解 @Cacheable 键生成器 @CachePut @CacheEvict @Caching @CacheConfig Spring Cache缓存注解 本篇文 ...

  5. Spring Cache缓存技术,Cacheable、CachePut、CacheEvict、Caching、CacheConfig注解的使用

    前置知识: 在Spring Cache缓存中有两大组件CacheManager和Cache.在整个缓存中可以有多个CacheManager,他们负责管理他们里边的Cache.一个CacheManage ...

  6. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  7. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  8. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  9. Spring cache 缓存

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  10. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

随机推荐

  1. MTK WIFI底部加入返回按钮

    wifi设置页面的源码是WiFiSettings.java 类,该类实际就是一个PreferenceFragment的子类,下面是源码,工作原理在注释中说明 FrameLayout.LayoutPar ...

  2. JAVA程序员_常用英语

    干程序员这行实在是离不开英语,干程序员是一项很辛苦的工作,要成为一个高水平的程序员尤为艰难.这是因为计算机软件技术更新的速度越来越快,而这些技术大多来源于英语国家,我们在引进这些技术时往往受到语言障碍 ...

  3. 修改testtools框架,将测试结果显示用例注释名字

    在之前介绍的测试框架testtool中,发现测试结果中显示的都是测试用例的函数名,并没有将注释显示出来 这很不符合国人使用阿,没办法,自己动手来改改吧 首先,testtools是继承unittest的 ...

  4. A - Fire Net

    Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...

  5. 更换title上的ico

    var rurl = "/static/dashboard/img/favicon.ico"; var a = ''; var links = document.getElemen ...

  6. css学习_css复合选择器

    css复合选择器 a.交集选择器  (即...又...:选择器之间不能有空格) p.one{color:red;] b.并集选择器(中间由逗号隔开) p,div{color:red;} c.后代选择器 ...

  7. [No0000165]SQL 优化

    SELECT 标识选择哪些列FROM 标示从哪个表中选择WHERE 过滤条件GROUP BY 按字段数据分组HAVING 字句过滤分组结果集ORDER BY 序按字段排序 ASC( 默认) 序升序 D ...

  8. 静态方法中只允许访问静态数据,那么,如何在静态方法中访问类的实例成员(即没有附加static关键字的字段或方法)?

    package test.two; public class jingtaihanshu { int x = 3; static int  y = 4; public static void Meth ...

  9. arcgis10.4 server第一次发布地图报错:We were unable to connect to...Error:Proxy server got bad address...

    arcgis 10.4发布地图跟10.2不一样.server url里的http要改为https,否则就会连接不上.

  10. io.UnsupportedOperation: not readable

    两处错误一.你是用open打开一个文件,此时调用的是w写入模式,下面使用read是没有权限的,你得使用w+读写模式二.使用write写入一个字符s,但是此时并没有真正的写入,而是还存在与内存中.此时执 ...