Class 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 a RedisURI:
    • Use an URI:

      RedisURI.create("redis://localhost/");

      See create(String) for more options
    • Use the Builder:

      RedisURI.Builder.redis("localhost", 6379).withPassword("password").withDatabase(1).build();

      See RedisURI.Builder.redis(String) and RedisURI.Builder.sentinel(String) for more options.
    • Construct your own instance:

      new RedisURI("localhost", 6379, Duration.ofSeconds(60));

      or

      RedisURI uri = new RedisURI(); uri.setHost("localhost");

    URI syntax

    Redis Standalone
    redis://[[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 Standalone (SSL)
    rediss://[[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 Standalone (Unix Domain Sockets)
    redis-socket:// [[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
    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 through RedisCredentials. Credentials are obtained at connection time from RedisCredentialsProvider. When configuring username/password on the URI statically, then a StaticCredentialsProvider 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
    • Constructor Detail

      • RedisURI

        public RedisURI()
        Default empty constructor.
      • RedisURI

        public RedisURI​(String host,
                        int port,
                        Duration timeout)
        Constructor with host/port and timeout.
        Parameters:
        host - the host
        port - the port
        timeout - timeout value
    • Method Detail

      • create

        public static RedisURI create​(String host,
                                      int port)
        Create a Redis URI from host and port.
        Parameters:
        host - the host
        port - the port
        Returns:
        An instance of RedisURI containing details from the host and port.
      • create

        public static RedisURI create​(String uri)
        Create a Redis URI from an URI string. The uri must follow conventions of URI
        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 of URI
        Parameters:
        uri - The URI.
        Returns:
        An instance of RedisURI containing details from the URI.
      • builder

        public static RedisURI.Builder builder​(RedisURI source)
        Create a new RedisURI.Builder that is initialized from a plain RedisURI.
        Parameters:
        source - the initialization source, must not be null.
        Returns:
        the initialized builder.
        Since:
        6.0
      • 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 interface ConnectionPoint
        Returns:
        the Redis port
      • setPort

        public void setPort​(int port)
        Sets the Redis port. Defaults to DEFAULT_REDIS_PORT.
        Parameters:
        port - the Redis port
      • getSocket

        public String getSocket()
        Returns the Unix Domain Socket path.
        Specified by:
        getSocket in interface ConnectionPoint
        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 another RedisURI. The authentication settings of the source URI will be applied to this URI. That is in particular the RedisCredentialsProvider.
        Parameters:
        source - must not be null.
        Since:
        6.0
      • setCredentialsProvider

        public void setCredentialsProvider​(RedisCredentialsProvider credentialsProvider)
        Sets the RedisCredentialsProvider. 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 another RedisURI. The SSL settings of the source URI will be applied to this URI. That is in particular SSL usage, peer verification and StartTLS.
        Parameters:
        source - must not be null.
        Since:
        6.0
      • isSsl

        public boolean isSsl()
        Returns true 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 using SSL.
        Returns:
        true to verify peers when using SSL.
      • getVerifyMode

        public SslVerifyMode getVerifyMode()
        Returns the mode to verify peers when using SSL.
        Returns:
        the verification mode
        Since:
        6.1
      • setVerifyPeer

        public void setVerifyPeer​(boolean verifyPeer)
        Sets whether to verify peers when using SSL. Sets peer verification also for already configured Redis Sentinel nodes.
        Parameters:
        verifyPeer - true to verify peers when using SSL.
      • setVerifyPeer

        public void setVerifyPeer​(SslVerifyMode verifyMode)
        Sets how to verify peers when using SSL. Sets peer verification also for already configured Redis Sentinel nodes.
        Parameters:
        verifyMode - verification mode to use when using SSL.
        Since:
        6.1
      • isStartTls

        public boolean isStartTls()
        Returns true 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.
      • 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()
        Overrides:
        toString in class Object
        Returns:
        the RedisURL in a URI-like form.
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object