最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2025年04月20日 13:37:44   作者:Java皇帝  
這篇文章主要為大家詳細介紹了SpringBoot中配置Redis連接池的完整指南,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

一、添加依賴

在 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)文章

  • java中幾種http請求方式示例詳解

    java中幾種http請求方式示例詳解

    在日常工作和學(xué)習(xí)中有很多地方都需要發(fā)送HTTP請求,下面這篇文章主要給大家介紹了關(guān)于java中幾種http請求方式的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • SpringBoot集成FFmpeg實現(xià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?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)

    這篇文章主要介紹了如何使用Maven創(chuàng)建web目錄結(jié)構(gòu)的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Java使用poi-tl庫操作wold文檔的方法

    Java使用poi-tl庫操作wold文檔的方法

    java項目實際開發(fā)中經(jīng)常會遇到制作word表單且表格數(shù)據(jù)行循環(huán)功能,這篇文章主要介紹了Java使用poi-tl庫操作wold文檔的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-11-11
  • SpringBoot實現(xiàn)HTTP服務(wù)監(jiān)聽的代碼示例

    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
  • springboot排除某些自動配置的操作方法

    springboot排除某些自動配置的操作方法

    Spring Boot 提供的自動配置非常強大,某些情況下,自動配置的功能可能不符合我們的需求,需要我們自定義配置,這個時候就需要排除/禁用Spring Boot 某些類的自動化配置了,本文給大家介紹springboot排除某些自動配置的方法,感興趣的朋友一起看看吧
    2023-08-08
  • 詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù)

    詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù)

    本篇文章主要介紹了詳解Spring整合Quartz實現(xiàn)動態(tài)定時任務(wù),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Java操作MongoDB事務(wù)未生效的常見場景及解決方案

    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加載外部配置方式

    這篇文章主要介紹了@ConfigurationProperties加載外部配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評論

邻水| 荃湾区| 平舆县| 安吉县| 黔东| 诏安县| 万州区| 吉首市| 淮南市| 固镇县| 饶平县| 济阳县| 彩票| 康乐县| 浠水县| 庆阳市| 浠水县| 永靖县| 正定县| 西充县| 普洱| 如东县| 赣榆县| 阜阳市| 东阿县| 贡觉县| 通州区| 抚顺县| 嘉荫县| 永嘉县| 汝阳县| 宜章县| 建湖县| 左权县| 前郭尔| 孟村| 米易县| 仁怀市| 灵璧县| 宁安市| 色达县|