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. Myeclipse项目出现红叉解决方案

    1.右键点击你的项目.选中properties 2.选中MyEclipse下的Project Facets里面的java 此时的版本号为1.5,修改 3.选中MyEclipse下的Project Fa ...

  2. C++ 类 与 static

    背景 从学习C++到使用现在,发现很多新的东西,正好整理一下. static 为静态,指是当类编译加载的时候,内存就会开辟存储空间的. static 数据成员 在类中,static 可修饰 类中的成员 ...

  3. Android框架模式

    参考大佬写的文章:https://www.jianshu.com/p/f17f5d981de7 1.MVC模式 Model:模型层,负责处理数据的加载或存储 View:视图层,负责界面数据的展示,与和 ...

  4. html基础与入门

    html就是指一个html文件,它是由各种标签组成的 html分为 < !DOCTYPE html > 和 Head 和 Body Head title+meta+link+style B ...

  5. leetcode1261 Find Elements in a Contaminated Binary Tree

    """ Given a binary tree with the following rules: root.val == 0 If treeNode.val == x ...

  6. ubuntu18.04下载yarn

    下载curl sudo apt-get update && sudo apt-get install curl 配置库 curl -sS https://dl.yarnpkg.com/ ...

  7. Java按位运算符之按位取反

    一 数据储存形式 二进制在内存中以补码的形式存在. 补码首位是符号位,0表示该数是正数,1表示该数是负数.   例如:   数值 带符号的二进制原码 (首位表示符号位) 补码 内存中的形式 (*表示无 ...

  8. 在ubuntu中使用ipython

    python自带的shell实在是不怎么好用 大家可以用一下ipython这个软件,它可以自动缩进,补齐,语法高亮等 安装办法: sudo apt install ipython #这个是安装2.7的 ...

  9. UVA - 12166 Equilibrium Mobile (修改天平)(dfs字符串表示的二叉树)

    题意:问使天平平衡需要改动的最少的叶子结点重量的个数. 分析:天平达到平衡总会有个重量,这个重量可以由某个叶子结点的重量和深度直接决定. 如下例子: 假设根结点深度为0,结点6深度为1,若以该结点为基 ...

  10. POJ 3321:Apple Tree 树状数组

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22131   Accepted: 6715 Descr ...