Skip to content

Commit de268d9

Browse files
committed
Add auto-configuration for reactive Redis
This commit provides an auto-configuration for reactive Redis and a starter that provides Lettuce as Jedis doesn't support reactive operations. There are no support for reactive redis repositories at the moment so only a `ReactiveRedisTemplate` is auto-configured if necessary. Closes gh-8053
1 parent 0fbe903 commit de268d9

File tree

8 files changed

+216
-3
lines changed

8 files changed

+216
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import reactor.core.publisher.Flux;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.core.io.ResourceLoader;
29+
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
30+
import org.springframework.data.redis.core.ReactiveRedisTemplate;
31+
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
32+
import org.springframework.data.redis.serializer.RedisSerializationContext;
33+
34+
/**
35+
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive Redis
36+
* support.
37+
*
38+
* @author Mark Paluch
39+
* @author Stephane Nicoll
40+
* @since 2.0.0
41+
*/
42+
@Configuration
43+
@ConditionalOnClass({ ReactiveRedisConnectionFactory.class, ReactiveRedisTemplate.class,
44+
Flux.class })
45+
@AutoConfigureAfter(RedisAutoConfiguration.class)
46+
public class RedisReactiveAutoConfiguration {
47+
48+
@Bean
49+
@ConditionalOnMissingBean(name = "reactiveRedisTemplate")
50+
@ConditionalOnBean(ReactiveRedisConnectionFactory.class)
51+
public ReactiveRedisTemplate<Object, Object> reactiveRedisTemplate(
52+
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory,
53+
ResourceLoader resourceLoader) {
54+
55+
JdkSerializationRedisSerializer jdkSerializer =
56+
new JdkSerializationRedisSerializer(resourceLoader.getClassLoader());
57+
58+
RedisSerializationContext<Object, Object> serializationContext =
59+
RedisSerializationContext.newSerializationContext()
60+
.key(jdkSerializer)
61+
.value(jdkSerializer)
62+
.hashKey(jdkSerializer)
63+
.hashValue(jdkSerializer).build();
64+
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory,
65+
serializationContext);
66+
}
67+
68+
}

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\
4949
org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\
5050
org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\
5151
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\
52+
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration,\
5253
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\
5354
org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\
5455
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import java.util.Map;
20+
21+
import org.junit.After;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.test.util.EnvironmentTestUtils;
25+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.data.redis.core.ReactiveRedisTemplate;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link RedisReactiveAutoConfiguration}.
32+
*
33+
* @author Stephane Nicoll
34+
*/
35+
public class RedisReactiveAutoConfigurationTests {
36+
37+
private AnnotationConfigApplicationContext context;
38+
39+
@After
40+
public void close() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
46+
@Test
47+
public void testDefaultRedisConfiguration() {
48+
load();
49+
Map<String, ReactiveRedisTemplate> beans = this.context.getBeansOfType(
50+
ReactiveRedisTemplate.class);
51+
assertThat(beans).containsOnlyKeys("reactiveRedisTemplate");
52+
}
53+
54+
55+
private void load(String... environment) {
56+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
57+
EnvironmentTestUtils.addEnvironment(ctx, environment);
58+
ctx.register(RedisAutoConfiguration.class, RedisReactiveAutoConfiguration.class);
59+
ctx.refresh();
60+
this.context = ctx;
61+
}
62+
63+
}

spring-boot-dependencies/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,11 @@
389389
<artifactId>spring-boot-starter-data-redis</artifactId>
390390
<version>2.0.0.BUILD-SNAPSHOT</version>
391391
</dependency>
392+
<dependency>
393+
<groupId>org.springframework.boot</groupId>
394+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
395+
<version>2.0.0.BUILD-SNAPSHOT</version>
396+
</dependency>
392397
<dependency>
393398
<groupId>org.springframework.boot</groupId>
394399
<artifactId>spring-boot-starter-data-neo4j</artifactId>

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,9 +3264,12 @@ http://redis.io/[Redis] is a cache, message broker and richly-featured key-value
32643264
Spring Boot offers basic auto-configuration for the
32653265
https://github.com/xetorthio/jedis/[Jedis] and and https://github.com/mp911de/lettuce/[Lettuce]
32663266
client library and abstractions on top of it provided by
3267-
https://github.com/spring-projects/spring-data-redis[Spring Data Redis]. There
3268-
is a `spring-boot-starter-data-redis` '`Starter`' for collecting the dependencies in a
3269-
convenient way that uses https://github.com/xetorthio/jedis/[Jedis] by default.
3267+
https://github.com/spring-projects/spring-data-redis[Spring Data Redis].
3268+
3269+
There is a `spring-boot-starter-data-redis` '`Starter`' for collecting the dependencies in
3270+
a convenient way that uses https://github.com/xetorthio/jedis/[Jedis] by default. If you
3271+
are building a reactive application, the `spring-boot-starter-data-redis-reactive`
3272+
'`Starter`' will get you going.
32703273

32713274

32723275

spring-boot-starters/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<module>spring-boot-starter-data-mongodb-reactive</module>
3939
<module>spring-boot-starter-data-neo4j</module>
4040
<module>spring-boot-starter-data-redis</module>
41+
<module>spring-boot-starter-data-redis-reactive</module>
4142
<module>spring-boot-starter-data-rest</module>
4243
<module>spring-boot-starter-data-solr</module>
4344
<module>spring-boot-starter-freemarker</module>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.springframework.boot</groupId>
6+
<artifactId>spring-boot-starters</artifactId>
7+
<version>2.0.0.BUILD-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
10+
<name>Spring Boot Data Redis Reactive Starter</name>
11+
<description>Starter for using Redis key-value data store with Spring Data Redis
12+
reactive and the Lettuce client</description>
13+
<url>http://projects.spring.io/spring-boot/</url>
14+
<organization>
15+
<name>Pivotal Software, Inc.</name>
16+
<url>http://www.spring.io</url>
17+
</organization>
18+
<properties>
19+
<main.basedir>${basedir}/../..</main.basedir>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.data</groupId>
28+
<artifactId>spring-data-redis</artifactId>
29+
<exclusions>
30+
<exclusion>
31+
<groupId>org.slf4j</groupId>
32+
<artifactId>jcl-over-slf4j</artifactId>
33+
</exclusion>
34+
</exclusions>
35+
</dependency>
36+
<dependency>
37+
<groupId>io.lettuce</groupId>
38+
<artifactId>lettuce-core</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.apache.commons</groupId>
42+
<artifactId>commons-pool2</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>io.projectreactor</groupId>
46+
<artifactId>reactor-core</artifactId>
47+
</dependency>
48+
</dependencies>
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.basepom.maven</groupId>
53+
<artifactId>duplicate-finder-maven-plugin</artifactId>
54+
<executions>
55+
<execution>
56+
<id>duplicate-dependencies</id>
57+
<phase>validate</phase>
58+
<goals>
59+
<goal>check</goal>
60+
</goals>
61+
<configuration>
62+
<ignoredResourcePatterns>
63+
<ignoredResourcePattern>changelog.txt</ignoredResourcePattern>
64+
</ignoredResourcePatterns>
65+
</configuration>
66+
</execution>
67+
</executions>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
provides: spring-data-redis,lettuce-core

0 commit comments

Comments
 (0)