SpringBoot学习之整合SpringDataRedis

项目环境:Jdk11.0.2、Redis3.0.0、Centos7

一、安装Redis3.0.0

在Linux下解压redis安装包
在这里插入图片描述
在这里插入图片描述
进入解压后的目录进行编译
SpringBoot学习之整合SpringDataRedis_第1张图片
编译完成
SpringBoot学习之整合SpringDataRedis_第2张图片
将redis安装到指定目录
SpringBoot学习之整合SpringDataRedis_第3张图片
启动redis
SpringBoot学习之整合SpringDataRedis_第4张图片
默认端口Port:6379
属于前置启动,会占用整个终端,按Ctrl+C停止
后置启动,将redis.conf复制到redis/bin目录下
SpringBoot学习之整合SpringDataRedis_第5张图片
修改复制后的配置文件,将no该为yes

在这里插入图片描述
SpringBoot学习之整合SpringDataRedis_第6张图片
Centos7开放端口
在这里插入图片描述
启动redis 查看redis是否启动成功
在这里插入图片描述
IDEA客户端工具连接redis服务成功
SpringBoot学习之整合SpringDataRedis_第7张图片

二、整合SpringDataRedis

1.修改pom.xml文件


<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.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>springboot-redisartifactId>
    <version>1.0-SNAPSHOTversion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.6.RELEASEversion>
    parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>3.3.0version>
        dependency>
    dependencies>
project>

2.创建RedisConfig配置类

jedis中的源码:
SpringBoot学习之整合SpringDataRedis_第8张图片

/**
 * @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序列化器
SpringBoot学习之整合SpringDataRedis_第9张图片

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,代码与上述代码测试代码相同

略…

你可能感兴趣的:(JavaEE,#,SpringBoot)