Spring Boot 集成教程


概述

本文介绍spring boot项目集成redis缓存的过程。

redis是一个开源的内存NOSQL数据库,在web开发中主要被用于数据缓存。一般在高并发的情况下,web服务器接受访问时,直接从数据库加载是慢的,需要把常用数据缓存到redis中,提高加载速度和并发能力。

项目内容

创建一个spring boot项目,配置redis各相关 bean,实现几个接口,通过两种方式测试redis缓存:

  • 以注解方式自动缓存
  • RedisTemplate手动访问redis服务器

要求

  • 安装redis服务器,参考官网文档,如没有linux系统可虚拟机安装
  • JDK1.8或更新版本
  • Eclipse开发环境

如没有开发环境,可参考前面章节:[spring boot 开发环境搭建(Eclipse)]。

项目创建

创建spring boot项目

打开Eclipse,创建spring boot的spring starter project项目,选择菜单:File > New > Project ...,弹出对话框,选择:Spring Boot > Spring Starter Project,在配置依赖时,勾选webredis,完成项目创建。

项目依赖

需要用到commons-pool2库,在pom.xml中添加依赖

		<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

项目配置

application.properties文件中配置redis服务器的连接

## REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.0.99
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

代码实现

项目目录结构如下图,我们添加了几个类,下面将详细介绍。

Redis Java配置(RedisConfig.java)

首先使用@EnableCaching开启以注解方式使用缓存。

然后配置redis相关的bean

  • RedisTemplate - 访问redis的bean,用于手动访问redis服务器
  • 缓存管理器 - 注解方式使用缓存的配置
  • KeyGenerator - 自定义缓存key的生成
  • Json序列化 - Json对象被缓存时的序列化
/**
* @description redis配置 配置序列化方式以及缓存管理器
*/
@EnableCaching // 开启缓存
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig { /**
* 配置自定义redisTemplate
*
* @param connectionFactory
* @return
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setValueSerializer(jackson2JsonRedisSerializer());
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(jackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
} /**
* json序列化
* @return
*/
@Bean
public RedisSerializer<Object> jackson2JsonRedisSerializer() {
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
return serializer;
} /**
* 配置缓存管理器
* @param redisConnectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
// 生成一个默认配置,通过config对象即可对缓存进行自定义配置
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
// 设置缓存的默认过期时间,也是使用Duration设置
config = config.entryTtl(Duration.ofMinutes(1))
// 设置 key为string序列化
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
// 设置value为json序列化
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer()))
// 不缓存空值
.disableCachingNullValues(); // 设置一个初始化的缓存空间set集合
Set<String> cacheNames = new HashSet<>();
cacheNames.add("timeGroup");
cacheNames.add("user"); // 对每个缓存空间应用不同的配置
Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
configMap.put("timeGroup", config);
configMap.put("user", config.entryTtl(Duration.ofSeconds(120))); // 使用自定义的缓存配置初始化一个cacheManager
RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory)
// 一定要先调用该方法设置初始化的缓存名,再初始化相关的配置
.initialCacheNames(cacheNames)
.withInitialCacheConfigurations(configMap)
.build();
return cacheManager;
} /**
* 缓存的key是 包名+方法名+参数列表
*/
@Bean
public KeyGenerator keyGenerator() {
return (target, method, objects) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append("::" + method.getName() + ":");
for (Object obj : objects) {
sb.append(obj.toString());
}
return sb.toString();
};
} }

添加实体类 User


public class User {
private long id;
private String nickname;
private String mobile; @JsonProperty(access = Access.WRITE_ONLY) //在输出的Json数据中隐藏密码,只能输入不输出
private String password;
private String role; public User(long id, String nickname, String mobile, String password, String role) {
this.id = id;
this.nickname = nickname;
this.mobile = mobile;
this.password = password;
this.role = role;
} public User() {
super();
} public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}

常用的缓存注解

  • @Cacheable - 表明对应方法的返回结果可以被缓存,首次调用后,下次就从缓存中读取结果,方法不会再被执行了。
  • @CachePut - 更新缓存,方法每次都会执行
  • @CacheEvict - 清除缓存,方法每次都会执行

添加User的服务层

因为主要的业务逻辑在服务层实现,一般会把缓存注解加在服务层的方法上。

下面几个服务层的方法会加缓存注解:

  • getUserById - 方法的返回结果会被缓存到redis,使用注解@Cacheable
  • updateUserNickname - 原始数据被更新了,废弃缓存数据,使用注解@CacheEvict

UserSevice.java 接口

public interface UserService {

    public User getUserById(long userId);
public User updateUserNickname(long userId, String nickname);
}

UserServiceImpl.java 实现类


@Service("userService")
public class UserServiceImpl implements UserService { private static final org.slf4j.Logger log = LoggerFactory.getLogger(UserServiceImpl.class); private User user = new User(1l, "abc1", "13512345678", "123456", "role-user"); @Cacheable(value = "user", key= "#userId")
@Override
public User getUserById(long userId) { log.info("加载用户信息");
return user;
} @CacheEvict(value = "user", key= "#userId")
@Override
public User updateUserNickname(long userId, String nickname) { user.setNickname(nickname); return user;
}
}

添加User的控制层


@RestController
@EnableAutoConfiguration
@RequestMapping("/user")
public class UserController { // 注入service类
@Resource
private UserService userService; // 注入RedisTemplate
@Resource
private RedisTemplate<String, Object> redis; // 读取用户信息,测试缓存使用:除了首次读取,接下来都应该从缓存中读取
@RequestMapping(value="{id}", method=RequestMethod.GET, produces="application/json")
public User getUser(@PathVariable long id) throws Exception { User user = this.userService.getUserById(id); return user;
} // 修改用户信息,测试删除缓存
@RequestMapping(value = "/{id}/change-nick", method = RequestMethod.POST, produces="application/json")
public User changeNickname(@PathVariable long id) throws Exception{ String nick = "abc-" + Math.random();
User user = this.userService.updateUserNickname(id, nick); return user;
} // 使用RedisTemplate访问redis服务器
@RequestMapping(value="/redis", method=RequestMethod.GET, produces="application/json")
public String redis() throws Exception { // 设置键"project-name",值"qikegu-springboot-redis-demo"
redis.opsForValue().set("project-name", "qikegu-springboot-redis-demo");
String value = (String) redis.opsForValue().get("project-name"); return value;
}
}

运行

Eclipse左侧,在项目根目录上点击鼠标右键弹出菜单,选择:run as -> spring boot app 运行程序。 打开Postman访问接口,

同时监控redis服务器。

监控redis服务器,使用redis-cli命令连上服务器,然后使用monitor命令开始监控:

运行结果如下:

获取用户信息

redis中的数据,可以看到数据通过SET指令保存进redis了

多次获取用户信息,可以看到通过GET指令从redis中读取缓存

修改用户信息

redis中的缓存被删除了

测试使用RedisTemplate访问redis服务器

redis中的数据变化

总结

完整代码

spring boot redis 缓存(cache)集成的更多相关文章

  1. spring boot redis缓存JedisPool使用

    spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...

  2. spring boot redis缓存入门

    摘要: 原创出处 泥瓦匠BYSocket 下载工程 springboot-learning-example ,工程代码注解很详细.JeffLi1993/springboot-learning-exam ...

  3. Spring Boot Redis 集成配置(转)

    Spring Boot Redis 集成配置 .embody{ padding:10px 10px 10px; margin:0 -20px; border-bottom:solid 1px #ede ...

  4. Spring Boot 项目学习 (三) Spring Boot + Redis 搭建

    0 引言 本文主要介绍 Spring Boot 中 Redis 的配置和基本使用. 1 配置 Redis 1. 修改pom.xml,添加Redis依赖 <!-- Spring Boot Redi ...

  5. 使用maven简单搭建Spring mvc + redis缓存

    注:此文参考并整合了网上的文章 <spring缓存机制>:http://blog.csdn.net/sidongxue2/article/details/30516141 <配置 S ...

  6. Spring Boot微服务如何集成fescar解决分布式事务问题?

    什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...

  7. spring boot + redis 实现session共享

    这次带来的是spring boot + redis 实现session共享的教程. 在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring se ...

  8. Spring Boot 2.0 快速集成整合消息中间件 Kafka

    欢迎关注个人微信公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site ...

  9. Spring Boot与ActiveMQ的集成

    Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.本节中,将以Active ...

随机推荐

  1. 分享一款免费的工控组态软件(PCHMI)

    PCHMI严格的讲它并不是一款组态软件,也不是一款SCADA软件,而是一个基于.NET构架的DLL文件,开发者可以使用微软的Visual Studio将PCHMI.DLL加载到工具箱里面进行二次开发. ...

  2. 51nod 1441:士兵的数字游戏

    1441 士兵的数字游戏 题目来源: CodeForces 基准时间限制:4 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  取消关注 两个士兵正在玩一个游戏,游戏开始的时 ...

  3. 使用命令行连接远程DB2数据库

    1. 打开命令窗口 win + r  输入 db2cmd 2. 首先在客户机上对远程节点进行编目 CATALOG  TCPIP                    //编目一个TCP/IP节点NOD ...

  4. Day6 - D - Tree 园丁的烦恼 HYSBZ - 1935

    很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个园丁道: “最近我在思索一个问题, ...

  5. maven详解 之仓库

    Maven仓库分类   MAVEN仓库分类 Maven仓库分为:本地仓库+远程仓库两大类 远程仓库又分为:中央仓库+私服+其它公共远程仓库 1,在Maven中,任何一个依赖.插件或者项目构建的输出,都 ...

  6. django-腾讯paas-appengine阅读

    1 重写View基类的dispatch函数 api/baseview.py 在一个post请求中,在header中,CONTENT_TYPE为application/json,然后在request.b ...

  7. jmeter简单压测、下载文件

    一.jmeter做简单压测(单机) 1.添加需要压测的HTTP请求 2.添加聚合报告 3.设置压测场景 4.查看聚合报告 二.多机同时进行压测 1.在需要连接的电脑上打开jmeter  bin目录下的 ...

  8. 九九乘法表的四种三角形排布方式(for循环以及while循环的互换)

    #region //右上 for (int i = 1; i <= 9; i++){ for (int j = 1; j <= 9; j++){ if (i > j){ Consol ...

  9. use matplotlib to drew a table

    $sudo apt-get install python3-matplotlib gyf@gyf-VirtualBox:~$ python3Python 3.6.9 (default, Nov  7 ...

  10. 关于div水平垂直居中的几种方法

    Dom结构: <div class="box"> <div class="inner"> 123 </div> </d ...