Jedis

Access a DCS Redis instance through Jedis on an ECS in the same VPC. For more information about how to use other Redis clients, visit the Redis official website.

Spring Data Redis is already integrated with Jedis and Lettuce for Spring Boot projects. Spring Boot 1.x is integrated with Jedis, and Spring Boot 2.x is integrated with Lettuce. To use Jedis in Spring Boot 2.x and later, you need to solve Lettuce dependency conflicts.

Prerequisites

Pom Configuration

<!-- import spring-data-redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
<!--In Spring Boot 2.0, Lettuce is used by default. To use Jedis, solve dependency conflicts.-->
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--Jedis dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.10.0</version>
</dependency>

application.properties Configuration

Bean Configuration

(Optional) Configuring SSL Connections

If SSL is enabled for the instance, use the following content to replace the JedisClientConfiguration construction method clientConfiguration() in Bean Configuration for connecting to the instance with SSL. For details about whether your DCS Redis instances support SSL, see Configuring SSL.

@Bean
public JedisClientConfiguration clientConfiguration() throws Exception {
    JedisClientConfiguration.JedisClientConfigurationBuilder configurationBuilder
        = JedisClientConfiguration.builder()
        .connectTimeout(Duration.ofMillis(redisConnectTimeout))
        .readTimeout(Duration.ofMillis(redisReadTimeout));

    configurationBuilder.usePooling().poolConfig(redisPoolConfig());
    configurationBuilder.useSsl().sslSocketFactory(getTrustStoreSslSocketFactory());
    return configurationBuilder.build();
}

private SSLSocketFactory getTrustStoreSslSocketFactory() throws Exception{
    //Load the CA certificate in the user-defined path based on service requirements.
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    Certificate ca;
    try (InputStream is = new FileInputStream("./ca.crt")) {
        ca = cf.generateCertificate(is);
    }

    //Create keystore.
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    //Create TrustManager.
    TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
        TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(keyStore);

    //Create SSLContext.
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, trustManagerFactory.getTrustManagers(), new SecureRandom());
    return context.getSocketFactory();
}

Parameter Description

Table 1 RedisStandaloneConfiguration parameters

Parameter

Default Value

Description

hostName

localhost

IP address/domain name for connecting to a DCS Redis instance

port

6379

Port number

database

0

Database number. Default: 0.

password

-

Password

Table 2 RedisClusterConfiguration parameters

Parameter

Description

clusterNodes

Cluster node connection information, including the node IP address and port number

maxRedirects

Maximum redirecting times

password

Password

Table 3 JedisPoolConfig parameters

Parameter

Default Value

Description

minIdle

-

Minimum connections in the connection pool

maxIdle

-

Maximum idle connections in the connection pool

maxTotal

-

Maximum total connections in the connection pool

blockWhenExhausted

true

Indicates whether to wait after the connection pool is exhausted. true: Wait. false: Do not wait. To validate maxWaitMillis, this parameter must be set to true.

maxWaitMillis

-1

Maximum amount of time (in milliseconds) to wait for connection after the connection pool is exhausted. The default value -1 indicates to wait indefinitely.

testOnCreate

false

Indicates whether to enable connectivity test on creating connections. false: Disable. true: Enable.

testOnBorrow

false

Indicates whether to enable connectivity test on obtaining connections. false: Disable. true: Enable. For heavy-traffic services, set this parameter to false to reduce overhead.

testOnReturn

false

Indicates whether to enable connectivity test on returning connections. false: Disable. true: Enable. For heavy-traffic services, set this parameter to false to reduce overhead.

testWhileIdle

false

Indicates whether to check for idle connections. If this parameter is set to false, idle connections are not evicted. Recommended value: true.

softMinEvictableIdleTimeMillis

1800000

Duration (in milliseconds) after which idle connections are evicted. If the idle duration is greater than this value and the maximum number of idle connections is reached, idle connections are directly evicted.

minEvictableIdleTimeMillis

60000

Minimum amount of time (in milliseconds) a connection may remain idle in the pool before it is eligible for eviction. The recommended value is -1, indicating that softMinEvictableIdleTimeMillis is used instead.

timeBetweenEvictionRunsMillis

60000

Interval (in milliseconds) for checking and evicting idle connections.

Table 4 JedisClientConfiguration parameters

Parameter

Default Value

Description

connectTimeout

2000

Connection timeout interval, in milliseconds.

readTimeout

2000

Timeout interval for waiting for a response, in milliseconds.

poolConfig

-

Pool configurations. For details, see JedisPoolConfig.

Suggestion for Configuring DCS Instances