• 实现自定义SpringBoot的Starter


    starter机制

    Spring Boot Starter机制是Spring Boot项目中的一个重要概念,它主要用于简化Maven或Gradle等构建工具中的依赖管理。每个Starter都包含了实现特定功能所需的库和组件,以及相应的配置文件。开发者只需在项目中引入相应的Starter依赖,即可快速搭建起具备该功能的项目骨架。

    自定义starter好处

    简化依赖管理:与Spring Boot内置的Starter类似,自定义Starter同样可以隐藏底层库的复杂依赖关系,使开发者只需关注业务逻辑,而无需担心底层的依赖问题。这有助于降低项目的复杂性,提升开发效率。
    统一配置和自动装配:自定义Starter可以包含默认的配置信息和自动装配类,使得相关功能或组件的集成更加统一和便捷。开发者无需为每个项目重复编写相同的配置代码,只需引入自定义Starter即可实现快速集成。
    提高可复用性:通过自定义Starter,可以将一些通用的功能或组件进行封装,使得这些功能或组件可以在多个项目之间共享和复用。这不仅可以减少代码冗余,还可以确保不同项目之间的功能实现保持一致。
    扩展性和灵活性:自定义Starter允许开发者根据实际需求进行定制和扩展。开发者可以添加自己的配置项和默认配置值,以满足特定的业务需求。此外,自定义Starter还可以与其他第三方组件或库进行集成,以提供更加丰富的功能。
    易于维护:由于自定义Starter对底层依赖进行了封装和统一配置,因此在维护和升级时只需关注自定义Starter的变更,而无需对每个使用到该Starter的项目进行逐一修改。这大大简化了维护工作,提高了开发效率。
    综上所述,自定义Spring Boot Starter通过简化依赖管理、统一配置和自动装配、提高可复用性、扩展性和灵活性以及易于维护等好处,为开发者提供了一种高效、便捷的方式来构建和集成Spring Boot应用程序中的特定功能或组件。

    使用场景

    在日常开发工作中,如统一整合redis,mq,minIO,鉴权,日志等,用于框架集成,供自己和公司其他团队使用,通过简化配置来提高开发效率。

    实现原理

    具体来说,Spring Boot Starter通过自动配置(Auto-configuration)和依赖管理来实现其机制。自动配置类使用@Configuration注解进行标记,并通过条件注解(如@ConditionalOnClass、@ConditionalOnBean、@ConditionalOnProperty等)来确定是否应用自动配置。当在Spring Boot应用的pom.xml文件中添加了一个Starter的依赖时,它会触发Spring Boot的自动配置机制。自动配置类中的配置将被应用于应用程序上下文,并根据需要创建和配置相关的Bean。

    实现步骤

    源码地址:https://github.com/mikewuhao/redis-spring-boot-starter
    项目结构
    在这里插入图片描述
    RedisConfiguration

    package com.wuhao.redis.config;
    
    import com.wuhao.redis.utils.RedisUtils;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.util.StringUtils;
    import redis.clients.jedis.JedisPoolConfig;
    
    /**
     * Copyright 2022 skyworth
     *
     * @Author: wuhao
     * @CreateTime: 2024-04-22 11:51
     * @Description: redis配置类
     * @Version: 1.0
     **/
    @Configuration
    @EnableConfigurationProperties(RedisProperties.class)
    public class RedisConfiguration {
    
        private final RedisProperties properties;
    
        public RedisConfiguration(RedisProperties redisProperties) {
            this.properties = redisProperties;
        }
    
        @Bean(name = "redisHandler")
        @ConditionalOnMissingBean(RedisUtils.class)
        public RedisUtils redisUtils() {
            JedisPoolConfig poolConfig = new JedisPoolConfig();
            //最大连接数
            poolConfig.setMaxTotal(properties.getMaxTotal());
            //最多空闲数
            poolConfig.setMaxIdle(properties.getMaxIdLe());
            //当池中没有连接时,最多等待5秒
            poolConfig.setMaxWaitMillis(properties.getMaxWaitMillis());
            String pw = StringUtils.isEmpty(properties.getPassword()) ? null : properties.getPassword();
            return new RedisUtils(poolConfig, properties.getHost(), properties.getPort(), properties.getTimeOut(), pw, properties.getDatabase());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    RedisProperties

    
    package com.wuhao.redis.config;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    /**
     * Copyright 2022 skyworth
     *
     * @Author: wuhao
     * @CreateTime: 2024-04-22 11:51
     * @Description: redis配置属性
     * @Version: 1.0
     **/
    @ConfigurationProperties("spring.redis")
    public class RedisProperties {
    
        @Value("${host:127.0.0.1}")
        private String host;
    
        @Value("${port:6379}")
        private int port;
    
        @Value("${database:0}")
        private int database;
    
        @Value("${password:}")
        private String password;
    
        @Value("${timeOut:3000}")
        private int timeOut;
    
        @Value("${maxTotal:10000}")
        private int maxTotal;
    
        @Value("${maxIdLe:50}")
        private int maxIdLe;
    
        @Value("${maxWaitMillis:5000}")
        private long maxWaitMillis;
    
        public String getHost() {
            return host;
        }
    
        public void setHost(String host) {
            this.host = host;
        }
    
        public int getPort() {
            return port;
        }
    
        public void setPort(int port) {
            this.port = port;
        }
    
        public int getDatabase() {
            return database;
        }
    
        public void setDatabase(int database) {
            this.database = database;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public int getTimeOut() {
            return timeOut;
        }
    
        public void setTimeOut(int timeOut) {
            this.timeOut = timeOut;
        }
    
        public int getMaxTotal() {
            return maxTotal;
        }
    
        public void setMaxTotal(int maxTotal) {
            this.maxTotal = maxTotal;
        }
    
        public int getMaxIdLe() {
            return maxIdLe;
        }
    
        public void setMaxIdLe(int maxIdLe) {
            this.maxIdLe = maxIdLe;
        }
    
        public long getMaxWaitMillis() {
            return maxWaitMillis;
        }
    
        public void setMaxWaitMillis(long maxWaitMillis) {
            this.maxWaitMillis = maxWaitMillis;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    RedisUtils

    package com.wuhao.redis.utils;
    
    
    import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    import redis.clients.jedis.Pipeline;
    import redis.clients.jedis.Tuple;
    
    import java.util.*;
    
    /**
     * Copyright 2022 skyworth
     *
     * @Author: wuhao
     * @CreateTime: 2024-04-22 11:51
     * @Description: redis工具类
     * @Version: 1.0
     **/
    public final class RedisUtils {
    
        /*
        除了该工具类提供的方法外,还可以在外面调用getJedis()方法,获取到jedis实例后,调用它原生的api来操作
         */
        private final JedisPool jedisPool;
    
        static final Long OPERATE_SUCCESS = 1L;
    
        /**
         * 定义获取锁的lua脚本
         */
        static final String LOCK_LUA_SCRIPT = "if redis.call('setnx', KEYS[1], ARGV[1]) == 1 then return redis.call('expire', KEYS[1], ARGV[2]) else return 0 end";
    
        /**
         * 定义释放锁的lua脚本
         */
        static final String UNLOCK_LUA_SCRIPT = "if redis.call('get',KEYS[1]) == ARGV[1] then return redis.call('del',KEYS[1]) else return -1 end";
    
        /**
         * 获取jedis对象,并选择redis库。jedis默认是0号库,可传入1-16之间的数选择库存放数据
         * 原则上使用一个redis库存放数据,通过特定的key的命令规则来区分不同的数据就行了。
         *
         * @param index redis库号。使用可变参数的目的就是该参数可传可不传。
         * @return 返回jedis对象
         */
        public Jedis getJedis(int... index) {
    
            Jedis jedis = jedisPool.getResource();
            if (index != null && index.length > 0) {
                if (index[0] > 0 && index[0] <= 16) {
                    jedis.select(index[0]);
                }
            }
            return jedis;
    
        }
    
        /*########################  key的操作  ################################*/
    
    
        /**
         * 删除一个或多个key
         *
         * @param key 一个或多个key
         */
        public Long del(String... key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.del(key);
            }
    
        }
    
        /**
         * 批量删除
         *
         * @param keyList 要删除的key的集合
         */
        public void mDel(List<String> keyList) {
    
            Jedis jedis = getJedis();
            //获取pipeline
            Pipeline pipeline = jedis.pipelined();
            for (String key : keyList) {
                pipeline.del(key);
            }
            //执行结果同步,这样才能保证结果的正确性。实际上不执行该方法也执行了上面的命令,但是结果确不一定完全正确。
            //注意
            pipeline.sync();
            //关闭连接
            jedis.close();
    
        }
    
        /**
         * 判断某个key是否还存在
         *
         * @param key key
         */
        public Boolean exists(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.exists(key);
            }
    
        }
    
        /**
         * 设置某个key的过期时间,单位秒
         *
         * @param key     key
         * @param seconds 过期时间秒
         */
        public void expire(String key, int seconds) {
    
            try (Jedis jedis = getJedis()) {
                jedis.expire(key, seconds);
            }
    
        }
    
        /**
         * 设置某个key的过期时间,单位秒
         *
         * @param key     key
         * @param seconds 过期时间秒
         */
        public void expire(String key, Long seconds) {
    
            try (Jedis jedis = getJedis()) {
                jedis.expire(key, seconds.intValue());
            }
    
        }
    
        /**
         * 设置某个key的过期时间,单位毫秒
         *
         * @param key     key
         * @param seconds 过期时间秒
         */
        public void expireAt(String key, long seconds) {
    
            try (Jedis jedis = getJedis()) {
                jedis.expireAt(key, seconds);
            }
    
        }
    
        /**
         * 查看某个key还有几秒过期,-1表示永不过期 ,-2表示已过期
         */
        public Long ttl(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.ttl(key);
            }
    
        }
    
        /**
         * 查看某个key对应的value的类型
         */
        public String type(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.type(key);
            }
    
        }
    
        /*########################  string(字符串)的操作  ####################*/
    
        /**
         * 获取某个key的value,类型要对,只能value是string的才能获取
         */
        public String get(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.get(key);
            }
    
        }
    
        /**
         * 设置某个key的value
         */
        public String set(String key, String value) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.set(key, value);
            }
    
        }
    
        /**
         * 设置某个key的value
         */
        public String set(String key, String value, int expireSeconds) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.setex(key, expireSeconds, value);
            }
    
        }
    
        /**
         * 设置某个key的value
         */
        public String set(String key, String value, Long expireSeconds) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.setex(key, expireSeconds.intValue(), value);
            }
    
        }
    
        public String set(String key, String value, String nxxx, String expx, long time) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.set(key, value, nxxx, expx, time);
            }
    
        }
    
        /**
         * 字符串后追加内容
         *
         * @param key           key
         * @param appendContent 要追加的内容
         */
        public Long append(String key, String appendContent) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.append(key, appendContent);
            }
    
        }
    
        /**
         * 返回key的value的长度
         */
        public Long strLen(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.strlen(key);
            }
    
        }
    
        /**
         * value 加1 必
         * 须是字符型数字
         */
        public Long incr(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.incr(key);
            }
    
        }
    
        /**
         * value
         * 须是字符型数字
         */
        public Long incr(String key, int num) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.incrBy(key, num);
            }
    
        }
    
        /**
         * value 减1   必须是字符型数字
         */
        public Long decr(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.decr(key);
            }
    
        }
    
        /**
         * value 加increment
         */
        public Long incrBy(String key, int increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.incrBy(key, increment);
            }
    
        }
    
        public Double incrByFloat(String key, double increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.incrByFloat(key, increment);
    
            }
        }
    
        /**
         * value 加increment
         */
        public Long incrBy(String key, Long increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.incrBy(key, increment);
            }
    
        }
    
        /**
         * value 减increment
         */
        public Long decrBy(String key, int increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.decrBy(key, increment);
            }
    
        }
    
        /**
         * 给某个key设置过期时间和value,成功返回OK
         *
         * @param key     key
         * @param seconds 过期时间秒
         * @param value   设置的值
         */
        public String setEx(String key, int seconds, String value) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.setex(key, seconds, value);
            }
    
        }
    
        /*########################  list(列表)的操作  #######################*/
    
        /**
         * 从左边向列表中添加值
         */
        public void lPush(String key, String str) {
    
            try (Jedis jedis = getJedis()) {
                jedis.lpush(key, str);
            }
    
        }
    
        /**
         * 从左边向列表中添加值
         */
        public void lPushAll(String key, String... strings) {
    
            try (Jedis jedis = getJedis()) {
                jedis.lpush(key, strings);
            }
    
        }
    
        public void rPushAll(String key, String... strings) {
    
            try (Jedis jedis = getJedis()) {
                jedis.rpush(key, strings);
            }
    
        }
    
        /**
         * 从右边向列表中添加值
         */
        public void rPush(String key, String str) {
    
            try (Jedis jedis = getJedis()) {
                jedis.rpush(key, str);
            }
    
        }
    
        /**
         * 从左边取出一个列表中的值
         */
        public String lPop(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.lpop(key);
            }
    
        }
    
        /**
         * 从右边取出一个列表中的值
         */
        public String rPop(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.rpop(key);
            }
    
        }
    
        /**
         * 取出列表中指定范围内的值,0 到 -1 表示全部
         */
        public List<String> lRange(String key, int startIndex, int endIndex) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.lrange(key, startIndex, endIndex);
            }
    
        }
    
        /**
         * 返回某列表指定索引位置的值
         */
        public String lIndex(String key, int index) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.lindex(key, index);
            }
    
        }
    
        /**
         * 返回某列表的长度
         */
        public Long lLen(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.llen(key);
            }
    
        }
    
        /**
         * 给某列表指定位置设置为指定的值
         */
        public String lSet(String key, Long index, String str) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.lset(key, index, str);
            }
    
        }
    
        /**
         * 对列表进行剪裁,保留指定闭区间的元素(索引位置也会重排)
         */
        public void ltrim(String key, Integer startIndex, Integer endIndex) {
    
            try (Jedis jedis = getJedis()) {
                jedis.ltrim(key, startIndex, endIndex);
            }
    
        }
    
        /**
         * 从列表的左边阻塞弹出一个元素
         */
        public List<String> blpop(String key, Integer timeout) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.blpop(timeout, key);
            }
    
        }
    
        /**
         * 从列表的右边阻塞弹出一个元素
         */
        public List<String> brpop(String key, Integer timeout) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.brpop(timeout, key);
            }
    
        }
    
        /*########################  hash(哈希表)的操作  #######################*/
        //hset hget hmset hmget hgetall hdel hkeys hvals hexists hincrby
    
        /**
         * 给某个hash表设置一个键值对
         */
        public void hset(String key, String field, String value) {
    
            try (Jedis jedis = getJedis()) {
                jedis.hset(key, field, value);
            }
    
        }
    
        /**
         * 取出某个hash表中某个field对应的value
         */
        public String hget(String key, String field) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hget(key, field);
            }
    
        }
    
        /**
         * 某个hash表设置一个或多个键值对
         */
        public void hmset(String key, Map<String, String> kvMap) {
    
            try (Jedis jedis = getJedis()) {
                jedis.hmset(key, kvMap);
            }
    
        }
    
        /**
         * 取出某个hash表中任意多个key对应的value的集合
         */
        public List<String> hmget(String key, String... fields) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hmget(key, fields);
            }
    
        }
    
        /**
         * 取出某个hash表中所有的键值对
         */
        public Map<String, String> hgetAll(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hgetAll(key);
            }
    
        }
    
        /**
         * 判断某个hash表中的某个key是否存在
         */
        public Boolean hexists(String key, String field) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hexists(key, field);
            }
    
        }
    
        /**
         * 返回某个hash表中所有的key
         */
        public Set<String> hkeys(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hkeys(key);
            }
    
        }
    
        /**
         * 返回某个hash表中所有的value
         */
        public List<String> hvals(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hvals(key);
            }
    
        }
    
        /**
         * 删除某个hash表中的一个或多个键值对
         */
        public Long hdel(String key, String... fields) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hdel(key, fields);
            }
    
        }
    
        /**
         * 给某个hash表中的某个field的value增加多少
         */
        public Long hincrBy(String key, String field, Long increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hincrBy(key, field, increment);
            }
    
        }
    
        /**
         * 给某个hash表中的某个field的value增加多少
         */
        public Long hincrBy(String key, String field, Integer increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hincrBy(key, field, increment);
            }
    
        }
    
        /**
         * 给某个hash表中的某个field的value增加多少
         */
        public Long hdecrBy(String key, String field, Integer increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hincrBy(key, field, -increment);
            }
    
        }
    
        public Double hincrByFloat(String key, String field, Double increment) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.hincrByFloat(key, field, increment);
            }
    
        }
    
        /*########################  set(集合)的操作  ###########################*/
    
        /**
         * 往set集合中添加一个或多个元素
         */
        public Long sadd(String key, String... members) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.sadd(key, members);
            }
    
        }
    
        /**
         * 返回set集合中的所有元素,顺序与加入时的顺序一致
         */
        public Set<String> smembers(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.smembers(key);
            }
    
        }
    
        /**
         * 判断集合中是否存在某个元素
         */
        public Boolean sismember(String key, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.sismember(key, member);
            }
    
        }
    
        /**
         * 返回set集合的长度
         */
        public Long scard(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.scard(key);
            }
    
        }
    
        /**
         * 删除set集合中指定的一个或多个元素
         */
        public Long srem(String key, String... members) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.srem(key, members);
            }
    
        }
    
        /**
         * 将key1中的元素key1Member移动到key2中
         */
        public Long smove(String key1, String key2, String key1Member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.smove(key1, key2, key1Member);
            }
    
        }
    
        /**
         * 随机查询返回集合中的指定个数的元素(若count为负数,返回的元素可能会重复)
         */
        public List<String> srandmember(String key, int count) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.srandmember(key, count);
            }
    
        }
    
        /**
         * 从set集合中随机弹出指定个数个元素
         *
         * @param key   key
         * @param count 要弹出的个数
         * @return 随机弹出的元素
         */
        public Set<String> spop(String key, int count) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.spop(key, count);
            }
    
        }
    
        /**
         * 求交集,返回多个set集合相交的部分
         */
        public Set<String> sinter(String... setKeys) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.sinter(setKeys);
            }
    
        }
    
        /**
         * 求并集,求几个set集合的并集(因为set中不会有重复的元素,合并后的集合也不会有重复的元素)
         */
        public Set<String> sunion(String... setKeys) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.sunion(setKeys);
            }
    
        }
    
        /**
         * 求差集,求几个集合之间的差集
         */
        public Set<String> sdiff(String... setKeys) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.sdiff(setKeys);
            }
    
        }
    
    
        /*########################  zset(有序集合)的操作  #######################*/
    
        /**
         * 添加一个元素到zset
         */
        public Long zadd(String key, double score, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zadd(key, score, member);
            }
    
        }
    
        /**
         * 添加一个或多个元素到zset
         */
        public Long zadd(String key, Map<String, Double> memberScores) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zadd(key, memberScores);
            }
    
        }
    
        /**
         * 查询指定闭区间的元素(根据分数升序)
         * (0,-1表示全部)
         */
        public Set<String> zrange(String key, long start, long end) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrange(key, start, end);
            }
    
        }
    
        /**
         * 查询指定闭区间的元素,带着分数
         * (0,-1表示全部)
         */
        public Set<Tuple> zrangeWithScores(String key, long start, long end) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrangeWithScores(key, start, end);
            }
    
        }
    
        /**
         * 查询指定索引闭区间的元素(根据分数降序)
         * (0,-1表示全部)
         */
        public Set<String> zrevrange(String key, long start, long end) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrange(key, start, end);
            }
    
        }
    
        /**
         * 查询指定索引闭区间的元素,带着分数(根据分数降序)
         * (0,-1表示全部)
         */
        public Set<Tuple> zrevrangeWithScores(String key, long start, long end) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrangeWithScores(key, start, end);
            }
    
        }
    
        /**
         * 返回有序集合(zset)中的元素个数
         */
        public Long zcard(String key) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zcard(key);
            }
    
        }
    
        /**
         * 返回指定分数区间的元素个数(闭区间)
         */
        public Long zcount(String key, Long startScore, Long endScore) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zcount(key, startScore, endScore);
            }
    
        }
    
        /**
         * 返回某元素在集合中的排名(根据分数降序排列时)
         */
        public Long zrevrank(String key, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrank(key, member);
            }
    
        }
    
        /**
         * 返回某元素在集合中的排名(根据分数升序排列时)
         */
        public Long zrank(String key, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrank(key, member);
            }
    
        }
    
        /**
         * 升序查询指定分数闭区间的元素
         */
        public Set<String> zrangeByScore(String key, double min, double max) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrangeByScore(key, min, max);
            }
    
        }
    
        /**
         * 升序查询指定分数闭区间的元素,并指定偏移量
         */
        public Set<String> zrangeByScore(String key, double min, double max, int offset, int size) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrangeByScore(key, min, max, offset, size);
            }
    
        }
    
        /**
         * 升序查询指定分数闭区间的元素,带着分数
         */
        public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrangeByScoreWithScores(key, min, max);
            }
    
        }
    
        /**
         * 升序查询指定分数闭区间的元素,并指定偏移量,带着分数
         */
        public Set<Tuple> zrangeByScoreWithScores(String key, double min, double max, int offset, int size) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrangeByScoreWithScores(key, min, max, offset, size);
            }
    
        }
    
        /**
         * 降序查询指定分数闭区间的元素
         */
        public Set<String> zrevrangebyscore(String key, double max, double min) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrangeByScore(key, max, min);
            }
    
        }
    
        /**
         * 降序查询指定分数闭区间的元素,并指定偏移量
         */
        public Set<String> zrevrangebyscore(String key, double max, double min, int offset, int size) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrangeByScore(key, max, min, offset, size);
            }
    
        }
    
        /**
         * 降序查询指定分数闭区间的元素,并带着分数
         */
        public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrangeByScoreWithScores(key, max, min);
            }
    
        }
    
        /**
         * 降序查询指定分数闭区间的元素,并指定偏移量,带着分数
         */
        public Set<Tuple> zrevrangeByScoreWithScores(String key, double max, double min, int offset, int size) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrevrangeByScoreWithScores(key, min, max, offset, size);
            }
    
        }
    
        /**
         * 有序集合中指定删除一或多个元素
         */
        public Long zrem(String key, String... member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zrem(key, member);
            }
    
        }
    
        /**
         * 分数升序排名时,删除指定索引区间的元素
         */
        public Long zremrangebyrank(String key, long start, long end) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zremrangeByRank(key, start, end);
            }
    
        }
    
        /**
         * 分数升序排名时,删除指定分数区间的元素
         */
        public Long zremrangeByScore(String key, long min, long max) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zremrangeByScore(key, min, max);
            }
    
        }
    
        /**
         * 查询有序集合中某元素的分数
         */
        public Double zscore(String key, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zscore(key, member);
            }
    
        }
    
        /**
         * 给有序集合中某元素的分数增加(正数)或减少(负数)
         */
        public Double zincrby(String key, double score, String member) {
    
            try (Jedis jedis = getJedis()) {
                return jedis.zincrby(key, score, member);
            }
    
        }
    
        /*########################  lock 相关  #######################*/
    
        /**
         * 获取一把锁
         */
        public boolean lock(String key, String lockValue, int expire) {
    
            if (key == null || lockValue == null) {
                return false;
            }
            try (Jedis jedis = getJedis()) {
                List<String> args = new ArrayList<>();
                args.add(lockValue);
                args.add(String.valueOf(expire));
                Object res = jedis.eval(LOCK_LUA_SCRIPT, Collections.singletonList(key), args);
                return res != null && res.equals(OPERATE_SUCCESS);
            } catch (Exception e) {
                return false;
            }
    
        }
    
        /**
         * 获取一把锁
         */
        public boolean lock(String key, String lockValue, long expire) {
    
            if (key == null || lockValue == null) {
                return false;
            }
            try (Jedis jedis = getJedis()) {
                List<String> args = new ArrayList<>();
                args.add(lockValue);
                args.add(String.valueOf(expire));
                Object res = jedis.eval(LOCK_LUA_SCRIPT, Collections.singletonList(key), args);
                return res != null && res.equals(OPERATE_SUCCESS);
            } catch (Exception e) {
                return false;
            }
    
        }
    
        public boolean releaseLock(String key, String lockValue) {
    
            if (key == null || lockValue == null) {
                return false;
            }
            try (Jedis jedis = getJedis()) {
                Object res = jedis.eval(UNLOCK_LUA_SCRIPT, Collections.singletonList(key), Collections.singletonList(lockValue));
                return res != null && res.equals(OPERATE_SUCCESS);
            } catch (Exception e) {
                return false;
            }
    
        }
    
        /**
         * 私有化构造器,不让实例化对象
         */
        public RedisUtils(final GenericObjectPoolConfig poolConfig, final String host, int port, int timeout, final String password, int database) {
            this.jedisPool = new JedisPool(poolConfig, host, port, timeout, password, database);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
    • 734
    • 735
    • 736
    • 737
    • 738
    • 739
    • 740
    • 741
    • 742
    • 743
    • 744
    • 745
    • 746
    • 747
    • 748
    • 749
    • 750
    • 751
    • 752
    • 753
    • 754
    • 755
    • 756
    • 757
    • 758
    • 759
    • 760
    • 761
    • 762
    • 763
    • 764
    • 765
    • 766
    • 767
    • 768
    • 769
    • 770
    • 771
    • 772
    • 773
    • 774
    • 775
    • 776
    • 777
    • 778
    • 779
    • 780
    • 781
    • 782
    • 783
    • 784
    • 785
    • 786
    • 787
    • 788
    • 789
    • 790
    • 791
    • 792
    • 793
    • 794
    • 795
    • 796
    • 797
    • 798
    • 799
    • 800
    • 801
    • 802
    • 803
    • 804
    • 805
    • 806
    • 807
    • 808
    • 809
    • 810
    • 811
    • 812
    • 813
    • 814
    • 815
    • 816
    • 817
    • 818
    • 819
    • 820
    • 821
    • 822
    • 823
    • 824
    • 825
    • 826
    • 827
    • 828
    • 829
    • 830
    • 831
    • 832
    • 833
    • 834
    • 835
    • 836
    • 837
    • 838
    • 839
    • 840
    • 841
    • 842
    • 843
    • 844
    • 845
    • 846
    • 847
    • 848
    • 849
    • 850
    • 851
    • 852
    • 853
    • 854
    • 855
    • 856
    • 857
    • 858
    • 859
    • 860
    • 861
    • 862
    • 863
    • 864
    • 865
    • 866
    • 867
    • 868
    • 869
    • 870
    • 871
    • 872
    • 873
    • 874
    • 875
    • 876
    • 877
    • 878
    • 879
    • 880
    • 881
    • 882
    • 883
    • 884
    • 885
    • 886
    • 887
    • 888
    • 889
    • 890
    • 891
    • 892
    • 893
    • 894
    • 895
    • 896
    • 897
    • 898
    • 899
    • 900
    • 901
    • 902
    • 903
    • 904
    • 905
    • 906
    • 907
    • 908
    • 909
    • 910
    • 911
    • 912
    • 913
    • 914
    • 915
    • 916
    • 917
    • 918
    • 919
    • 920
    • 921
    • 922
    • 923
    • 924
    • 925
    • 926
    • 927
    • 928
    • 929
    • 930
    • 931
    • 932
    • 933
    • 934
    • 935
    • 936
    • 937
    • 938
    • 939
    • 940
    • 941
    • 942
    • 943
    • 944
    • 945
    • 946
    • 947
    • 948
    • 949
    • 950
    • 951
    • 952
    • 953
    • 954
    • 955
    • 956
    • 957
    • 958
    • 959
    • 960
    • 961
    • 962
    • 963
    • 964
    • 965
    • 966
    • 967
    • 968
    • 969
    • 970
    • 971
    • 972
    • 973
    • 974
    • 975
    • 976
    • 977
    • 978
    • 979
    • 980
    • 981
    • 982
    • 983
    • 984
    • 985
    • 986
    • 987
    • 988
    • 989
    • 990
    • 991
    • 992
    • 993
    • 994
    • 995
    • 996
    • 997
    • 998
    • 999
    • 1000
    • 1001
    • 1002
    • 1003
    • 1004
    • 1005
    • 1006
    • 1007
    • 1008
    • 1009
    • 1010
    • 1011
    • 1012
    • 1013
    • 1014
    • 1015
    • 1016
    • 1017
    • 1018
    • 1019
    • 1020
    • 1021
    • 1022
    • 1023
    • 1024
    • 1025
    • 1026
    • 1027
    • 1028
    • 1029
    • 1030
    • 1031
    • 1032
    • 1033
    • 1034
    • 1035
    • 1036
    • 1037
    • 1038
    • 1039
    • 1040
    • 1041
    • 1042
    • 1043
    • 1044
    • 1045
    • 1046
    • 1047
    • 1048
    • 1049
    • 1050
    • 1051
    • 1052
    • 1053
    • 1054
    • 1055
    • 1056
    • 1057
    • 1058
    • 1059
    • 1060
    • 1061
    • 1062
    • 1063
    • 1064
    • 1065
    • 1066
    • 1067
    • 1068
    • 1069
    • 1070
    • 1071
    • 1072
    • 1073
    • 1074
    • 1075
    • 1076
    • 1077
    • 1078

    spring.factories

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wuhao.redis.config.RedisConfiguration
    
    • 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>com.wuhaogroupId>
        <artifactId>redis-spring-boot-starterartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <revision>2.0.2.RELEASErevision>
            <redis.revision>2.9.0redis.revision>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-data-redisartifactId>
                <version>${revision}version>
            dependency>
            <dependency>
                <groupId>redis.clientsgroupId>
                <artifactId>jedisartifactId>
                <version>${redis.revision}version>
            dependency>
        dependencies>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    对starter打jar包
    先clean,再install安装到本地仓库
    在这里插入图片描述
    外部引用
    pom文件加入依赖的starter的jar包
    在这里插入图片描述
    配置文件填写redis的配置信息
    在这里插入图片描述
    注入依赖redis工具类,写业务代码
    在这里插入图片描述

  • 相关阅读:
    Java中树形菜单的实现方式(超全详解!)
    友菜友饭荣获“互联网最具投资价值品牌”殊荣,掀起私厨到家新时代
    HTML学生个人网站作业设计成品 HTML+CSS肖战明星人物介绍网页 web结课作业的源码
    过滤器,计算属性,属性侦听器
    Python学习七(线程)
    ambari安装 本地源
    Redis下载安装教程 (windows)
    【代码】Python3|用Python PIL压缩图片至指定大小,并且不自动旋转
    SSM - Springboot - MyBatis-Plus 全栈体系(十)
    互联网Java工程师面试题·Java 总结篇·第五弹
  • 原文地址:https://blog.csdn.net/mikewuhao/article/details/138080452