SpringBoot集成Redis

引入依赖

建议使用 Lettuce 连接驱动

		<!--
         一:
         Jedis连接驱动缺点:
         Jedis基于TCP的阻塞性的连接方式
         1. 阻塞性IO
         2. 不能异步
         3. 线程不安全的

         Lettuce连接驱动优点:
         Lettuce基于Netty的多路复用的异步非阻塞的连接方式,
         1. 线程安全
         2. 基于Netty框架的事件驱动通信,可以异步的
         3. 适合于分布式的缓存。

         二:
         Springboot 1.x 默认redis驱动使用Jedis
         Springboot 2.x 默认redis驱动使用Lettuce

          Jedis驱动使用连接池是 commons-pool2
          Lettuce驱动使用连接池也是 commons-pool2
         -->

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

        <!-- 显式导入 commons-pool2 连接池 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

配置文件

spring:
  redis:
    # 集群
    cluster:
      # 集群地址
      nodes:
        redis:7000,redis:7001,redis:7002,redis:7003,redis:7004,redis:7005 # redis换成ip

    # 连接驱动
    lettuce:
      # 连接池配置
      pool:
        # 最大空闲连接
        max-idle: 8
        # 最大连接数
        max-active: 8
        # 最小空闲连接
        min-idle: 2
        # 最大建立连接等待时间
        max-wait: 1ms

配置类

配置文件读取类

@ConfigurationProperties(prefix = "spring.redis.cluster")
@Component
@Data
public class RedisProperties {

     // 集群节点列表
    private String nodes;

}
/**
 *  Redis连接池配置信息读取类
 */
@Component
@ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
@Data
public class RedisPoolProperties {

    // 最大空闲连接
    private Integer maxIdle;
    // 最小空闲连接
    private Integer minIdle;
    // 最大连接数
    private Integer maxActive;
    // 最大建立连接等待时间
    private Duration maxWait;
}

配置文件Config

@Configuration
public class RedisConfig {

    // Redis配置信息读取类
    @Resource
    private RedisProperties redisProperties;

    // 连接池配置信息读取类
    @Resource
    private RedisPoolProperties redisPoolProperties;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

        // key,value序列化配置
        /* **********************
         *
         * 常用的RedisSerializer
         *
         * 1. Jackson2JsonRedisSerializer
         * 2. GenericFastJsonRedisSerializer 或 FastJsonRedisSerializer (fastJson2)
         * 3. StringRedisSerializer (只能支持String)
         * *********************/

        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        // string key & value
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);

        // hash key & value	
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(genericFastJsonRedisSerializer);

        // 连接驱动的配置
        redisTemplate.setConnectionFactory(lettuceConnectionFactory());

        return redisTemplate;
    }

    /**
     * Lettuce连接驱动的配置
     */
    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory() {

        // redis集群的配置
        Map<String, Object> map = new HashMap<>();
        map.put("spring.redis.cluster.nodes", redisProperties.getNodes());

        RedisClusterConfiguration redisClusterConfiguration =
                new RedisClusterConfiguration(
                        new MapPropertySource(
                                "RedisClusterConfiguration",
                                map)
                );

        /* **********************
         *
         * redis集群方式的不同,需要使用对应的Redis配置实现类
         *
         * RedisClusterConfiguration: 集群
         * RedisSentinelConfiguration: 哨兵
         * RedisStandaloneConfiguration:单机
         *
         * *********************/

        // Lettuce连接池的配置
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxWait(redisPoolProperties.getMaxWait());
        genericObjectPoolConfig.setMinIdle(redisPoolProperties.getMinIdle());
        genericObjectPoolConfig.setMaxTotal(redisPoolProperties.getMaxActive());
        genericObjectPoolConfig.setMaxWait(redisPoolProperties.getMaxWait());

        LettucePoolingClientConfiguration lettucePoolingClientConfiguration =
                LettucePoolingClientConfiguration.builder()
                        .poolConfig(genericObjectPoolConfig)
                        .build();


        return new LettuceConnectionFactory(
                redisClusterConfiguration,
                lettucePoolingClientConfiguration);
    }
}

工具类

/**
 * Redis工具类
 */
@Component
public class RedisUtil {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    public void stringSet(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }


    public Object stringGet(String key) {
        return redisTemplate.opsForValue().get(key);
    }


    public void hashSet(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key, map);
    }

    public Object hashGet(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key, hashKey);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不进大厂不改名二号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值