SpringBoot中配置Redis连接池的完整指南

2025-05-14 10:28:07 103
魁首哥

一、添加依赖

在 pom.xml 文件中添加以下依赖:


    
        org.springframework.boot
        spring-boot-starter-data-redis
    
    
        org.apache.commons
        commons-pool2
    

二、配置 redis 连接池

(一)通过 java 配置类

创建一个配置类,用于定义 redis 连接工厂和连接池配置:

import org.apache.commons.pool2.impl.genericobjectpoolconfig;
import org.springframework.cache.annotation.cachingconfigurersupport;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
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.stringredisserializer;

@enablecaching
@configuration
public class redisconfig extends cachingconfigurersupport {

    @bean
    public lettuceconnectionfactory redisconnectionfactory() {
        redisstandaloneconfiguration redisstandaloneconfiguration = new redisstandaloneconfiguration();
        redisstandaloneconfiguration.sethostname("localhost");
        redisstandaloneconfiguration.setport(6379);

        genericobjectpoolconfig poolconfig = new genericobjectpoolconfig<>();
        poolconfig.setmaxtotal(10); // 最大连接数
        poolconfig.setmaxidle(5); // 最大空闲连接数
        poolconfig.setminidle(1); // 最小空闲连接数
        poolconfig.setmaxwaitmillis(2000); // 获取连接的最大等待时间

        lettucepoolingclientconfiguration poolingclientconfig = lettucepoolingclientconfiguration.builder()
                .poolconfig(poolconfig)
                .build();

        return new lettuceconnectionfactory(redisstandaloneconfiguration, poolingclientconfig);
    }

    @bean
    public redistemplate redistemplate() {
        redistemplate template = new redistemplate<>();
        template.setconnectionfactory(redisconnectionfactory());
        template.setkeyserializer(new stringredisserializer());
        template.setvalueserializer(new stringredisserializer());
        return template;
    }
}

(二)通过 application.properties 文件

在 application.properties 文件中添加以下配置:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=1
spring.redis.lettuce.pool.max-wait=-1

三、测试 redis 操作

创建一个简单的控制器来测试 redis 的基本操作:

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class rediscontroller {

    @autowired
    private redistemplate redistemplate;

    @getmapping("/set")
    public string set(@requestparam string key, @requestparam string value) {
        redistemplate.opsforvalue().set(key, value);
        return "value set successfully";
    }

    @getmapping("/get")
    public string get(@requestparam string key) {
        return (string) redistemplate.opsforvalue().get(key);
    }

    @getmapping("/delete")
    public string delete(@requestparam string key) {
        redistemplate.delete(key);
        return "value deleted successfully";
    }
}

四、完整示例代码

(一)pom.xml


    
        org.springframework.boot
        spring-boot-starter-data-redis
    
    
        org.apache.commons
        commons-pool2
    

(二)redisconfig.java

import org.apache.commons.pool2.impl.genericobjectpoolconfig;
import org.springframework.cache.annotation.cachingconfigurersupport;
import org.springframework.cache.annotation.enablecaching;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
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.stringredisserializer;

@enablecaching
@configuration
public class redisconfig extends cachingconfigurersupport {

    @bean
    public lettuceconnectionfactory redisconnectionfactory() {
        redisstandaloneconfiguration redisstandaloneconfiguration = new redisstandaloneconfiguration();
        redisstandaloneconfiguration.sethostname("localhost");
        redisstandaloneconfiguration.setport(6379);

        genericobjectpoolconfig poolconfig = new genericobjectpoolconfig<>();
        poolconfig.setmaxtotal(10); // 最大连接数
        poolconfig.setmaxidle(5); // 最大空闲连接数
        poolconfig.setminidle(1); // 最小空闲连接数
        poolconfig.setmaxwaitmillis(2000); // 获取连接的最大等待时间

        lettucepoolingclientconfiguration poolingclientconfig = lettucepoolingclientconfiguration.builder()
                .poolconfig(poolconfig)
                .build();

        return new lettuceconnectionfactory(redisstandaloneconfiguration, poolingclientconfig);
    }

    @bean
    public redistemplate redistemplate() {
        redistemplate template = new redistemplate<>();
        template.setconnectionfactory(redisconnectionfactory());
        template.setkeyserializer(new stringredisserializer());
        template.setvalueserializer(new stringredisserializer());
        return template;
    }
}

(三)application.properties

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.database=0
spring.redis.lettuce.pool.max-active=10
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=1
spring.redis.lettuce.pool.max-wait=-1

(四)rediscontroller.java

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.data.redis.core.redistemplate;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestparam;
import org.springframework.web.bind.annotation.restcontroller;

@restcontroller
public class rediscontroller {

    @autowired
    private redistemplate redistemplate;

    @getmapping("/set")
    public string set(@requestparam string key, @requestparam string value) {
        redistemplate.opsforvalue().set(key, value);
        return "value set successfully";
    }

    @getmapping("/get")
    public string get(@requestparam string key) {
        return (string) redistemplate.opsforvalue().get(key);
    }

    @getmapping("/delete")
    public string delete(@requestparam string key) {
        redistemplate.delete(key);
        return "value deleted successfully";
    }
}

总结

通过以上步骤,您已经成功配置了 spring boot 中的 redis 连接池。这种配置方式不仅提高了 redis 操作的性能,还确保了资源的高效利用。

到此这篇关于springboot中配置redis连接池的完整指南的文章就介绍到这了,更多相关springboot配置redis连接池内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

分享
海报
103
上一篇:SpringBoot中Excel处理完全指南分享 下一篇:SpringBoot处理跨域请求(CORS)的五种方式

忘记密码?

图形验证码