【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案
前言
之前已经写过一篇文章介绍SpringBoot整合Spring Cache,SpringBoot默认使用的是ConcurrentMapCacheManager,在实际项目中,我们需要一个高可用的、分布式的缓存解决方案,使用默认的这种缓存方式,只是在当前进程里缓存了而已。Spring Cache整合Redis来实现缓存,其实也不是一件复杂的事情,下面就开始吧。
关于Spring Cache的运用,请参考[快学SpringBoot]快速上手好用方便的Spring Cache缓存框架
新建一个SpringBoot项目
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
主要是最下面两个依赖:spring-boot-starter-cache 和 spring-boot-starter-data-redis。
配置Redis
在application.properties中添加redis的配置
spring.redis.host=127.0.0.1
# spring.redis.password=
spring.redis.port=6379
这是最基础的三个配置(其实默认值就是这样,就算不写也可以)。当然,还有空闲连接数,超时时间,最大连接数等参数,我这里都没有设置,在生产项目中,根据实际情况设置。
RedisCacheConfig
新建RedisCacheConfig.class
@Configuration
@EnableCaching
public class RedisCacheConfig {
/**
* 缓存管理器
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
//设置CacheManager的值序列化方式为json序列化
RedisSerializer<Object> jsonSerializer = new GenericJackson2JsonRedisSerializer();
RedisSerializationContext.SerializationPair<Object> pair = RedisSerializationContext.SerializationPair
.fromSerializer(jsonSerializer);
RedisCacheConfiguration defaultCacheConfig=RedisCacheConfiguration.defaultCacheConfig()
.serializeValuesWith(pair);
return new RedisCacheManager(redisCacheWriter, defaultCacheConfig);
}
}
如果要设置缓存管理器所管理的缓存名字,RedisCacheManager构造方法提供一个可变参数的构造器:
测试
新建一个MockService.java,代码如下:
@Service
public class MockService {
/**
* value 缓存的名字,与cacheName是一个东西
* key 需要缓存的键,如果为空,则会根据参数自动拼接
* 写法:SpEL 表达式
*/
@Cacheable(value = "listUsers", key = "#username")
public List<String> listUsers(String username) {
System.out.println("执行了listUsers方法");
return Arrays.asList("Happyjava", "Hello-SpringBoot", System.currentTimeMillis() + "");
}
}
新建一个TestController.java,代码如下:
@RestController
public class TestController {
private final MockService mockService;
public TestController(MockService mockService) {
this.mockService = mockService;
}
@GetMapping(value = "/listUsers")
public Object listUsers(String username) {
return mockService.listUsers(username);
}
}
请求接口:
Redis中的数据:
原创声明
本文发布于掘金号【Happyjava】。Happy的掘金地址:https://juejin.im/user/5cc2895df265da03a630ddca,Happy的个人博客:http://blog.happyjava.cn。欢迎转载,但须保留此段声明。
关注公众号领资料
搜索公众号【Happyjava】,回复【电子书】和【视频】,即可获取大量优质电子书和大数据、kafka、nginx、MySQL等视频资料
【快学SpringBoot】Spring Cache+Redis实现高可用缓存解决方案的更多相关文章
- Redis主从高可用缓存
nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存 一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集 ...
- nopCommerce 3.9 大波浪系列 之 使用Redis主从高可用缓存
一.概述 nop支持Redis作为缓存,Redis出众的性能在企业中得到了广泛的应用.Redis支持主从复制,HA,集群. 一般来说,只有一台Redis是不可行的,原因如下: 单台Redis服务器会发 ...
- 【快学springboot】13.操作redis之String数据结构
前言 在之前的文章中,讲解了使用redis解决集群环境session共享的问题[快学springboot]11.整合redis实现session共享,这里已经引入了redis相关的依赖,并且通过spr ...
- 【快学springboot】8.JPA乐观锁OptimisticLocking
介绍 当涉及到企业应用程序时,正确地管理对数据库的并发访问是至关重要的.为此,我们可以使用Java Persistence API提供的乐观锁定机制.它导致在同一时间对同一数据进行多次更新不会相互干扰 ...
- 【快学springboot】12.实现拦截器
前言 之前在[快学springboot]6.WebMvcConfigurer配置静态资源和解决跨域里有用到WebMvcConfigurer接口来实现静态资源的映射和解决跨域请求,并且在文末还说了Web ...
- 【快学springboot】4.接口参数校验
前言 在开发接口的时候,参数校验是必不可少的.参数的类型,长度等规则,在开发初期都应该由产品经理或者技术负责人等来约定.如果不对入参做校验,很有可能会因为一些不合法的参数而导致系统出现异常. 上一篇文 ...
- Redis Sentinel安装与部署,实现redis的高可用
前言 对于生产环境,高可用是避免不了要面对的问题,无论什么环境.服务,只要用于生产,就需要满足高可用:此文针对的是redis的高可用. 接下来会有系列文章,该系列是对spring-session实现分 ...
- Redis Cluster高可用集群在线迁移操作记录【转】
之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移.由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上.考虑 ...
- Redis Cluster高可用集群在线迁移操作记录
之前介绍了redis cluster的结构及高可用集群部署过程,今天这里简单说下redis集群的迁移.由于之前的redis cluster集群环境部署的服务器性能有限,需要迁移到高配置的服务器上.考虑 ...
随机推荐
- [蓝桥杯][基础训练]Sine之舞
Description 最近FJ为它的奶牛开设了数学分析课,FJ知道,若要学好这门课,必须有一个好的三角函数基本功. 所以他为奶牛们做了一个“Sine之舞”的游戏,寓教于乐,提高奶牛的计算能力. 不妨 ...
- Html5 Canvas 使用
<div> <img id="scream" src="dali.jpg" alt="The Scream" width= ...
- dremio的学习点滴
在连接数据源后,进行数据源反射的创建,dremio会在本地创建一个类似于副本的文件,具体目录未知,当下次去执行sql时,则会启动加速器进行查询速度的优化. 反射策略: full update:数据源全 ...
- 云原生学习笔记(3)——Kubernetes基本概念
学习地址:https://developer.aliyun.com/lesson_1651_13078?spm=5176.270689.1397405.6.716ef5f8Q9z1z3#_13078 ...
- linux 内存,cpu占用最高进程查询
1. 可以使用一下命令查使用内存最多的10个进程 ps -aux | sort -k4nr | head -n 10 2. 可以使用一下命令查使用CPU最多的10个进程 ps -aux | sort ...
- RMQ入门解析
参照大佬博客:https://www.cnblogs.com/yoke/p/6949838.html RMQ(Range Minimum/Maximum Query), 是一种问题,即 查询给定区间 ...
- 【StarUML】用例图
用例图是在项目初期确认需求的时候,需要明确各个参与者之间的关系以及对应的功能,它可视化地展示了整个系统的功能以及功能之间.功能与参与者之间的关系. 1.元素 1.1 角色(actor) 角色不一定是人 ...
- AcWing 870. 约数个数
#include <iostream> #include <algorithm> #include <unordered_map> #include <vec ...
- 引用opencv静态库的makefile写法
参考博客:https://blog.csdn.net/baidu_31872269/article/details/91985846 参考博客:https://blog.csdn.net/Felaim ...
- 染色板QPlette
QPalette::Window 背景颜色 QPalette::WindowText 文本颜色 QPalette::Background 同QPalette::Window QPalette::For ...