Title

Springboot使用Jedis

1、导包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.1.0</version>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.8</version>
    <scope>provided</scope>
</dependency>

2、添加 application.properties 配置

#ip地址
redis.host=192.168.25.131
#端口号
redis.port=6379
#如果有密码
redis.password=123456
#客户端超时时间单位是毫秒 默认是2000
redis.timeout=3000
#数据库,默认的是0
redis.database=0
#最大空闲数
redis.maxIdle=300  
#连接池的最大数据库连接数。设为0表示无限制,如果是jedis 2.4以后用redis.maxTotal
redis.maxActive=1000
#控制一个pool可分配多少个jedis实例,用来替换上面的redis.maxActive,如果是jedis 2.4以后用该属性
redis.maxTotal=1000
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
redis.maxWaitMillis=1000
#在空闲时检查有效性, 默认false
redis.testOnBorrow=false
#连接耗尽是否阻塞,false代表抛异常,true代表阻塞直到超时,默认为true
redis.blockWhenExhausted=false
#是否启用pool的jmx管理功能, 默认true
redis.JmxEnabled=true
 
#下面的不是必须的配置
#连接的最小空闲时间 默认1800000毫秒(30分钟)
minEvictableIdleTimeMillis=300000  
#每次释放连接的最大数目,默认3
numTestsPerEvictionRun=1024  
#逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
timeBetweenEvictionRunsMillis=30000  
#是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个,数据量大的时候建议关闭
testWhileIdle=true

3、编写 RedisConfig 配置文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@PropertySource("classpath:application.properties")
public class RedisConfig {
    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private int port;

    @Value("${redis.password}")
    private int password;

    @Value("${redis.database}")
    private int database;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.maxIdle}")
    private int maxIdle;

    @Value("${redis.maxWaitMillis}")
    private int maxWaitMillis;

    @Value("${redis.blockWhenExhausted}")
    private Boolean blockWhenExhausted;

    @Value("${redis.JmxEnabled}")
    private Boolean JmxEnabled;

    @Bean
    public JedisPool jedisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true
        jedisPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
        // 是否启用pool的jmx管理功能, 默认true
        jedisPoolConfig.setJmxEnabled(JmxEnabled);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout,password,database);
        return jedisPool;
    }
}

使用常量

package com.common.constants;

/**
 * @author z
 */
public interface RedisConstants {

    String HOST = "XX.XX.XX.XX";
    int PORT = 6379;
    String PASSWORD = "";
    int TIME_OUT = 5000;
    int MAX_ACTIVE = 200;
    int MAX_IDLE = 8;
    int MIN_IDLE = 0;
    int MAX_WAIT_MILLIS = -1;
    int DATA_BASE = 2;
}
package com.common.config;

import com.common.constants.RedisConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author z
 */
@Configuration
public class RedisConfig {

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(RedisConstants.MAX_IDLE);
        jedisPoolConfig.setMaxWaitMillis(RedisConstants.MAX_WAIT_MILLIS);
        jedisPoolConfig.setMaxTotal(RedisConstants.MAX_ACTIVE);
        jedisPoolConfig.setMinIdle(RedisConstants.MIN_IDLE);
        return jedisPoolConfig;
    }

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig jedisPoolConfig = jedisPoolConfig();
        return new JedisPool(jedisPoolConfig, RedisConstants.HOST, RedisConstants.PORT, RedisConstants.TIME_OUT, RedisConstants.PASSWORD, RedisConstants.DATA_BASE);
    }

}

4、编写RedisUtil工具类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;


@Component
public class RedisUtil {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 向Redis中存值,永久有效
     */
    public String set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            return "0";
        } finally {
            jedis.close();
        }
    }

    /**
     * 根据传入Key获取指定Value
     */
    public String get(String key) {
        Jedis jedis = null;
        String value;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            return "0";
        } finally {
            jedis.close();
        }
        return value;
    }

    /**
     * 校验Key值是否存在
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            return false;
        } finally {
            jedis.close();
        }
    }

    /**
     * 删除指定Key-Value
     */
    public Long del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            return 0L;
        } finally {
            jedis.close();
        }
    }

    /**
     * 分布式锁
     * @param key
     * @param value
     * @param time 锁的超时时间,单位:秒
     *
     * @return 获取锁成功返回"OK",失败返回null
     */
    public String getDistributedLock(String key,String value,int time){
        Jedis jedis = null;
        String ret = "";
        try {
            jedis = jedisPool.getResource();

            ret = jedis.set(key, value, new SetParams().nx().ex(time));
            return ret;
        } catch (Exception e) {
            return null;
        } finally {
            jedis.close();
        }
    }
}

使用jackson进行对象的序列化与反序列化

            // 序列化对象存入redis
            ObjectMapper om = new ObjectMapper();
            String s = om.writeValueAsString(userVO);
            redisUtil.set(redisKey, s);

            // 反序列化成对象
            ObjectMapper om = new ObjectMapper();
            UserVO userVO = om.readValue(user, UserVO.class);
            String username = userVO.getUsername();

进行反序列化时出现: Cannot construct instance of java.time.LocalDate、Could not read JSON: Expected array or string.

原因:

存入redis中时应该时间格式应该是 1998-07-01 和 2021-01-30 03:00:51这样的时间格式,但是默认存的却是默认格式的。
LocalDateTime、LocalDate、LocalTime没有格式化成yyyy-MM-dd HH:mm:ss类似这样的格式,还有要将LocalDateTime、LocalDate、LocalTime的反序列化方式设置一下,因为它们默认的反序列化方式是序列化成了一个数组类型

解决:在序列化实体属性添加注解

    /**
     * 创建时间
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

5、使用 springboot 默认 RedisTemplate

  • 导包
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  • 配置 application.properties
spring:
  redis:
    host: 192.168.1.111
    port: 6379

由于Springboot-data-redis帮我们自动装载了RedisTemplate对象,所以我们无需注册该bean。但是,如果用默认的 RedisTemplate ,那么在序列化存到redis中就会发现,key 就变的“不正常”了。
比如,存之前key为"test" ,进入redis看,key就变成了"\xac\xed\x00\x05t\x00\x04test" 。这与RedisTemplate默认提供的序列化协议有关。为了使其更符合我们的需求,我们需要订制序列化协议。

  • 选择Springboot自带的Jackson为序列化协议,进行配置
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 使用Jackson2JsonRedisSerialize 替换默认序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
        // 设置value的序列化规则和 key的序列化规则
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setKeySerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
  • 测试使用
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EdApplication.class)
public class FenCiUtilText {
    @Autowired
    private RedisTemplate redisTemplate;
    //测试放入
    @Test
    public void testRedisSet() {
        try {
            redisTemplate.opsForValue().set("test","This is a Springboot-Redis test!");
        } catch (Exception e){
            System.out.println(e.toString());
        }
 
    }
    //测试拿出
    @Test
    public void testRedisGet() {
        try {
            String key="test";
            Boolean isHas = redisTemplate.hasKey(key);
            if (isHas){
                Object test = redisTemplate.opsForValue().get(key);
                System.out.println(test);
            }else {
                System.out.println("抱歉!不存在key值为"+key);
            }
        } catch (Exception e){
            System.out.println(e.toString());
        }
    }
}

参考链接:
https://www.cnblogs.com/yuqingya/p/12881712.html
https://www.cnblogs.com/beanbag/p/13081444.html

posted @ 2022-03-29 18:35  快乐小洋人  阅读(2132)  评论(0编辑  收藏  举报