tencent cloud

腾讯云分布式缓存数据库(兼容 Redis)

动态与公告
产品动态
公告
新手指引
产品简介
产品概述
产品优势
应用场景
存储引擎
产品系列
产品版本
规格与性能
读写分离
多可用区部署
地域和可用区
名词解释
购买指南
计费概述
定价中心
购买实例
续费说明(包年包月)
退费说明(包年包月)
欠费说明
按量转包年包月
快速入门
快速创建实例
连接 Redis 实例
操作指南
操作总览
连接数据库实例
管理实例
升级实例
管理节点(Redis/ValKey 版)
管理多可用区
备份与恢复
账号管理
参数配置
慢查询
访问管理
网络与安全
监控与告警
事件管理(Redis/ValKey 版)
数据迁移
Redis 版全球复制
数据库审计
诊断优化
Sentinel 模式
开发准则
命名规则
基本使用准则
Key 与 Value 设计原则
命令使用准则
客户端程序设计准则
连接池配置
命令参考
命令参考概览
Redis 版与 Valkey 版命令兼容性
大版本命令使用差异
Proxy 架构与直连模式的使用差异
命令更多操作(Redis/Valkey 版)
Memcached 版命令兼容性
实践教程
基于 Spring Boot 搭建 Redis 客户端监控
Redis 客户端连接配置策略与实践
集群架构全局 SCAN 使用指南
实例安全下线
热 Key 与 大 key
可用区迁移方案
故障处理
连接异常
Redisson 客户端超时重连异常分析及解决方案
性能排查与调优
API 文档
History
Introduction
API Category
Making API Requests
Instance APIs
Parameter Management APIs
Other APIs
Backup and Restoration APIs
Region APIs
Monitoring and Management APIs
Log APIs
Data Types
Error Codes
常见问题
使用常见问题
连接登录问题
购买相关问题
相关协议
服务等级协议
Terms of Service
词汇表
联系我们

Jedis 连接 Redis

PDF
聚焦模式
字号
最后更新时间: 2026-03-17 18:23:48
Jedis 是一个简单易用的 Redis Java 客户端,它提供了同步阻塞的 API 来操作 Redis,适合在单线程或低并发场景下使用。

版本推荐

jedis 推荐使用5.1.2,连接模式为单机模式。
说明:
腾讯云通过 VPC IP 代理层统一屏蔽了 Redis 底层架构的复杂性(分片/高可用)。客户端访问腾讯云 Redis 实例时,无需配置集群或哨兵模式,而是像连接单节点 Redis 一样,直接访问腾讯云提供的统一入口(VPC IP)。

参数配置

参数名
类型
默认值
解释
推荐值
connectTimeout
int
2000ms
设置连接超时时间
3000ms
readTimeout
int
2000ms
设置命令超时时间
2000ms
poolMinIdle
int
连接池最小空闲连接数
50
poolMaxIdle
int
连接池最大空闲连接数
200
poolMaxTotal
int
连接池最大连接数
200

application.properties 格式

tencent.redis.host=XX.XX.XX.XX
tencent.redis.port=6379
tencent.redis.database=0
tencent.redis.password=XXXX
tencent.redis.connect.timeout=3000
tencent.redis.read.timeout=2000
tencent.redis.pool.minSize=50
tencent.redis.pool.maxSize=200

application.yml 格式

tencent:
redis:
host: XX.XX.XX.XX
port: 6379
database: 0
password: XXXX
connect:
timeout: 3000
read:
timeout: 2000
pool:
minSize: 50
maxSize: 200

代码示例

Pom 配置

<!-- 引入spring-data-redis组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<!--spring boot 2.0之后默认lettuce客户端, 使用jedis时需要排包-->
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入jedis依赖包 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.2</version>
</dependency>

参考代码

Jedis 连接池默认采用 LIFO(后进先出)配置,但实际运行中,因各连接响应时间不同,易导致连接建立不均衡。为解决此问题,建议将 LIFO 配置改为 FIFO(先进先出),以确保连接建立顺序公平,提升系统稳定性和性能。
import java.time.Duration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import redis.clients.jedis.JedisPoolConfig;

@Configuration
public class JedisConfig {

@Value("${tencent.redis.host}")
private String redisHost;

@Value("${tencent.redis.port:6379}")
private Integer redisPort = 6379;

@Value("${tencent.redis.database:0}")
private Integer redisDatabase = 0;

@Value("${tencent.redis.password:}")
private String redisPassword;

@Value("${tencent.redis.connect.timeout:3000}")
private Integer redisConnectTimeout = 3000;

@Value("${tencent.redis.read.timeout:2000}")
private Integer redisReadTimeout = 2000;

@Value("${tencent.redis.pool.minSize:50}")
private Integer redisPoolMinSize = 50;

@Value("${tencent.redis.pool.maxSize:200}")
private Integer redisPoolMaxSize = 200;

@Bean
public RedisConnectionFactory redisConnectionFactory(JedisClientConfiguration clientConfiguration) {

RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
standaloneConfiguration.setHostName(redisHost);
standaloneConfiguration.setPort(redisPort);
standaloneConfiguration.setDatabase(redisDatabase);
standaloneConfiguration.setPassword(redisPassword);

return new JedisConnectionFactory(standaloneConfiguration, clientConfiguration);
}

@Bean
public JedisClientConfiguration clientConfiguration() {

JedisClientConfiguration clientConfiguration = JedisClientConfiguration.builder()
.connectTimeout(Duration.ofMillis(redisConnectTimeout))
.readTimeout(Duration.ofMillis(redisReadTimeout))
.usePooling().poolConfig(redisPoolConfig())
.build();

return clientConfiguration;
}

private JedisPoolConfig redisPoolConfig() {

JedisPoolConfig poolConfig = new JedisPoolConfig();
//连接池的最小连接数
poolConfig.setMinIdle(redisPoolMinSize);
//连接池的最大空闲连接数
poolConfig.setMaxIdle(redisPoolMaxSize);
//连接池的最大连接数
poolConfig.setMaxTotal(redisPoolMaxSize);
//创建连接时校验有效性(ping),默认false
poolConfig.setTestOnCreate(false);
//获取连接时校验有效性(ping),默认false,业务量大时建议设置为false减少开销
poolConfig.setTestOnBorrow(true);
//归还连接时校验有效性(ping),默认false,业务量大时建议设置为false减少开销
poolConfig.setTestOnReturn(false);
//关闭lifo,将 LIFO 配置改为FIFO(先进先出),以确保连接建立顺序公平
poolConfig.setLifo(false);
return poolConfig;
}
}

帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈