springboot2.x版本整合redis(单机/集群)(使用lettuce)

逃亡中_      2022-05-18     791

关键词:

springboot1.x系列中,其中使用的是jedis,但是到了springboot2.x其中使用的是Lettuce。 此处springboot2.x,所以使用的是Lettuce
关于jedislettuce的区别:

  • Lettuce 和 Jedis 的定位都是Redis的client,所以他们当然可以直接连接redis server。
  • Jedis在实现上是直接连接的redis server,如果在多线程环境下是非线程安全的,这个时候只有使用连接池,为每个Jedis实例增加物理连接
  • Lettuce的连接是基于Netty的,连接实例(StatefulRedisConnection)可以在多个线程间并发访问,应为StatefulRedisConnection是线程安全的,所以一个连接实例(StatefulRedisConnection)就可以满足多线程环境下的并发访问,当然这个也是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

新建一个springboot工程,添加如下pom依赖。

技术图片
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- redis依赖commons-pool 这个依赖一定要添加 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
技术图片

然后在application.yml配置一下redis服务器的地址

技术图片
server:
  port: 1015
spring:
  redis:
    cache:
      nodes: -192.168.159.129:7001
             -192.168.159.129:7002
             -192.168.159.129:7003
             -192.168.159.129:7004
             -192.168.159.129:7005
             -192.168.159.129:7006
      host: localhost:6379
      password:
      maxIdle:
      minIdle:
      maxTotal:
      maxWaitMillis: 5000
技术图片

其中nodes为集群redis的参数 host为单机redis的参数

 

redis配置类:

 

技术图片
package webapp.conf;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class RedisConfiguration {
    @Value("${spring.redis.cache.nodes:}")
    private String nodes;
    @Value("${spring.redis.cache.host:}")
    private String host;
    @Value("${spring.redis.cache.password:}")
    private String password;
    @Value("${spring.redis.cache.maxIdle:}")
    private Integer maxIdle;
    @Value("${spring.redis.cache.minIdle:}")
    private Integer minIdle;
    @Value("${spring.redis.cache.maxTotal:}")
    private Integer maxTotal;
    @Value("${spring.redis.cache.maxWaitMillis:}")
    private Long maxWaitMillis;

    @Bean
    LettuceConnectionFactory lettuceConnectionFactory() {
        // 连接池配置
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxIdle(maxIdle == null ? 8 : maxIdle);
        poolConfig.setMinIdle(minIdle == null ? 1 : minIdle);
        poolConfig.setMaxTotal(maxTotal == null ? 8 : maxTotal);
        poolConfig.setMaxWaitMillis(maxWaitMillis == null ? 5000L : maxWaitMillis);
        LettucePoolingClientConfiguration lettucePoolingClientConfiguration = LettucePoolingClientConfiguration.builder()
                .poolConfig(poolConfig)
                .build();
        // 单机redis
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
        redisConfig.setHostName(host==null||"".equals(host)?"localhost":host.split(":")[0]);
        redisConfig.setPort(Integer.valueOf(host==null||"".equals(host)?"6379":host.split(":")[1]));
        if (password != null && !"".equals(password)) {
            redisConfig.setPassword(password);
        }

        // 哨兵redis
        // RedisSentinelConfiguration redisConfig = new RedisSentinelConfiguration();

        // 集群redis
        /*RedisClusterConfiguration redisConfig = new RedisClusterConfiguration();
        Set<RedisNode> nodeses = new HashSet<>();
        String[] hostses = nodes.split("-");
        for (String h : hostses) {
            h = h.replaceAll("s", "").replaceAll("
", "");
            if (!"".equals(h)) {
                String host = h.split(":")[0];
                int port = Integer.valueOf(h.split(":")[1]);
                nodeses.add(new RedisNode(host, port));
            }
        }
        redisConfig.setClusterNodes(nodeses);
        // 跨集群执行命令时要遵循的最大重定向数量
        redisConfig.setMaxRedirects(3);
        redisConfig.setPassword(password);*/

        return new LettuceConnectionFactory(redisConfig, lettucePoolingClientConfiguration);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(lettuceConnectionFactory);
        //序列化类
        MyRedisSerializer myRedisSerializer = new MyRedisSerializer();
        //key序列化方式
        template.setKeySerializer(myRedisSerializer);
        //value序列化
        template.setValueSerializer(myRedisSerializer);
        //value hashmap序列化
        template.setHashValueSerializer(myRedisSerializer);
        return template;
    }

    static class MyRedisSerializer implements RedisSerializer<Object> {

        @Override
        public byte[] serialize(Object o) throws SerializationException {
            return serializeObj(o);
        }

        @Override
        public Object deserialize(byte[] bytes) throws SerializationException {
            return deserializeObj(bytes);
        }

        /**
         * 序列化
         * @param object
         * @return
         */
        private static byte[] serializeObj(Object object) {
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                byte[] bytes = baos.toByteArray();
                return bytes;
            } catch (Exception e) {
                throw new RuntimeException("序列化失败!", e);
            }
        }

        /**
         * 反序列化
         * @param bytes
         * @return
         */
        private static Object deserializeObj(byte[] bytes) {
            if (bytes == null){
                return null;
            }
            ByteArrayInputStream bais = null;
            try {
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {
                throw new RuntimeException("反序列化失败!", e);
            }
        }
    }
}    
技术图片

 

以上已经完成整合教程,测试案例:

注入:

@Autowired
private RedisTemplate<String, Object> redisTemplate;

添加:

redisTemplate.opsForValue().set(key, value);

添加,设置过期时间:

redisTemplate.opsForValue().set(key, obj, expireTime, TimeUnit.SECONDS);

获取:

Object o = redisTemplate.opsForValue().get(key);

删除:

redisTemplate.delete(key);

 

springboot2.x整合redis以及连接哨兵模式/集群模式

依赖:<!--spirngboot版本为2.x--><!--加载springbootredis包,springboot2.0中直接使用jedis或者lettuce配置连接池,默认为lettuce连接池,这里使用jedis连接池--><!--加载springbootredis包--><dependency> <groupId>org.spr 查看详情

springboot2.x整合redis

#准备工作配置application.ymlspring:thymeleaf:#thymeleafcache:falsedatasource:#datasourcedriver-class-name:com.mysql.cj.jdbc.Driverurl:jdbc:mysql://localhost:3306/mp?useUnicode=true&characterEncoding=utf 查看详情

springboot2.x整合redis

pom文件<!--springboot中的redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>配置#R 查看详情

springboot2.x以上整合shiro1.7.1和redis的一些注意事项(代码片段)

因为之前用的shiro版本一直是1.4.0,后来我们老大跟我说版本太低了,不安全,叫我升级到1.7.1。升级后发现有些配置很不一样的,需要注意一下。一、pom.xml<parent><groupId>org.springframework.boot</groupId><ar... 查看详情

springboot2.x以上整合shiro1.7.1和redis的一些注意事项(代码片段)

因为之前用的shiro版本一直是1.4.0,后来我们老大跟我说版本太低了,不安全,叫我升级到1.7.1。升级后发现有些配置很不一样的,需要注意一下。一、pom.xml<parent><groupId>org.springframework.boot</groupId><ar... 查看详情

springboot2.x+shiro+redis整合填坑redis只做缓存的情况

...pringBoot+shiro已经集成完毕,如果没有集成,先查阅之前的Springboot2.0集成shiro权限管理2、redis已经安装完成3、redis客户端使用Lettuce,这也是sprinboot2.0后默认的,与jedis的区别,自行百度4、json使用springboot默认的一、依赖<dependency&... 查看详情

springboot2.x(十四):整合redis,看这一篇就够了

Redis简介Redis是一个开源的使用ANSIC语言编写、遵守BSD协议、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API的非关系型数据库(NoSQL)。Redis的特性Redis的所有操作都是原子性的,意思就是要么成... 查看详情

springboot整合thymeleaf-基于springboot2.x版本

1、为啥要用Thymeleaf模板引擎?现在不都前后端分离了么?熊dei们,别着急,我们先来谈谈为啥开始用Thymeleaf模板引擎,先照顾照顾下我们这些可爱的小白童鞋....为啥开始用Thymeleaf模板引擎?jsp她不香嘛?首先前端交给我们的页... 查看详情

springboot2.x集成单节点redis

Springboot2.x集成单节点Redis说明在Springboot1.x版本中,默认使用Jedis客户端来操作Redis,而在Springboot2.x版本中,默认使用Lettuce客户端来操作Redis。Springboot提供了RedisTemplate来统一封装了对Redis操作,开发者只需要使用RedisTemplate就可以... 查看详情

springboot2.x最佳实践《一》之springboot2.x初体验

SpringBoot2.X最佳实践前言本系列文章,从零基础接触 SpringBoot2.x新版本,基础入门使用,热部署,到整合各个主流框架Redis4.x,消息队列AciveMQ,RocketMQ等,搜索框架ElasticSearch5.6版本,到web-flux反应式编程,到Actuator监控应用信息... 查看详情

springboot2.x+maven+mybatis+shiro+redis

...的一哥们的springboot1.X整合shiro和redis的demo,我用的是springboot2.X,改了下,添加了druid和swagger2,部分,公司屏蔽git,只能上传csdn备份。那位哥们的原文找不到了,sorry。部署resource/sql/shiro.sql&# 查看详情

springboot之整合quartz调度框架-基于springboot2.0.2版本

1.项目基础项目是基于SpringBoot2.x版本的  2.添加依赖<!--quartz依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId&g 查看详情

springboot之整合quartz调度框架-基于springboot2.0.2版本

1.项目基础项目是基于SpringBoot2.x版本的  2.添加依赖<!--quartz依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId&g 查看详情

springboot+shiro+redis(单机redis版)整合教程-续(添加动态角色权限控制)

相关教程: 1. springboot+shiro整合教程 2. springboot+shiro+redis(单机redis版)整合教程 3. springboot+shiro+redis(集群redis版)整合教程 参考此教程前请先阅读2.springboot+shiro+redis(单机redis版)整合教程,此教程是在其 查看详情

springboot2.x-springboot整合amqp之rabbitmq

文章目录SpringBoot2.X-SpringBoot整合AMQP之RabbitMQRabbitMQ简介引入依赖编写配置编写接口启用Rabbit注解消息监听消息测试SpringBoot2.X-SpringBoot整合AMQP之RabbitMQSpringBoot2整合RabbitMQ案例。RabbitMQ简介简介RabbitMQ是一个由erlang开发的AMQP(AdvanvedMess... 查看详情

springboot整合redis(代码片段)

一、SpringBoot对Redis的支持Spring对Redis的支持是使用SpringDataRedis来实现的,一般使用Jedis或者lettuce(默认),Java客户端在 org.springframework.boot.autoconfigure.data.redis(SpringBoot2.x) 中redis的自动配置 AutoConfigureData 查看详情

springboot整合redis

...层:spring-dataSpringdata和springboot是齐名的项目!说明:在SpringBoot2.x之后,原来使用的jedis被替换为了lettuce?Jedis:采用的直连,多个线程操作的话,是不安全的,如果想要避免不安全的,使用jedispool连接池!更像BIO模式Lettuce:采... 查看详情

springboot2.x整合rocketmq4.x

开发生产者代码第一步:创建很普通的SpringBoot项目第二步:加入相关依赖<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-client</artifactId><version>4.3.0</version></ 查看详情