SpringBoot 2.0 整合Jedis

Jedis介绍

jedis是封装了redis的java客户端,提供了更简单的redis操作API,同时SpringBoot也将redis做了封装,但是用来直接操作redis步骤繁琐。

redis配置文件

1 . 引入redis

<dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

2 . 配置redis连接属性

说明:SpringBoot会根据application.properties中的配置文件,对SpringBoot整合的redis进行自动的配置,将属性文件自动注入到org.springframework.boot.autoconfigure.data.redis.RedisProperties类中,

        #redis配置
        ################################################

        #redis数据库索引(默认为0)
        spring.redis.database=0
        #redis服务器IP地址
        spring.redis.host=
        #redis端口号
        spring.redis.port=6379
        #redis密码,默认为空
        spring.redis.password=
        #连接redis超时时间(毫秒)
        spring.redis.time-out=0ms

        #jedis连接池
        ###############################
        #最大等待时间
        spring.redis.jedis.pool.max-wait=1000ms
        #最小空闲数量
        spring.redis.jedis.pool.min-idle=1
        #最大空闲数量
        spring.redis.jedis.pool.max-idle=10
        #最大连接数量
        spring.redis.jedis.pool.max-active=1000

3 . 关于RedisProperties类的源码分析:

package org.springframework.boot.autoconfigure.data.redis;

import java.time.Duration;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * Configuration properties for Redis.(redis的属性配置类)
 */
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    /**
     * Database index used by the connection factory.
     */
    private int database = 0;

    /**
     * Connection URL. Overrides host, port, and password. User is ignored. Example:
     * redis://user:[email protected]:6379
     */
    private String url;

    /**
     * Redis server host.
     */
    private String host = "localhost";

    /**
     * Login password of the redis server.
     */
    private String password;

    /**
     * Redis server port.
     */
    private int port = 6379;

    /**
     * Whether to enable SSL support.
     */
    private boolean ssl;

    /**
     * Connection timeout.
     */
    private Duration timeout;

    private Sentinel sentinel;

    private Cluster cluster;

    private final Jedis jedis = new Jedis();

    private final Lettuce lettuce = new Lettuce();

    /**
    *此处省略了所有的get set方法
    */

    /**
     * Pool properties.(连接池的配置信息)
     */
    public static class Pool {

        /**
         * Maximum number of "idle" connections in the pool. Use a negative value to
         * indicate an unlimited number of idle connections.
         */
        private int maxIdle = 8;

        /**
         * Target for the minimum number of idle connections to maintain in the pool. This
         * setting only has an effect if it is positive.
         */
        private int minIdle = 0;

        /**
         * Maximum number of connections that can be allocated by the pool at a given
         * time. Use a negative value for no limit.
         */
        private int maxActive = 8;

        /**
         * Maximum amount of time a connection allocation should block before throwing an
         * exception when the pool is exhausted. Use a negative value to block
         * indefinitely.
         */
        private Duration maxWait = Duration.ofMillis(-1);
        /**
        *省略了关于连接池属性信息的get set方法
        */
    }

    /**
     * Cluster properties.(集群配置信息)
     */
    public static class Cluster {

        /**
         * Comma-separated list of "host:port" pairs to bootstrap from. This represents an
         * "initial" list of cluster nodes and is required to have at least one entry.
         */
        private List nodes;

        /**
         * Maximum number of redirects to follow when executing commands across the
         * cluster.
         */
        private Integer maxRedirects;
        /**
        *省略了关于集群配置信息的get set方法
        */

    }

    /**
     * Redis sentinel properties.(哨兵属性信息)
     */
    public static class Sentinel {

        /**
         * Name of the Redis server.
         */
        private String master;

        /**
         * Comma-separated list of "host:port" pairs.
         */
        private List nodes;
        /**
        *省略了关于哨兵属性信息的get set方法
        */

    }

    /**
     * Jedis client properties.(redis的客户端jedis)
     */
    public static class Jedis {

        /**
         * Jedis pool configuration.
         */
        private Pool pool;
        /**
        *省略了关于jedis属性信息的get set方法
        */

    }

    /**
     * Lettuce client properties.
     */
    public static class Lettuce {

        /**
         * Shutdown timeout.
         */
        private Duration shutdownTimeout = Duration.ofMillis(100);

        /**
         * Lettuce pool configuration.
         */
        private Pool pool;
    }

}

Jedis配置

1 . 引入jedis客户端依赖

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.9.0version>
        dependency>

2 . 创建JedisPoolFactory类,用来配置JedisPool属性信息,以及创建RedisPool

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisPoolFactory {

    //自动注入redis配置属性文件
    @Autowired
    private RedisProperties properties;

    @Bean
    public JedisPool getJedisPool(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(properties.getJedis().getPool().getMaxIdle());
        config.setMaxTotal(properties.getJedis().getPool().getMaxActive());
        config.setMaxWaitMillis(properties.getJedis().getPool().getMaxWait().toMillis());
        JedisPool pool = new JedisPool(config,properties.getHost(),properties.getPort(),100);
        return pool;
    }
}

通过SpringBoot容器来自动创建以及注入JedisPool

    @Autowired
    private JedisPool jedisPool;

至此,便可以使用jedisPool.getResource();方法来获取Jedis来操作redis数据库了

你可能感兴趣的:(redis学习总结,SpringBoot)