Skip to main content

dawdler-jedis-core

Module introduction​

The Redis plugin implemented with Jedis, supporting standalone mode and sentinel mode.

1. Add the dependency to the pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-jedis-core</artifactId>

2. properties file description​

#######################
# masterName in sentinel mode (note: sentinel and standalone are mutually exclusive; do not set this item in standalone mode)
masterName=masterName
# sentinel list (note: sentinel and standalone are mutually exclusive; do not set this item in standalone mode)
sentinels=192.168.0.2:26379,192.168.0.3:26379,192.168.0.4:26379
# sentinel username
sentinelUser=sentinelUser
# sentinel password
sentinelPassword=sentinelPassword
#######################
# standalone host
host=127.0.0.1
# standalone port
port=6379
#######################

# Set a username since Redis 6; comment this item out if not needed
user=redis_user
# password
password=password
# number of reconnect attempts when reconnection fails; 0 disables retries; typically used for the network interruption that occurs when sentinel switches the master
failoverTryCount=0
# interval between reconnect attempts when reconnection fails (in milliseconds)
failoverIntervalMillis=5000
# maximum number of connections
pool.maxTotal=20
# minimum number of idle connections
pool.minIdle=2
# maximum number of idle connections
pool.maxIdle=8
# maximum wait time (in milliseconds)
pool.maxWaitMillis=10000
# whether to validate a connection when borrowing it; default false
pool.testOnBorrow=false
# whether to validate a connection when creating it; default false
pool.testOnCreate=false
# whether to validate a connection when returning it; default false
pool.testOnReturn=false
# timeout (in milliseconds)
timeout=10000
# use the specified database
database=0

The configuration file supports multiple environments, a unified configuration center, encryption, and variable substitution. See Multi-environment configuration.

3. JedisInjector annotation​

Used to inject a JedisOperator. For the methods of JedisOperator, refer to the official documentation of the Jedis class.

The value of the JedisInjector annotation takes the fileName, which is the name of the configuration file (without the .properties suffix).

For details, see:

dawdler-server-plug-jedis implements injection on the dawdler-server side.

dawdler-client-plug-jedis implements injection on the web side.

4. Usage outside the dawdler architecture​

//By calling the getUnifiedJedis method of UnifiedJedisFactory

public static UnifiedJedisWrapper getUnifiedJedis(String fileName)

//Obtain the UnifiedJedisWrapper by calling this method; fileName does not include the .properties suffix.

//For example: if fileName is myRedis, you need to create the configuration file myRedis.properties on the project classpath.

Note: when UnifiedJedisWrapper runs on the client or the server side, there is no need to close it manually; dawdler closes the related resources automatically.

When used outside the dawdler architecture, you need to call UnifiedJedisFactory.shutdownAll(); to release the resources.

5. Using distributed locks​

Distributed locks are implemented based on Redis Lua scripts.

The JedisLockInjector annotation is used to inject a JedisDistributedLockHolder.

/**
* @author jackson.song
* @version V1.0
* Marks a member field for injection of a JedisDistributedLockHolder
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface JedisLockInjector {
/**
* Configuration file name
*/
String fileName();

/**
* Wait before the next retry, in milliseconds
*/
int intervalInMillis() default JedisDistributedLockHolder.DEFAULT_INTERVAL_IN_MILLIS;

/**
* Lock expiration time, in milliseconds
*/
long lockExpiryInMillis() default JedisDistributedLockHolder.DEFAULT_LOCK_EXPIRY_IN_MILLIS;

/**
* Whether to enable the watchdog, which extends locks for operations that have not finished processing; enabled by default
*/
boolean useWatchDog() default true;

}

For details, see:

dawdler-server-plug-jedis implements injection on the dawdler-server side.

dawdler-client-plug-jedis implements injection on the web side.