SpringBoot系列之集成Jedis教程

SpringBoot系列之集成Jedis教程,Jedis是老牌的redis客户端框架,提供了比较齐全的redis使用命令,是一款开源的Java 客户端框架,本文使用Jedis3.1.0加上Springboot2.0,配合spring-boot-starter-data-redis使用,只给出简单的使用demo

软件环境:

  • JDK 1.8

  • SpringBoot 2.2.1

  • Maven 3.2+

  • Mysql 8.0.26

  • spring-boot-starter-data-redis 2.2.1

  • jedis3.1.0

  • 开发工具

    • IntelliJ IDEA

    • smartGit

项目搭建

使用Spring官网的https://start.spring.io快速创建Spring Initializr项目
在这里插入图片描述
选择maven、jdk版本
在这里插入图片描述

选择需要的Dependencies,选择一下Spring Data Redis
在这里插入图片描述
点击next就可以生成一个Springboot项目,不过jedis客户端配置还是要自己加的,所以对pom文件做简单的修改,spring-boot-starter-data-redis默认使用lettuce,所以不用的可以exclusion,然后再加上jedis的配置

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

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

新建一个application.yml配置文件,加上redis一些配置

spring:
  redis:
    port: 6379
    host: 127.0.0.1
    password:
    timeout: 180000ms
    database: 1
    jedis:
      pool:
        min-idle: 0
        max-idle: 100
        max-active: 100
        max-wait: -1ms

新增Redis配置,配置RedisConnectionFactory JedisConnectionFactoryJedisPoolRedisTemplate需要用到也可以配置一下

package com.example.jedis.configuration;

import cn.hutool.core.convert.Convert;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ConditionalOnClass({GenericObjectPool.class, JedisConnection.class, Jedis.class})
@EnableRedisRepositories(basePackages = "com.example.jedis.repository")
@EnableConfigurationProperties(RedisProperties.class)
@Slf4j
public class RedisConfiguration {


    private final RedisProperties properties;


    public RedisConfiguration(RedisProperties properties) {
        this.properties = properties;
    }

    /**
     *
     * testOnBorrow: 向资源池借用连接时是否做连接有效性检测(ping)
     * testOnReturn: 向资源池归还连接时是否做连接有效性检测(ping)
     * testWhileIdle: 是否在空闲资源监测时通过ping命令监测连接有效性,无效连接将被销毁。
     * jmxEnabled: 是否开启JMX监控
     * @Date 2023/12/11 16:29
     * @return redis.clients.jedis.JedisPoolConfig
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        RedisProperties.Pool pool = properties.getJedis().getPool();
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(pool.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(pool.getMaxWait().toMillis());
        jedisPoolConfig.setMaxTotal(pool.getMaxActive());
        jedisPoolConfig.setMinIdle(pool.getMinIdle());
        jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
        jedisPoolConfig.setNumTestsPerEvictionRun(-1);
        jedisPoolConfig.setTestOnBorrow(false);
        jedisPoolConfig.setTestOnReturn(false);
        jedisPoolConfig.setBlockWhenExhausted(false);
        jedisPoolConfig.setTestWhileIdle(true);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setJmxEnabled(true);
        return jedisPoolConfig;
    }

    @Bean
    public RedisStandaloneConfiguration jedisConfig() {
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(properties.getHost());
        config.setPort(properties.getPort());
        config.setDatabase(properties.getDatabase());
        config.setPassword(RedisPassword.of(properties.getPassword()));
        return config;
    }


    @Bean
    public JedisPool jedisPool() {
        JedisPool jedisPool = new JedisPool(jedisPoolConfig(),properties.getHost(),  properties.getPort(),
                Convert.toInt(properties.getTimeout().getSeconds()));
        return jedisPool;
    }


    @Bean
    public RedisConnectionFactory jedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisConfig());
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig());
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return template;
    }
}

写一个实体类,@RedisHash定义存储的hash key

package com.example.jedis.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.springframework.data.redis.core.RedisHash;

import java.io.Serializable;

@RedisHash("user")
@Data
@SuperBuilder(toBuilder = true)
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor
public class UserDto implements Serializable {


    private static final long serialVersionUID = 5962011647926411830L;

    public enum Gender {
        MALE, FEMALE
    }

    private Long id;
    private String name;
    private Gender gender;

}

使用Sping Data Redis的API来实现一个CRUD接口

package com.example.jedis.repository;


import com.example.jedis.model.UserDto;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<UserDto, Long> {
}

写一个测试类来进行测试,ContextConfiguration指定一个配置类

package com.example.jedis;

import cn.hutool.core.util.IdUtil;
import com.example.jedis.configuration.RedisConfiguration;
import com.example.jedis.model.UserDto;
import com.example.jedis.repository.UserRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@SpringBootTest
@ContextConfiguration(classes = RedisConfiguration.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
class SpringbootJedisApplicationTests {

    @Autowired
    JedisPool jedisPool;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserRepository userRepository;

    @Test
    void contextLoads() {
        Jedis jedis= jedisPool.getResource();
        jedis.set("tKey","你好呀");
        jedis.close();

    }

    @Test
    void testRedisTemplate() {
        redisTemplate.opsForValue().set("rtKey","你好呀");
    }

    @Test
    void testCrud() {
        final UserDto userDto = UserDto.builder()
                .id(IdUtil.getSnowflake().nextId())
                .name("用户1")
                .gender(UserDto.Gender.MALE)
                .build();
        userRepository.save(userDto);
    }

}