Skip to content

Spring Support

Mark Paluch edited this page Sep 17, 2018 · 11 revisions

Use Lettuce with Spring to manage the RedisClient and the RedisClusterClient. You need to specify a RedisURI or a URI string in order to create the client.

You can integrate with Lettuce either by using Spring Data Redis (recommended) or standalone by providing Java/XML configuration.

Spring Data Redis

Using Lettuce through Spring Data Redis provides familiar Spring abstractions for Redis usage. It integrates well with other Spring components such as Spring Session or Spring Boot. See Spring Data Redis reference documentation for lettuce usage.

Redis Client

Lettuce Standalone/Sentinel/Pub/Sub and Master/Replica can be used through RedisClient. You can provide bean definitions to manage Lettuce resources inside a Spring context. Bean management can take care of resource allocation and clean up through Spring’s bean lifecycle management. Using managed beans gives you the possibility to access lettuce’s RedisClient/connections from various places inside your code. Your code can also benefit from dependency injection.

Java Configuration

Configuring lettuce using Spring’s Java Configuration with @Bean definitions requires you to provide bean definitions. Notice the specification of destroyMethod to clean up resources once the application context is shut down.

@Configuration
public class LettuceConfig {

    @Bean(destroyMethod = "shutdown")
    ClientResources clientResources() {
        return DefaultClientResources.create();
    }

    @Bean(destroyMethod = "shutdown")
    RedisClient redisClient(ClientResources clientResources) {

        return RedisClient.create(clientResources, RedisURI.create(TestSettings.host(), TestSettings.port()));
    }

    @Bean(destroyMethod = "close")
    StatefulRedisConnection<String, String> connection(RedisClient redisClient) {
        return redisClient.connect();
    }
}

XML Configuration

Use RedisClientFactoryBean to create a managed instance of RedisClient using XML-based configuration.

<bean id="redisClient" class="com.lambdaworks.redis.support.RedisClientFactoryBean">
    <property name="password" value="mypassword"/>
    <!-- Redis URI Format: redis://host[:port]/database -->
    <!-- Redis URI: Specify Database as Path -->
    <property name="uri" value="redis://localhost/12"/>

    <!-- Redis Sentinel URI Format: redis-sentinel://host[:port][,host[:port][,host[:port]]/database#masterId -->
    <!-- Redis Sentinel URI: You can specify multiple sentinels. Specify Database as Path, Master Id as Fragment. -->
    <property name="uri" value="redis-sentinel://localhost,localhost2,localhost3/1#myMaster"/>
</bean>

Redis Cluster Client

Lettuce Redis Cluster support can be used through RedisClusterClient. You can provide bean definitions to manage Lettuce resources inside a Spring context. Bean management can take care of resource allocation and clean up through Spring’s bean lifecycle management. Using managed beans gives you the possibility to access Lettuce’s RedisClusterClient/connections from various places inside your code. Your code can also benefit from dependency injection.

Java Configuration

Configuring Lettuce using Spring’s Java Configuration with @Bean definitions requires you to provide bean definitions. Notice the specification of destroyMethod to clean up resources once the application context is shut down.

@Configuration
public class LettuceConfig {

    @Bean(destroyMethod = "shutdown")
    ClientResources clientResources() {
        return DefaultClientResources.create();
    }

    @Bean(destroyMethod = "shutdown")
    RedisClusterClient redisClusterClient(ClientResources clientResources) {

        RedisURI redisURI = RedisURI.create(TestSettings.host(), 7379);

        return RedisClusterClient.create(clientResources, redisURI);
    }

    @Bean(destroyMethod = "close")
    StatefulRedisClusterConnection<String, String> clusterConnection(RedisClusterClient clusterClient) {
        return clusterClient.connect();
    }
}

XML Configuration

Use RedisClusterClientFactoryBean to create a managed instance of RedisClusterClient using XML-based configuration.

<bean id="redisClient" class="com.lambdaworks.redis.support.RedisClusterClientFactoryBean">
    <property name="password" value="mypassword"/>
    <!-- Redis URI Format: redis://host[:port]/database -->
    <!-- Redis URI: Specify Database as Path -->
    <property name="uri" value="redis://localhost/12"/>
</bean>
Clone this wiki locally