Class RedisURI
- java.lang.Object
-
- io.lettuce.core.RedisURI
-
- All Implemented Interfaces:
ConnectionPoint
,Serializable
public class RedisURI extends Object implements Serializable, ConnectionPoint
Redis URI. Contains connection details for the Redis/Sentinel connections. You can provide the database, client name, password and timeouts within the RedisURI. You have the following possibilities to create aRedisURI
:- Use an URI:
RedisURI.create("redis://localhost/");
create(String)
for more options - Use the Builder:
RedisURI.Builder.redis("localhost", 6379).withPassword("password").withDatabase(1).build();
RedisURI.Builder.redis(String)
andRedisURI.Builder.sentinel(String)
for more options. - Construct your own instance:
new RedisURI("localhost", 6379, Duration.ofSeconds(60));
RedisURI uri = new RedisURI(); uri.setHost("localhost");
URI syntax
Redis Standaloneredis
Redis Standalone (SSL)://
[[username:
]password@]host [:
port][/
database][?
[timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&libraryName=libraryName] [&libraryVersion=libraryVersion] [&verifyPeer=NONE|CA|FULL]]rediss
Redis Standalone (Unix Domain Sockets)://
[[username:
]password@]host [:
port][/
database][?
[timeout=timeout[d|h|m|s|ms|us|ns]] [ &database=database] [&clientName=clientName] [&libraryName=libraryName] [&libraryVersion=libraryVersion] [&verifyPeer=NONE|CA|FULL]]redis-socket
Redis Sentinel://
[[username:
]password@]path[?
[timeout=timeout[d|h|m|s|ms|us|ns]][&database=database] [&clientName=clientName] [&libraryName=libraryName] [&libraryVersion=libraryVersion] [&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] [&libraryName=libraryName] [&libraryVersion=libraryVersion] [&verifyPeer=NONE|CA|FULL]]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.Authentication
Redis URIs may contain authentication details that effectively lead to usernames with passwords, password-only, or no authentication. Connections are authenticated by using information provided throughRedisCredentials
. Credentials are obtained at connection time fromRedisCredentialsProvider
. When configuring username/password on the URI statically, then aStaticCredentialsProvider
holds the configured information.Notes
- When using Redis Sentinel, the password from the URI applies to the data nodes only. Sentinel authentication must be
configured for each
sentinel node
. - Usernames are supported as of Redis 6.
- Library name and library version are automatically set on Redis 7.2 or greater defaulting to
LettuceVersion.getVersion()
.
- Since:
- 3.0
- Author:
- Mark Paluch, Guy Korland, Johnny Lim, Jon Iantosca, Jacob Halsey
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RedisURI.Builder
Builder for Redis URI.
-
Field Summary
Fields Modifier and Type Field Description static Map<String,LongFunction<Duration>>
CONVERTER_MAP
static int
DEFAULT_REDIS_PORT
The default redis port.static int
DEFAULT_SENTINEL_PORT
The default sentinel port.static long
DEFAULT_TIMEOUT
Default timeout: 60 secstatic Duration
DEFAULT_TIMEOUT_DURATION
static String
PARAMETER_NAME_CLIENT_NAME
static String
PARAMETER_NAME_DATABASE
static String
PARAMETER_NAME_DATABASE_ALT
static String
PARAMETER_NAME_LIBRARY_NAME
static String
PARAMETER_NAME_LIBRARY_VERSION
static String
PARAMETER_NAME_SENTINEL_MASTER_ID
static String
PARAMETER_NAME_TIMEOUT
static String
PARAMETER_NAME_VERIFY_PEER
static String
URI_SCHEME_REDIS
static String
URI_SCHEME_REDIS_SECURE
static String
URI_SCHEME_REDIS_SECURE_ALT
static String
URI_SCHEME_REDIS_SENTINEL
static String
URI_SCHEME_REDIS_SENTINEL_SECURE
static String
URI_SCHEME_REDIS_SOCKET
static String
URI_SCHEME_REDIS_SOCKET_ALT
static String
URI_SCHEME_REDIS_TLS_ALT
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
applyAuthentication(RedisURI source)
Apply authentication from anotherRedisURI
.void
applySsl(RedisURI source)
Apply authentication from anotherRedisURI
.static RedisURI.Builder
builder()
Return a newRedisURI.Builder
to construct aRedisURI
.static RedisURI.Builder
builder(RedisURI source)
Create a newRedisURI.Builder
that is initialized from a plainRedisURI
.static RedisURI
create(String uri)
Create a Redis URI from an URI string.static RedisURI
create(String host, int port)
Create a Redis URI from host and port.static RedisURI
create(URI uri)
Create a Redis URI from an URI string: The uri must follow conventions ofURI
boolean
equals(Object o)
String
getClientName()
Returns the client name.RedisCredentialsProvider
getCredentialsProvider()
Returns theRedisCredentialsProvider
to use to authenticate Redis connections.int
getDatabase()
Returns the Redis database number.String
getHost()
Returns the host.String
getLibraryName()
Returns the library name.String
getLibraryVersion()
Returns the library version.char[]
getPassword()
Deprecated.since 6.2, usegetCredentialsProvider()
instead.int
getPort()
Returns the Redis port.String
getSentinelMasterId()
Returns the Sentinel Master Id.List<RedisURI>
getSentinels()
String
getSocket()
Returns the Unix Domain Socket path.Duration
getTimeout()
Returns the command timeout for synchronous command execution.String
getUsername()
Deprecated.since 6.2, usegetCredentialsProvider()
instead.SslVerifyMode
getVerifyMode()
Returns the mode to verify peers when usingSSL
.int
hashCode()
boolean
isSsl()
Returnstrue
if SSL mode is enabled.boolean
isStartTls()
Returnstrue
if StartTLS is enabled.boolean
isVerifyPeer()
Returns whether to verify peers when usingSSL
.void
setClientName(String clientName)
Sets the client name to be applied on Redis connections.void
setCredentialsProvider(RedisCredentialsProvider credentialsProvider)
Sets theRedisCredentialsProvider
.void
setDatabase(int database)
Sets the Redis database number.void
setHost(String host)
Sets the Redis host.void
setLibraryName(String libraryName)
Sets the library name to be applied on Redis connections.void
setLibraryVersion(String libraryVersion)
Sets the library version to be applied on Redis connections.void
setPassword(char[] password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.void
setPassword(CharSequence password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.void
setPassword(String password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.void
setPort(int port)
Sets the Redis port.void
setSentinelMasterId(String sentinelMasterId)
Sets the Sentinel Master Id.void
setSocket(String socket)
Sets the Unix Domain Socket path.void
setSsl(boolean ssl)
Sets whether to use SSL.void
setStartTls(boolean startTls)
Returns whether StartTLS is enabled.void
setTimeout(Duration timeout)
Sets the command timeout for synchronous command execution.void
setUsername(String username)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.void
setVerifyPeer(boolean verifyPeer)
Sets whether to verify peers when usingSSL
.void
setVerifyPeer(SslVerifyMode verifyMode)
Sets how to verify peers when usingSSL
.String
toString()
URI
toURI()
Creates an URI based on the RedisURI if possible.
-
-
-
Field Detail
-
URI_SCHEME_REDIS_SENTINEL
public static final String URI_SCHEME_REDIS_SENTINEL
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SENTINEL_SECURE
public static final String URI_SCHEME_REDIS_SENTINEL_SECURE
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS
public static final String URI_SCHEME_REDIS
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SECURE
public static final String URI_SCHEME_REDIS_SECURE
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SECURE_ALT
public static final String URI_SCHEME_REDIS_SECURE_ALT
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_TLS_ALT
public static final String URI_SCHEME_REDIS_TLS_ALT
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SOCKET
public static final String URI_SCHEME_REDIS_SOCKET
- See Also:
- Constant Field Values
-
URI_SCHEME_REDIS_SOCKET_ALT
public static final String URI_SCHEME_REDIS_SOCKET_ALT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_TIMEOUT
public static final String PARAMETER_NAME_TIMEOUT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_DATABASE
public static final String PARAMETER_NAME_DATABASE
- See Also:
- Constant Field Values
-
PARAMETER_NAME_DATABASE_ALT
public static final String PARAMETER_NAME_DATABASE_ALT
- See Also:
- Constant Field Values
-
PARAMETER_NAME_SENTINEL_MASTER_ID
public static final String PARAMETER_NAME_SENTINEL_MASTER_ID
- See Also:
- Constant Field Values
-
PARAMETER_NAME_CLIENT_NAME
public static final String PARAMETER_NAME_CLIENT_NAME
- See Also:
- Constant Field Values
-
PARAMETER_NAME_LIBRARY_NAME
public static final String PARAMETER_NAME_LIBRARY_NAME
- See Also:
- Constant Field Values
-
PARAMETER_NAME_LIBRARY_VERSION
public static final String PARAMETER_NAME_LIBRARY_VERSION
- See Also:
- Constant Field Values
-
PARAMETER_NAME_VERIFY_PEER
public static final String PARAMETER_NAME_VERIFY_PEER
- See Also:
- Constant Field Values
-
CONVERTER_MAP
public static final Map<String,LongFunction<Duration>> CONVERTER_MAP
-
DEFAULT_SENTINEL_PORT
public static final int DEFAULT_SENTINEL_PORT
The default sentinel port.- See Also:
- Constant Field Values
-
DEFAULT_REDIS_PORT
public static final int DEFAULT_REDIS_PORT
The default redis port.- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT
public static final long DEFAULT_TIMEOUT
Default timeout: 60 sec- See Also:
- Constant Field Values
-
DEFAULT_TIMEOUT_DURATION
public static final Duration DEFAULT_TIMEOUT_DURATION
-
-
Method Detail
-
builder
public static RedisURI.Builder builder()
Return a newRedisURI.Builder
to construct aRedisURI
.- Returns:
- a new
RedisURI.Builder
to construct aRedisURI
.
-
create
public static RedisURI create(String host, int port)
Create a Redis URI from host and port.- Parameters:
host
- the hostport
- the port- Returns:
- An instance of
RedisURI
containing details from thehost
andport
.
-
create
public static RedisURI create(String uri)
Create a Redis URI from an URI string. The uri must follow conventions ofURI
- Parameters:
uri
- The URI string.- Returns:
- An instance of
RedisURI
containing details from the URI.
-
create
public static RedisURI create(URI uri)
Create a Redis URI from an URI string: The uri must follow conventions ofURI
- Parameters:
uri
- The URI.- Returns:
- An instance of
RedisURI
containing details from the URI.
-
builder
public static RedisURI.Builder builder(RedisURI source)
Create a newRedisURI.Builder
that is initialized from a plainRedisURI
.- Parameters:
source
- the initialization source, must not benull
.- Returns:
- the initialized builder.
- Since:
- 6.0
-
getHost
public String getHost()
Returns the host.- Specified by:
getHost
in interfaceConnectionPoint
- Returns:
- the host.
-
setHost
public void setHost(String host)
Sets the Redis host.- Parameters:
host
- the host
-
getSentinelMasterId
public String getSentinelMasterId()
Returns the Sentinel Master Id.- Returns:
- the Sentinel Master Id.
-
setSentinelMasterId
public void setSentinelMasterId(String sentinelMasterId)
Sets the Sentinel Master Id.- Parameters:
sentinelMasterId
- the Sentinel Master Id.
-
getPort
public int getPort()
Returns the Redis port.- Specified by:
getPort
in 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
public String getSocket()
Returns the Unix Domain Socket path.- Specified by:
getSocket
in interfaceConnectionPoint
- Returns:
- the Unix Domain Socket path.
-
setSocket
public void setSocket(String socket)
Sets the Unix Domain Socket path.- Parameters:
socket
- the Unix Domain Socket path.
-
applyAuthentication
public void applyAuthentication(RedisURI source)
Apply authentication from anotherRedisURI
. The authentication settings of thesource
URI will be applied to this URI. That is in particular theRedisCredentialsProvider
.- Parameters:
source
- must not benull
.- Since:
- 6.0
-
getUsername
@Deprecated public String getUsername()
Deprecated.since 6.2, usegetCredentialsProvider()
instead.Returns the username.- Returns:
- the username
- Since:
- 6.0
-
setUsername
@Deprecated public void setUsername(String username)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.Sets the username.- Parameters:
username
- the username, must not benull
.- Throws:
IllegalStateException
- if aRedisCredentialsProvider
is configured- Since:
- 6.0
-
getPassword
@Deprecated public char[] getPassword()
Deprecated.since 6.2, usegetCredentialsProvider()
instead.Returns the password.- Returns:
- the password
-
setPassword
@Deprecated public void setPassword(String password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.Sets the password. Use empty string to skip authentication.This method is deprecated as of Lettuce 6.0. The reason is that
String
has a strong caching affinity and the JVM cannot easily GCString
instances. Therefore, we suggest using eitherchar[]
or a customCharSequence
(e.g.StringBuilder
or netty'sAsciiString
).- Parameters:
password
- the password, must not benull
.- Throws:
IllegalStateException
- if aRedisCredentialsProvider
is configured- Since:
- 4.4
-
setPassword
@Deprecated public void setPassword(CharSequence password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.Sets the password. Usenull
to skip authentication. Empty password is supported (although not recommended for security reasons).- Parameters:
password
- the password, must not benull
.- Throws:
IllegalStateException
- if aRedisCredentialsProvider
is configured- Since:
- 5.2
-
setPassword
@Deprecated public void setPassword(char[] password)
Deprecated.since 6.2, usesetCredentialsProvider(RedisCredentialsProvider)
instead.Sets the password. Usenull
to skip authentication. Empty password is supported (although not recommended for security reasons).- Parameters:
password
- the password, can benull
.- Throws:
IllegalStateException
- if aRedisCredentialsProvider
is configured- Since:
- 4.4
-
getCredentialsProvider
public RedisCredentialsProvider getCredentialsProvider()
Returns theRedisCredentialsProvider
to use to authenticate Redis connections. Returns a static credentials provider no explicitRedisCredentialsProvider
was configured.- Returns:
- the
RedisCredentialsProvider
to use to authenticate Redis connections - Since:
- 6.2
-
setCredentialsProvider
public void setCredentialsProvider(RedisCredentialsProvider credentialsProvider)
Sets theRedisCredentialsProvider
. Configuring a credentials provider resets the configured static username/password.- Parameters:
credentialsProvider
- the credentials provider to use when authenticating a Redis connection.- Since:
- 6.2
-
getTimeout
public Duration getTimeout()
Returns the command timeout for synchronous command execution.- Returns:
- the Timeout
- Since:
- 5.0
-
setTimeout
public void setTimeout(Duration timeout)
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
public String getClientName()
Returns the client name.- Returns:
- the client name.
- Since:
- 4.4
-
setClientName
public void setClientName(String clientName)
Sets the client name to be applied on Redis connections.- Parameters:
clientName
- the client name.- Since:
- 4.4
-
getLibraryName
public String getLibraryName()
Returns the library name.- Returns:
- the library name.
- Since:
- 6.3
-
setLibraryName
public void setLibraryName(String libraryName)
Sets the library name to be applied on Redis connections.- Parameters:
libraryName
- the library name.- Since:
- 6.3
-
getLibraryVersion
public String getLibraryVersion()
Returns the library version.- Returns:
- the library version.
- Since:
- 6.3
-
setLibraryVersion
public void setLibraryVersion(String libraryVersion)
Sets the library version to be applied on Redis connections.- Parameters:
libraryVersion
- the library version.- Since:
- 6.3
-
applySsl
public void applySsl(RedisURI source)
Apply authentication from anotherRedisURI
. The SSL settings of thesource
URI 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()
Returnstrue
if SSL mode is enabled.- Returns:
true
if 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:
true
to verify peers when usingSSL
.
-
getVerifyMode
public SslVerifyMode 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
-true
to verify peers when usingSSL
.
-
setVerifyPeer
public void setVerifyPeer(SslVerifyMode verifyMode)
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()
Returnstrue
if StartTLS is enabled.- Returns:
true
if 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
-true
if StartTLS is enabled.
-
getSentinels
public List<RedisURI> getSentinels()
- Returns:
- the list of
Redis Sentinel URIs
.
-
toURI
public URI 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
public String toString()
-
-