解析SpringBoot整合SpringDataRedis的过程

网友投稿 308 2022-12-03


解析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文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POMhttp:///4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gtVaWYAMoa;

4.0.0

org.example

springboot-redis

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

2.2.6.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

org.springframework.boot

spring-boot-starter-data-redis

redis.clients

jedis

3.3.0

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POMhttp:///4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gtVaWYAMoa;

4.0.0

org.example

springboot-redis

1.0-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

2.2.6.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

org.springframework.boot

spring-boot-starter-data-redis

redis.clients

jedis

3.3.0

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 redisTemplate(JedisConnectionFactory factory){

RedisTemplate 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 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(naVaWYAMoame);

}

}

三、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 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 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 redisTemplate(RedisConnectionFactory factory){

RedisTemplate redisTemplate = new RedisTemplate<>();

redisTemplate.setConnectionFactory(factory);

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setValueSerializer(new StringRedisSerializer());

return redisTemplate;

}

}

3.测试SpringDataRedis,代码与上述代码测试代码相同,在这就不给大家重复介绍了。

总结


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot在有参构造方法类中使用@Value注解取值
下一篇:IntelliJ IDEA 常用设置(配置)吐血整理(首次安装必需)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~