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)文章
經(jīng)典的Java面試題及回答集錦(基礎(chǔ)篇)
本文給大家收藏整理了java面試題及回答,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-03-03
Spring?@Bean?修飾方法時注入?yún)?shù)的操作方法
對于 Spring 而言,IOC 容器中的 Bean 對象的創(chuàng)建和使用是一大重點,Spring 也為我們提供了注解方式創(chuàng)建 bean 對象:使用 @Bean,這篇文章主要介紹了Spring?@Bean?修飾方法時如何注入?yún)?shù),需要的朋友可以參考下2023-10-10
jmeter+ant+jenkins自動化測試環(huán)境配置搭建過程
在搭建jmeter+ant+jenkins環(huán)境有些前提條件,那就是要先配置好java環(huán)境、安裝好jenkins以及配置好jmeter,這樣才能省去很多的事情,對jmeter+ant+jenkins自動化測試環(huán)境配置搭建過程感興趣的朋友一起看看吧2021-12-12
使用Spring?Batch實現(xiàn)大數(shù)據(jù)處理的操作方法
通過使用Spring?Batch,我們可以高效地處理大規(guī)模數(shù)據(jù),本文介紹了如何配置和實現(xiàn)一個基本的Spring?Batch作業(yè),包括讀取數(shù)據(jù)、處理數(shù)據(jù)和寫入數(shù)據(jù)的全過程,感興趣的朋友跟隨小編一起看看吧2024-07-07
在Spring Boot中淺嘗內(nèi)存泄漏的實戰(zhàn)記錄
本文給大家分享在Spring Boot中淺嘗內(nèi)存泄漏的實戰(zhàn)記錄,結(jié)合實例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04
java應(yīng)用占用內(nèi)存過高排查的解決方案
這篇文章主要介紹了java應(yīng)用占用內(nèi)存過高排查的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Spring Boot應(yīng)用啟動時自動執(zhí)行代碼的五種方式(常見方法)
Spring Boot為開發(fā)者提供了多種方式在應(yīng)用啟動時執(zhí)行自定義代碼,這些方式包括注解、接口實現(xiàn)和事件監(jiān)聽器,本文我們將探討一些常見的方法,以及如何利用它們在應(yīng)用啟動時執(zhí)行初始化邏輯,感興趣的朋友一起看看吧2024-04-04
SpringBoot配置主從數(shù)據(jù)庫實現(xiàn)讀寫分離
現(xiàn)在的 Web 應(yīng)用大都是讀多寫少,本文主要介紹了SpringBoot配置主從數(shù)據(jù)庫實現(xiàn)讀寫分離,具有一定的參考價值,感興趣的可以了解一下2023-11-11

