tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

JedisによるRedisへの接続

ダウンロード
フォーカスモード
フォントサイズ
最終更新日: 2026-10-20 19:07:18
AI翻訳
Jedisは、シンプルで使いやすいRedis Javaクライアントです。Redisを操作するための同期ブロッキングAPIを提供し、シングルスレッドまたは低同時実行のシナリオでの使用に適しています。

推奨バージョン

jedisは5.1.2の使用を推奨します。接続モードはスタンドアロンモードです。
説明:
Tencent CloudはVPC IPプロキシ層を通じてRedisの基盤アーキテクチャの複雑性(シャーディング/高可用性)を統一的に隠蔽しています。クライアントがTencent Cloud Redisインスタンスにアクセスする際、クラスタやセンチネルモードを設定する必要はなく、単一ノードのRedisに接続するのと同様に、Tencent Cloudが提供する統一エントリ(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;
}
}

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック