栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

解析SpringBoot整合SpringDataRedis的过程

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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



  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中的源码:


@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;
  }

  
  @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测试类


@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(name);
  }
}

三、SpringDataRedis存取Java对象

不推荐该种方式存取java对象,会造成空间浪费,使用json字符串格式存取会更好


@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对象


@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配置类


@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,代码与上述代码测试代码相同,在这就不给大家重复介绍了。

总结

到此这篇关于SpringBoot整合SpringDataRedis的文章就介绍到这了,更多相关SpringBoot整合SpringDataRedis内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/133533.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号