解析SpringBoot整合SpringDataRedis的过程
Spring-Data-Redis项目(简称SDR)对Redis的Key-Value数据存储操作提供了更高层次的抽象,类似于Spring Framework对JDBC支持一样。
项目主页: http://projects.spring.io/spring-data-redis/
项目文档: http://docs.spring.io/spring-data/redis/docs/1.5.0.RELEASE/reference/html/
本文给大家介绍SpringBoot整合SpringDataRedis的过程。
项目环境:Jdk11.0.2、Redis3.0.0、Centos7
一、安装Redis3.0.0
在Linux下解压redis安装包
进入解压后的目录进行编译
编译完成
将redis安装到指定目录
启动redis
默认端口Port:6379
属于前置启动,会占用整个终端,按Ctrl+C停止
后置启动,将redis.conf复制到redis/bin目录下
修改复制后的配置文件,将no该为yes
Centos7开放端口
启动redis 查看redis是否启动成功
IDEA客户端工具连接redis服务成功
二、整合SpringDataRedis
1.修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springboot-redis</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
</parent>
<dependencies>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- SpringDataRedis2.x已不使用jedis 如需要jedis,需手动导入 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
2.创建RedisConfig配置类
jedis中的源码:
/**
* @Author: kenewstar
* @Description: Redis配置类
* @Date:Created in 2020/6/27
*/
@Configuration
public class RedisConfig {
//连接池
@Bean
public JedisPoolConfig jedisPoolConfig(){
JedisPoolConfig config = new JedisPoolConfig();
//最大空闲数(默认8)
config.setMaxIdle(12);
//最小空闲数(默认0)
config.setMinIdle(6);
//最大连接数(默认8)
config.setMaxTotal(24);
return config;
}
/**
* SpringDataRedis2.x版本已废弃使用jedis
* @param config
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig config){
JedisConnectionFactory factory = new JedisConnectionFactory();
//不推荐使用,SpringDataRedis2.x中已过时
factory.setPoolConfig(config);
factory.setHostName("192.168.40.128"); //redis服务的ip
factory.setPort(6379); //redis服务的端口
return factory;
}
//redis操作类
@Bean
public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//设置key/value的序列化器
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
Redis序列化器
3.创建Redis测试类
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void set(){
this.redisTemplate.opsForValue().set("name","muke");
}
@Test
public void get(){
Object name = this.redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
三、SpringDataRedis存取Java对象
不推荐该种方式存取java对象,会造成空间浪费,使用json字符串格式存取会更好
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void setObject(){
User user = new User();
user.setId(1);
user.setName("kenewstar");
user.setAge(21);
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
this.redisTemplate.opsForValue().set("user",user);
}
@Test
public void getObject(){
this.redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
User user = (User)this.redisTemplate.opsForValue().get("user");
System.out.println(user);
}
}
四、SpringDataRedis存取Json格式的Java对象
/**
* @Author: kenewstar
* @Description: 测试redis操作
* @Date:Created in 2020/6/27
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class)
public class TestRedisString {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Test
public void setJsonObject(){
User user = new User();
user.setId(2);
user.setName("kenewstar2");
user.setAge(22);
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
this.redisTemplate.opsForValue().set("userJson",user);
}
@Test
public void getJsonObject(){
this.redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(User.class));
User user = (User) this.redisTemplate.opsForValue().get("userJson");
System.out.println(user);
}
}
五、SpringDataRedis2.x中的配置
1.创建application.yml全局配置文件
将redis数据库连接信息与连接池信息配置在全局配置文件中
#redis单机应用环境配置
spring:
redis:
host: 192.168.40.128
port: 6379
password: #无密码不配置
database: 0 #数据库索引(0-15)默认为0
timeout: 300s #连接超时时间
#redis连接池配置
jedis:
pool:
max-idle: 16 #最大空闲数(默认8)
min-idle: 4 #最小空闲数(默认0)
max-active: 20 #最大连接数(默认8)
max-wait: 60000ms # 连接池最大阻塞等待时间 默认-1ms (-1 :表示没有限制) 这里设置1分钟
2.创建RedisConfig配置类
/**
* @Author: kenewstar
* @Description: Redis配置类
* @Date:Created in 2020/6/27
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory){
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
return redisTemplate;
}
}
3.测试SpringDataRedis,代码与上述代码测试代码相同,在这就不给大家重复介绍了。
总结
到此这篇关于SpringBoot整合SpringDataRedis的文章就介绍到这了,更多相关SpringBoot整合SpringDataRedis内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
以上是 解析SpringBoot整合SpringDataRedis的过程 的全部内容, 来源链接: utcz.com/z/317890.html