SpringBoot中配置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 配置類
創(chuàng)建一個配置類,用于定義 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); // 最大連接數(shù)
poolConfig.setMaxIdle(5); // 最大空閑連接數(shù)
poolConfig.setMinIdle(1); // 最小空閑連接數(shù)
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 操作
創(chuàng)建一個簡單的控制器來測試 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";
}
}
四、完整示例代碼
(一)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>
(二)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<Object> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxTotal(10); // 最大連接數(shù)
poolConfig.setMaxIdle(5); // 最大空閑連接數(shù)
poolConfig.setMinIdle(1); // 最小空閑連接數(shù)
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
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<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";
}
}
總結(jié)
通過以上步驟,您已經(jīng)成功配置了 Spring Boot 中的 Redis 連接池。這種配置方式不僅提高了 Redis 操作的性能,還確保了資源的高效利用。
到此這篇關(guān)于SpringBoot中配置Redis連接池的完整指南的文章就介紹到這了,更多相關(guān)SpringBoot配置Redis連接池內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成FFmpeg實現(xiàn)多媒體處理
在現(xiàn)代 Web 應(yīng)用中,音視頻處理需求越來越常見,例如:視頻轉(zhuǎn)碼、截圖、音頻提取、格式轉(zhuǎn)換等,FFmpeg 是一個功能極其強大的開源音視頻處理工具,可以幫助我們高效完成這些任務(wù),本文將介紹SpringBoot集成FFmpeg實現(xiàn)多媒體處理,需要的朋友可以參考下2025-10-10
Spring?Clou整合?Security?+?Oauth2?+?jwt實現(xiàn)權(quán)限認(rèn)證的詳細過程
本文介紹了如何使用Spring?Cloud、Spring?Security、Oauth2和JWT實現(xiàn)統(tǒng)一認(rèn)證和權(quán)限管理,并分享了項目結(jié)構(gòu)和主要代碼示例,感興趣的朋友跟隨小編一起看看吧2025-02-02
如何使用Maven創(chuàng)建web目錄結(jié)構(gòu)
這篇文章主要介紹了如何使用Maven創(chuàng)建web目錄結(jié)構(gòu)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-06-06
SpringBoot實現(xiàn)HTTP服務(wù)監(jiān)聽的代碼示例
前后端分離項目中,在調(diào)用接口調(diào)試時候,我們可以通過cpolar內(nèi)網(wǎng)穿透將本地服務(wù)端接口模擬公共網(wǎng)絡(luò)環(huán)境遠程調(diào)用調(diào)試,本次教程我們以Java服務(wù)端接口為例,需要的朋友可以參考下2023-05-05
詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù)
本篇文章主要介紹了詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Java操作MongoDB事務(wù)未生效的常見場景及解決方案
在 Java 開發(fā)中,使用 MongoDB 存儲數(shù)據(jù)時,事務(wù)的正確使用至關(guān)重要,然而,在實際開發(fā)過程中,經(jīng)常會遇到 MongoDB 事務(wù)沒有生效的情況,本文我將結(jié)合多年實踐經(jīng)驗,深入剖析事務(wù)未生效的常見場景,并給出詳細的解決方案,需要的朋友可以參考下2025-07-07
@ConfigurationProperties加載外部配置方式
這篇文章主要介紹了@ConfigurationProperties加載外部配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03

