public class RedisURI extends Object implements Serializable, ConnectionPoint
RedisURI
:
RedisURI.create("redis://localhost/");
create(String)
for more options
RedisURI.Builder.redis("localhost", 6379).withPassword("password").withDatabase(1).build();
RedisURI.Builder.redis(String)
and RedisURI.Builder.sentinel(String)
for
more options.
new RedisURI("localhost", 6379, Duration.ofSeconds(60));
RedisURI uri = new RedisURI(); uri.setHost("localhost");
redisRedis 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
Timeout units
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.Modifier and Type | Class and Description |
---|---|
static class |
RedisURI.Builder
Builder for Redis URI.
|
Modifier and Type | Field and 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 sec
|
static 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_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 |
Constructor and Description |
---|
RedisURI()
Default empty constructor.
|
RedisURI(String host,
int port,
Duration timeout)
Constructor with host/port and timeout.
|
Modifier and Type | Method and Description |
---|---|
void |
applyAuthentication(RedisURI source)
Apply authentication from another
RedisURI . |
void |
applySsl(RedisURI source)
Apply authentication from another
RedisURI . |
static RedisURI.Builder |
builder()
Return a new
RedisURI.Builder to construct a RedisURI . |
static RedisURI.Builder |
builder(RedisURI source)
Create a new
RedisURI.Builder that is initialized from a plain RedisURI . |
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 of
URI |
boolean |
equals(Object o) |
String |
getClientName()
Returns the client name.
|
int |
getDatabase()
Returns the Redis database number.
|
String |
getHost()
Returns the host.
|
char[] |
getPassword()
Returns the password.
|
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()
Returns the username.
|
SslVerifyMode |
getVerifyMode()
Returns the mode to verify peers when using
SSL . |
int |
hashCode() |
boolean |
isSsl()
Returns
true if SSL mode is enabled. |
boolean |
isStartTls()
Returns
true if StartTLS is enabled. |
boolean |
isVerifyPeer()
Returns whether to verify peers when using
SSL . |
void |
setClientName(String clientName)
Sets the client name to be applied on Redis connections.
|
void |
setDatabase(int database)
Sets the Redis database number.
|
void |
setHost(String host)
Sets the Redis host.
|
void |
setPassword(char[] password)
Sets the password.
|
void |
setPassword(CharSequence password)
Sets the password.
|
void |
setPassword(String password)
Deprecated.
since 6.0. Use
setPassword(CharSequence) or setPassword(char[]) to avoid String caching. |
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)
Sets the username.
|
void |
setVerifyPeer(boolean verifyPeer)
Sets whether to verify peers when using
SSL . |
void |
setVerifyPeer(SslVerifyMode verifyMode)
Sets how to verify peers when using
SSL . |
String |
toString() |
URI |
toURI()
Creates an URI based on the RedisURI if possible.
|
public static final String URI_SCHEME_REDIS_SENTINEL
public static final String URI_SCHEME_REDIS_SENTINEL_SECURE
public static final String URI_SCHEME_REDIS
public static final String URI_SCHEME_REDIS_SECURE
public static final String URI_SCHEME_REDIS_SECURE_ALT
public static final String URI_SCHEME_REDIS_TLS_ALT
public static final String URI_SCHEME_REDIS_SOCKET
public static final String URI_SCHEME_REDIS_SOCKET_ALT
public static final String PARAMETER_NAME_TIMEOUT
public static final String PARAMETER_NAME_DATABASE
public static final String PARAMETER_NAME_DATABASE_ALT
public static final String PARAMETER_NAME_SENTINEL_MASTER_ID
public static final String PARAMETER_NAME_CLIENT_NAME
public static final String PARAMETER_NAME_VERIFY_PEER
public static final Map<String,LongFunction<Duration>> CONVERTER_MAP
public static final int DEFAULT_SENTINEL_PORT
public static final int DEFAULT_REDIS_PORT
public static final long DEFAULT_TIMEOUT
public static final Duration DEFAULT_TIMEOUT_DURATION
public static RedisURI.Builder builder()
RedisURI.Builder
to construct a RedisURI
.RedisURI.Builder
to construct a RedisURI
.public static RedisURI create(String host, int port)
host
- the hostport
- the portRedisURI
containing details from the host
and port
.public static RedisURI create(String uri)
URI
uri
- The URI string.RedisURI
containing details from the URI.public static RedisURI create(URI uri)
URI
uri
- The URI.RedisURI
containing details from the URI.public static RedisURI.Builder builder(RedisURI source)
RedisURI.Builder
that is initialized from a plain RedisURI
.source
- the initialization source, must not be null
.public String getHost()
getHost
in interface ConnectionPoint
public void setHost(String host)
host
- the hostpublic String getSentinelMasterId()
public void setSentinelMasterId(String sentinelMasterId)
sentinelMasterId
- the Sentinel Master Id.public int getPort()
getPort
in interface ConnectionPoint
public void setPort(int port)
DEFAULT_REDIS_PORT
.port
- the Redis portpublic String getSocket()
getSocket
in interface ConnectionPoint
public void setSocket(String socket)
socket
- the Unix Domain Socket path.public void applyAuthentication(RedisURI source)
RedisURI
. The authentication settings of the source
URI 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 the source
URI has no
authentication, then this URI's authentication credentials will be reset.source
- must not be null
.public String getUsername()
public void setUsername(String username)
username
- the username, must not be null
.public char[] getPassword()
@Deprecated public void setPassword(String password)
setPassword(CharSequence)
or setPassword(char[])
to avoid String caching.
This method is deprecated as of Lettuce 6.0. The reason is that String
has a strong caching affinity and the JVM
cannot easily GC String
instances. Therefore we suggest using either char[]
or a custom
CharSequence
(e.g. StringBuilder
or netty's AsciiString
).
password
- the password, must not be null
.public void setPassword(CharSequence password)
password
- the password, must not be null
.public void setPassword(char[] password)
password
- the password, can be null
.public Duration getTimeout()
public void setTimeout(Duration timeout)
timeout
- the command timeout for synchronous command execution.public int getDatabase()
public void setDatabase(int database)
database
- the Redis database number.public String getClientName()
public void setClientName(String clientName)
clientName
- the client name.public void applySsl(RedisURI source)
RedisURI
. The SSL settings of the source
URI will be applied to this
URI. That is in particular SSL usage, peer verification and StartTLS.source
- must not be null
.public boolean isSsl()
true
if SSL mode is enabled.true
if SSL mode is enabled.public void setSsl(boolean ssl)
ssl
- public boolean isVerifyPeer()
SSL
.true
to verify peers when using SSL
.public SslVerifyMode getVerifyMode()
SSL
.public void setVerifyPeer(boolean verifyPeer)
SSL
. Sets peer verification also for already configured Redis
Sentinel nodes.verifyPeer
- true
to verify peers when using SSL
.public void setVerifyPeer(SslVerifyMode verifyMode)
SSL
. Sets peer verification also for already configured Redis
Sentinel nodes.verifyMode
- verification mode to use when using SSL
.public boolean isStartTls()
true
if StartTLS is enabled.true
if StartTLS is enabled.public void setStartTls(boolean startTls)
startTls
- true
if StartTLS is enabled.public List<RedisURI> getSentinels()
Redis Sentinel URIs
.public URI toURI()
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
.
IllegalStateException
- if the URI cannot be rendered.public String toString()
Copyright © 2022 lettuce.io. All rights reserved.