- All Implemented Interfaces:
ConnectionPoint,Serializable
public class RedisURI extends Object implements Serializable, ConnectionPoint
RedisURI:
- Use an URI:
SeeRedisURI.create("redis://localhost/");create(String)for more options - Use the Builder:
SeeRedisURI.Builder.redis("localhost", 6379).withPassword("password").withDatabase(1).build();RedisURI.Builder.redis(String)andRedisURI.Builder.sentinel(String)for more options. - Construct your own instance:
ornew RedisURI("localhost", 6379, Duration.ofSeconds(60));RedisURI uri = new RedisURI(); uri.setHost("localhost");
URI syntax
Redis StandaloneredisRedis Standalone (SSL)://[[username:]password@]host [:port][/database][?[timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
redissRedis Standalone (Unix Domain Sockets)://[[username:]password@]host [:port][/database][?[timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
redis-socketRedis Sentinel://[[username:]password@]path[?[timeout=timeout[d|h|m|s|ms|us|ns]][&database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
redis-sentinel://[[username:]password@]host1 [:port1][, host2 [:port2]][, hostN [:portN]][/database][?[timeout=timeout[d|h|m|s|ms|us|ns]] [ &sentinelMasterId=sentinelMasterId] [&database=database] [&clientName=clientName] [&verifyPeer=NONE|CA|FULL]]
Note: When using Redis Sentinel, the password from the URI applies to the data nodes only. Sentinel authentication must be
configured for each sentinel node.
Note:Usernames are supported as of Redis 6.
Schemes
- redis Redis Standalone
- rediss Redis Standalone SSL
- redis-socket Redis Standalone Unix Domain Socket
- redis-sentinel Redis Sentinel
- rediss-sentinel Redis Sentinel SSL
Timeout units
- d Days
- h Hours
- m Minutes
- s Seconds
- ms Milliseconds
- us Microseconds
- ns Nanoseconds
Hint: The database parameter within the query part has higher precedence than the database in the path.
RedisURI supports Redis Standalone, Redis Sentinel and Redis Cluster with plain, SSL, TLS and unix domain socket connections.- Since:
- 3.0
- Author:
- Mark Paluch, Guy Korland, Johnny Lim
- See Also:
- Serialized Form
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRedisURI.BuilderBuilder for Redis URI. -
Field Summary
Fields Modifier and Type Field Description static Map<String,LongFunction<Duration>>CONVERTER_MAPstatic intDEFAULT_REDIS_PORTThe default redis port.static intDEFAULT_SENTINEL_PORTThe default sentinel port.static longDEFAULT_TIMEOUTDefault timeout: 60 secstatic DurationDEFAULT_TIMEOUT_DURATIONstatic StringPARAMETER_NAME_CLIENT_NAMEstatic StringPARAMETER_NAME_DATABASEstatic StringPARAMETER_NAME_DATABASE_ALTstatic StringPARAMETER_NAME_SENTINEL_MASTER_IDstatic StringPARAMETER_NAME_TIMEOUTstatic StringPARAMETER_NAME_VERIFY_PEERstatic StringURI_SCHEME_REDISstatic StringURI_SCHEME_REDIS_SECUREstatic StringURI_SCHEME_REDIS_SECURE_ALTstatic StringURI_SCHEME_REDIS_SENTINELstatic StringURI_SCHEME_REDIS_SENTINEL_SECUREstatic StringURI_SCHEME_REDIS_SOCKETstatic StringURI_SCHEME_REDIS_SOCKET_ALTstatic StringURI_SCHEME_REDIS_TLS_ALT -
Constructor Summary
-
Method Summary
Modifier and Type Method Description voidapplyAuthentication(RedisURI source)Apply authentication from anotherRedisURI.voidapplySsl(RedisURI source)Apply authentication from anotherRedisURI.static RedisURI.Builderbuilder()Return a newRedisURI.Builderto construct aRedisURI.static RedisURI.Builderbuilder(RedisURI source)Create a newRedisURI.Builderthat is initialized from a plainRedisURI.static RedisURIcreate(String uri)Create a Redis URI from an URI string.static RedisURIcreate(String host, int port)Create a Redis URI from host and port.static RedisURIcreate(URI uri)Create a Redis URI from an URI string: The uri must follow conventions ofURIbooleanequals(Object o)StringgetClientName()Returns the client name.intgetDatabase()Returns the Redis database number.StringgetHost()Returns the host.char[]getPassword()Returns the password.intgetPort()Returns the Redis port.StringgetSentinelMasterId()Returns the Sentinel Master Id.List<RedisURI>getSentinels()StringgetSocket()Returns the Unix Domain Socket path.DurationgetTimeout()Returns the command timeout for synchronous command execution.StringgetUsername()Returns the username.SslVerifyModegetVerifyMode()Returns the mode to verify peers when usingSSL.inthashCode()booleanisSsl()Returnstrueif SSL mode is enabled.booleanisStartTls()Returnstrueif StartTLS is enabled.booleanisVerifyPeer()Returns whether to verify peers when usingSSL.voidsetClientName(String clientName)Sets the client name to be applied on Redis connections.voidsetDatabase(int database)Sets the Redis database number.voidsetHost(String host)Sets the Redis host.voidsetPassword(char[] password)Sets the password.voidsetPassword(CharSequence password)Sets the password.voidsetPassword(String password)Deprecated.since 6.0.voidsetPort(int port)Sets the Redis port.voidsetSentinelMasterId(String sentinelMasterId)Sets the Sentinel Master Id.voidsetSocket(String socket)Sets the Unix Domain Socket path.voidsetSsl(boolean ssl)Sets whether to use SSL.voidsetStartTls(boolean startTls)Returns whether StartTLS is enabled.voidsetTimeout(Duration timeout)Sets the command timeout for synchronous command execution.voidsetUsername(String username)Sets the username.voidsetVerifyPeer(boolean verifyPeer)Sets whether to verify peers when usingSSL.voidsetVerifyPeer(SslVerifyMode verifyMode)Sets how to verify peers when usingSSL.StringtoString()URItoURI()Creates an URI based on the RedisURI if possible.
-
Field Details
-
URI_SCHEME_REDIS_SENTINEL
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SENTINEL_SECURE
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SECURE
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SECURE_ALT
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_TLS_ALT
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SOCKET
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SOCKET_ALT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_TIMEOUT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_DATABASE
- See Also:
- Constant Field Values
-
PARAMETER_NAME_DATABASE_ALT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_SENTINEL_MASTER_ID
- See Also:
- Constant Field Values
-
PARAMETER_NAME_CLIENT_NAME
- See Also:
- Constant Field Values
-
PARAMETER_NAME_VERIFY_PEER
- See Also:
- Constant Field Values
-
CONVERTER_MAP
-
DEFAULT_SENTINEL_PORT
public static final int DEFAULT_SENTINEL_PORTThe default sentinel port.- See Also:
- Constant Field Values
-
DEFAULT_REDIS_PORT
public static final int DEFAULT_REDIS_PORTThe default redis port.- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUTDefault timeout: 60 sec- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT_DURATION
-
-
Constructor Details
-
RedisURI
public RedisURI()Default empty constructor. -
RedisURI
Constructor with host/port and timeout.- Parameters:
host- the hostport- the porttimeout- timeout value
-
-
Method Details
-
builder
Return a newRedisURI.Builderto construct aRedisURI.- Returns:
- a new
RedisURI.Builderto construct aRedisURI.
-
create
Create a Redis URI from host and port.- Parameters:
host- the hostport- the port- Returns:
- An instance of
RedisURIcontaining details from thehostandport.
-
create
Create a Redis URI from an URI string. The uri must follow conventions ofURI- Parameters:
uri- The URI string.- Returns:
- An instance of
RedisURIcontaining details from the URI.
-
create
Create a Redis URI from an URI string: The uri must follow conventions ofURI- Parameters:
uri- The URI.- Returns:
- An instance of
RedisURIcontaining details from the URI.
-
builder
Create a newRedisURI.Builderthat is initialized from a plainRedisURI.- Parameters:
source- the initialization source, must not benull.- Returns:
- the initialized builder.
- Since:
- 6.0
-
getHost
Returns the host.- Specified by:
getHostin interfaceConnectionPoint- Returns:
- the host.
-
setHost
Sets the Redis host.- Parameters:
host- the host
-
getSentinelMasterId
Returns the Sentinel Master Id.- Returns:
- the Sentinel Master Id.
-
setSentinelMasterId
Sets the Sentinel Master Id.- Parameters:
sentinelMasterId- the Sentinel Master Id.
-
getPort
public int getPort()Returns the Redis port.- Specified by:
getPortin interfaceConnectionPoint- Returns:
- the Redis port
-
setPort
public void setPort(int port)Sets the Redis port. Defaults toDEFAULT_REDIS_PORT.- Parameters:
port- the Redis port
-
getSocket
Returns the Unix Domain Socket path.- Specified by:
getSocketin interfaceConnectionPoint- Returns:
- the Unix Domain Socket path.
-
setSocket
Sets the Unix Domain Socket path.- Parameters:
socket- the Unix Domain Socket path.
-
applyAuthentication
Apply authentication from anotherRedisURI. The authentication settings of thesourceURI will be applied to this URI. That is in particular username and password. If the source has authentication credentials configured, then this URI will use the same credentials. If this URI has authentication configured and thesourceURI has no authentication, then this URI's authentication credentials will be reset.- Parameters:
source- must not benull.- Since:
- 6.0
-
getUsername
Returns the username.- Returns:
- the username
- Since:
- 6.0
-
setUsername
Sets the username.- Parameters:
username- the username, must not benull.- Since:
- 6.0
-
getPassword
public char[] getPassword()Returns the password.- Returns:
- the password
-
setPassword
Deprecated.since 6.0. UsesetPassword(CharSequence)orsetPassword(char[])to avoid String caching.Sets the password. Use empty string to skip authentication.This method is deprecated as of Lettuce 6.0. The reason is that
Stringhas a strong caching affinity and the JVM cannot easily GCStringinstances. Therefore we suggest using eitherchar[]or a customCharSequence(e.g.StringBuilderor netty'sAsciiString).- Parameters:
password- the password, must not benull.
-
setPassword
Sets the password. Use empty string to skip authentication.- Parameters:
password- the password, must not benull.- Since:
- 5.2
-
setPassword
public void setPassword(char[] password)Sets the password. Use empty char array to skip authentication.- Parameters:
password- the password, can benull.- Since:
- 4.4
-
getTimeout
Returns the command timeout for synchronous command execution.- Returns:
- the Timeout
- Since:
- 5.0
-
setTimeout
Sets the command timeout for synchronous command execution. A zero timeout value indicates to not time out.- Parameters:
timeout- the command timeout for synchronous command execution.- Since:
- 5.0
-
getDatabase
public int getDatabase()Returns the Redis database number. Databases are only available for Redis Standalone and Redis Master/Slave.- Returns:
- the Redis database number
-
setDatabase
public void setDatabase(int database)Sets the Redis database number. Databases are only available for Redis Standalone and Redis Master/Slave.- Parameters:
database- the Redis database number.
-
getClientName
Returns the client name.- Returns:
- the client name.
- Since:
- 4.4
-
setClientName
Sets the client name to be applied on Redis connections.- Parameters:
clientName- the client name.- Since:
- 4.4
-
applySsl
Apply authentication from anotherRedisURI. The SSL settings of thesourceURI will be applied to this URI. That is in particular SSL usage, peer verification and StartTLS.- Parameters:
source- must not benull.- Since:
- 6.0
-
isSsl
public boolean isSsl()Returnstrueif SSL mode is enabled.- Returns:
trueif SSL mode is enabled.
-
setSsl
public void setSsl(boolean ssl)Sets whether to use SSL. Sets SSL also for already configured Redis Sentinel nodes.- Parameters:
ssl-
-
isVerifyPeer
public boolean isVerifyPeer()Returns whether to verify peers when usingSSL.- Returns:
trueto verify peers when usingSSL.
-
getVerifyMode
Returns the mode to verify peers when usingSSL.- Returns:
- the verification mode
- Since:
- 6.1
-
setVerifyPeer
public void setVerifyPeer(boolean verifyPeer)Sets whether to verify peers when usingSSL. Sets peer verification also for already configured Redis Sentinel nodes.- Parameters:
verifyPeer-trueto verify peers when usingSSL.
-
setVerifyPeer
Sets how to verify peers when usingSSL. Sets peer verification also for already configured Redis Sentinel nodes.- Parameters:
verifyMode- verification mode to use when usingSSL.- Since:
- 6.1
-
isStartTls
public boolean isStartTls()Returnstrueif StartTLS is enabled.- Returns:
trueif StartTLS is enabled.
-
setStartTls
public void setStartTls(boolean startTls)Returns whether StartTLS is enabled. Sets StartTLS also for already configured Redis Sentinel nodes.- Parameters:
startTls-trueif StartTLS is enabled.
-
getSentinels
- Returns:
- the list of
Redis Sentinel URIs.
-
toURI
Creates an URI based on the RedisURI if possible.An URI an represent a Standalone address using host and port or socket addressing or a Redis Sentinel address using host/port. A Redis Sentinel URI with multiple nodes using Unix Domain Sockets cannot be rendered to a
URI.- Returns:
- URI based on the RedisURI.
- Throws:
IllegalStateException- if the URI cannot be rendered.
-
toString
-
equals
-
hashCode
public int hashCode()
-