Spring Boot 中配置 Redis 连接池的详细

一、添加依赖

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

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>
</dependencies>

二、配置 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<Object> 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<String, Object> redisTemplate() {
        RedisTemplate<String, Object> 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<String, Object> 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 操作的性能,还确保了资源的高效利用。希望本文的示例和讲解对您有所帮助,如果您在配置 Redis 连接池时有任何疑问,欢迎随时交流探讨!

### Spring BootRedis 连接池配置Spring Boot 应用程序中,可以通过 `application.yml` 或者 `application.properties` 文件来配置 Redis 连接池的相关参数。以下是基于 YAML 配置文件的一个典型例子: ```yaml spring: redis: host: 127.0.0.1 port: 6379 database: 0 password: timeout: 10s lettuce: pool: min-idle: 5 max-idle: 8 max-active: 20 max-wait: -1ms ``` 上述配置定义了一个名为 `lettuce.pool` 的部分用于设定连接池的行为[^3]。 对于更复杂的场景,比如主从复制环境下的客户端配置,则可以参照官方文档进一步调整设置以适应特定需求[^4]。 当应用程序启动时会读取这些属性,并自动装配到对应的组件当中去。如果希望自定义更多细节或实现某些特殊逻辑的话,还可以通过编写 Java Config 类来进行扩展性的定制化开发。 下面给出一段简单的代码片段展示如何利用编程方式修改默认行为: ```java import org.springframework.context.annotation.Bean; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory){ final RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); // 可在此处添加额外配置 return template; } } ``` 这段代码展示了怎样创建一个 Bean 来覆盖默认模板实例,在这里可以根据项目实际需要加入更多的个性化选项[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Java皇帝

有帮助就赏点吧,博主点杯水喝喝

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

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

打赏作者

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

抵扣说明:

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

余额充值