tencent cloud

Tencent Cloud Distributed Cache (Redis OSS-Compatible)

Release Notes and Announcements
Release Notes
Announcements
User Tutorial
Product Introduction
Overview
Product Strengths
Use Cases
Storage Engine
Product Series
Product Versions
Specifications and Performance
Read/Write Separation
Multi-AZ Deployment
Regions and AZs
Terms
Service Regions and Service Providers
Purchase Guide
Billing Overview
Pricing Center
Instance Purchasing
Renewal (Yearly/Monthly Subscription)
Refund (Yearly/Monthly Subscription)
Overdue Payments
Switching from Pay-as-You-Go to Yearly/Monthly Subscription
Getting Started
Quickly Creating an Instance
Connecting to Redis Instance
Operation Guide
Operation Overview
Connecting to a Database Instance
Managing Instances
Upgrade Instance
Management Node (Redis/ValKey Edition)
Multi-AZ Deployment Management
Backup and Restoration
Managing Accounts
Parameter Configuration
Slow Query
Access Management
Network and Security
Monitoring and Alarms
Event Management (Redis/ValKey Edition)
Data Migration
Global Replication for Redis Edition
Database Audit
Performance Optimization
Sentinel Mode
Development Guidelines
Naming Rules
Basic Usage Guidelines
Design Principles of Key and Value
Command Usage Guidelines
Design Principles of Client Programs
Connection Pool Configuration
Command Reference
Command Reference Overview
Redis Edition and Valkey Edition Command Compatibility
Version Command Usage Differences
Differences Between the Proxy Architecture and Direct Connection Mode
More Command Operations (Redis/Valkey Edition)
Memcached Edition Command Compatibility
Practical Tutorial
Building TencentDB for Redis® Client Monitoring Based on Spring Boot
Redis Client Connection Configuration Policy and Practice
Global SCAN Guide for Cluster Architecture
Eliminating Instances Securely
Hot Key and Big Key
AZ Migration Scheme
Troubleshooting
Connection Exception
Exception Analysis and Solution of Redisson Client Timeout Reconnection
Performance Troubleshooting and Fine-Tuning
API Documentation
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
FAQs
General
Connection and Login
Purchase
Service Agreement
Service Level Agreement
Terms of Service
Glossary
Contact Us

Java Connection Sample

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-03-18 11:26:59
This document provides client code samples for Java to help you access a database with or without SSL encryption enabled.

Preparations

Get the Private IPv4 Address and Port information for database connection in the Network Info section on the Instance Details page in the Tencent Cloud Distributed Cache console. For detailed directions, see Viewing Instance Information.
Get the account and password for database access. For detailed directions, see Managing Account.
If you want to connect to the database over SSL, enable SSL encryption to get the SSL certificate file.
Prepare the client Jedis and use the latest edition.
Download Jedis and its dependency library (jar package) as follows.
wget https://repo1.maven.org/maven2/redis/clients/jedis/4.3.1/jedis-4.3.1.jar
wget https://repo1.maven.org/maven2/com/google/code/gson/gson/2.8.9/gson-2.8.9.jar
wget https://repol.maven.org/maven2/org/apache/commons/commons-pool2/2.11.1/commons-pool2-2.11.1.jar
Create a test project with the directory as shown below.


Connection sample with SSL encryption not enabled

You need to modify the parameters based on the comments, including IP, port, account, and password for database access.
Note:
If an exception occurs during connection, please refer to the Private Network Unable to Connect Location Guide to fix it.
import redis.clients.jedis.Jedis;

public class HelloRedis {

public static void main(String[] args) {
try {
/**The following parameters: if you are accessing via the private network, fill in the private IP address, port number, instance ID, and password of your distributed cache database instance;
Configure the instance's public network address, port number, and password in case of public network access.*/
String host = "192.xx.xx.195";
int port = 6379;
String instanceid = "crs-09xxxqv";
String password = "123ad6aq";
// Connect to Redis
Jedis jedis = new Jedis(host, port);
// Authenticate
jedis.auth(instanceid + ":" + password);

/**For operating instances, refer to https://github.com/xetorthio/jedis */
// Set the key
jedis.set("redis", "tencent");
System.out.println("set key redis suc, value is: tencent");
// Get the key
String value = jedis.get("redis");
System.out.println("get key redis is: " + value);

// Close and exit
jedis.quit();
jedis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output information, as shown below.

img



Connection sample with SSL encryption enabled

You need to modify the parameters based on the comments, including SSL certificate file, IP, port, account, and password for database access. To obtain an SSL certificate, please refer to SSL Encryption.
Note:
If an exception occurs during connection, please refer to the Private Network Unable to Connect Location Guide to fix it.
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;


public class Main {

public static void main(String[] args) throws Exception {
KeyStore trustStore = KeyStore.getInstance("jks");
// `ca.jks` is the certificate file name.
try (InputStream inputStream = new FileInputStream("ca.jks") ){
trustStore.load(inputStream, null);
}
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("PKIX");
trustManagerFactory.init(trustStore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();

//with ssl config jedis pool
// `vip` is the private IPv4 address for database connection, `6379` is the default port number, and `pwd` is the password of the default account. You need to replace them as needed.
JedisPool pool = new JedisPool(genericObjectPoolConfig, "vip",
6379, 2000, "pwd", 0, true, sslSocketFactory, null, null);
Jedis jedis = pool.getResource();
System.out.println(jedis.ping());
jedis.close();
}
}

Execution result

Run the sample program (SSL non-encryption connection example or SSL encryption connection example) under the project.

javac -cp ".:lib/*" src/HelloRedis. java -d ./
java-cp ".:lib/*" HelloRedis

도움말 및 지원

문제 해결에 도움이 되었나요?

피드백