Skip to content

Commit d08ab93

Browse files
mp911dechristophstrobl
authored andcommitted
DATAREDIS-682 - Connect to Redis using unix domain sockets.
We now support Redis connections using OS-native transports through unix domain sockets when using the Lettuce driver. Domain sockets do not require a roundtrip through the networking layer but directly use native epoll or kqueue interfaces. LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisSocketConfiguration("/var/run/redis.sock")); Unix domain socket support requires netty's native epoll/kqueue dependencies matching the runtime environment: <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport-native-epoll</artifactId> <classifier>linux-x86_64</classifier> <version>${netty}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-transport-native-kqueue</artifactId> <classifier>osx-x86_64</classifier> <version>${netty}</version> </dependency> Original Pull Request: #286
1 parent 5c16a60 commit d08ab93

File tree

10 files changed

+375
-42
lines changed

10 files changed

+375
-42
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ work/redis-%.conf:
2929
echo notify-keyspace-events Ex >> $@
3030
echo pidfile $(shell pwd)/work/redis-$*.pid >> $@
3131
echo logfile $(shell pwd)/work/redis-$*.log >> $@
32+
echo unixsocket $(shell pwd)/work/redis-$*.sock >> $@
33+
echo unixsocketperm 755 >> $@
3234
echo save \"\" >> $@
3335
echo slaveof 127.0.0.1 6379 >> $@
3436

@@ -42,6 +44,8 @@ work/redis-6379.conf:
4244
echo notify-keyspace-events Ex >> $@
4345
echo pidfile $(shell pwd)/work/redis-6379.pid >> $@
4446
echo logfile $(shell pwd)/work/redis-6379.log >> $@
47+
echo unixsocket $(shell pwd)/work/redis-6379.sock >> $@
48+
echo unixsocketperm 755 >> $@
4549
echo save \"\" >> $@
4650

4751
work/redis-%.pid: work/redis-%.conf work/redis/bin/redis-server
@@ -122,7 +126,7 @@ cluster-init: cluster-start cluster-meet cluster-slots
122126
# Global
123127
########
124128
clean:
125-
rm -rf work/*.conf work/*.log
129+
rm -rf work/*.conf work/*.log dump.rdb
126130

127131
clobber:
128132
rm -rf work

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<lettuce>5.0.1.RELEASE</lettuce>
2525
<jedis>2.9.0</jedis>
2626
<multithreadedtc>1.01</multithreadedtc>
27+
<netty>4.1.15.Final</netty>
2728
<java-module-name>spring.data.redis</java-module-name>
2829
</properties>
2930

@@ -85,6 +86,22 @@
8586
<optional>true</optional>
8687
</dependency>
8788

89+
<dependency>
90+
<groupId>io.netty</groupId>
91+
<artifactId>netty-transport-native-epoll</artifactId>
92+
<classifier>linux-x86_64</classifier>
93+
<version>${netty}</version>
94+
<scope>test</scope>
95+
</dependency>
96+
97+
<dependency>
98+
<groupId>io.netty</groupId>
99+
<artifactId>netty-transport-native-kqueue</artifactId>
100+
<classifier>osx-x86_64</classifier>
101+
<version>${netty}</version>
102+
<scope>test</scope>
103+
</dependency>
104+
88105
<!-- reactive -->
89106
<dependency>
90107
<groupId>io.projectreactor</groupId>

src/main/asciidoc/new-features.adoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
New and noteworthy in the latest releases.
55

6-
[[new-in-2.0.0]]
6+
[[new-in-2.1.0]]
7+
== New in Spring Data Redis 2.1
8+
9+
* Unix domain socket connections.
10+
711
== New in Spring Data Redis 2.0
812

913
* Upgrade to Java 8.

src/main/asciidoc/reference/redis.adoc

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -39,63 +39,77 @@ NOTE: Depending on the underlying configuration, the factory can return a new co
3939

4040
The easiest way to work with a `RedisConnectionFactory` is to configure the appropriate connector through the IoC container and inject it into the using class.
4141

42-
IMPORTANT: Unfortunately, currently, not all connectors support all Redis features. When invoking a method on the Connection API that is unsupported by the underlying library, an `UnsupportedOperationException` is thrown.
43-
This situation is likely to be fixed in the future, as the various connectors mature.
42+
IMPORTANT: Unfortunately, currently, not all connectors support all Redis features. When invoking a method on the Connection API that is unsupported by the underlying library, an `UnsupportedOperationException` is thrown.
4443

45-
[[redis:connectors:jedis]]
46-
=== Configuring Jedis connector
44+
[[redis:connectors:lettuce]]
45+
=== Configuring Lettuce connector
4746

48-
http://github.com/xetorthio/jedis[Jedis] is one of the connectors supported by the Spring Data Redis module through the `org.springframework.data.redis.connection.jedis` package. In its simplest form, the Jedis configuration looks as follow:
47+
https://github.com/lettuce-io/lettuce-core[Lettuce] is a http://netty.io/[netty]-based open-source connector supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
4948

50-
[source,xml]
49+
[source,java]
5150
----
52-
<?xml version="1.0" encoding="UTF-8"?>
53-
<beans xmlns="http://www.springframework.org/schema/beans"
54-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
51+
@Configuration
52+
class AppConfig {
5653
57-
<!-- Jedis ConnectionFactory -->
58-
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"/>
54+
@Bean
55+
public LettuceConnectionFactory redisConnectionFactory() {
5956
60-
</beans>
57+
return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
58+
}
59+
}
6160
----
6261

63-
For production use however, one might want to tweak the settings such as the host or password:
62+
There are also a few Lettuce-specific connection parameters that can be tweaked. By default, all `LettuceConnection` s created by the `LettuceConnectionFactory` share the same thread-safe native connection for all non-blocking and non-transactional operations. Set `shareNativeConnection` to false to use a dedicated connection each time. `LettuceConnectionFactory` can also be configured with a `LettucePool` to use for pooling blocking and transactional connections, or all connections if `shareNativeConnection` is set to false.
6463

65-
[source,xml]
64+
Lettuce integrates with netty's http://netty.io/wiki/native-transports.html[native transports] allowing to use unix domain sockets to communicate with Redis. Make sure to include the appropriate native transport dependencies that match your runtime environment.
65+
66+
[source,java]
6667
----
67-
<?xml version="1.0" encoding="UTF-8"?>
68-
<beans xmlns="http://www.springframework.org/schema/beans"
69-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
70-
xmlns:p="http://www.springframework.org/schema/p"
71-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
68+
@Configuration
69+
class AppConfig {
7270
73-
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="server" p:port="6379" />
71+
@Bean
72+
public LettuceConnectionFactory redisConnectionFactory() {
7473
75-
</beans>
74+
return new LettuceConnectionFactory(new RedisSocketConfiguration("/var/run/redis.sock"));
75+
}
76+
}
7677
----
7778

78-
[[redis:connectors:lettuce]]
79-
=== Configuring Lettuce connector
79+
NOTE: Netty currently supports epoll (Linux) and kqueue (BSD/macOS) interfaces for OS-native transport.
8080

81-
https://github.com/mp911de/lettuce[Lettuce] is a http://netty.io/[netty]-based open-source connector supported by Spring Data Redis through the `org.springframework.data.redis.connection.lettuce` package.
81+
[[redis:connectors:jedis]]
82+
=== Configuring Jedis connector
8283

83-
Its configuration is probably easy to guess:
84+
http://github.com/xetorthio/jedis[Jedis] is a community-driven connector supported by the Spring Data Redis module through the `org.springframework.data.redis.connection.jedis` package. In its simplest form, the Jedis configuration looks as follow:
8485

85-
[source,xml]
86+
[source,java]
8687
----
87-
<?xml version="1.0" encoding="UTF-8"?>
88-
<beans xmlns="http://www.springframework.org/schema/beans"
89-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
90-
xmlns:p="http://www.springframework.org/schema/p"
91-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
88+
@Configuration
89+
class AppConfig {
9290
93-
<bean id="lettuceConnectionFactory" class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory" p:host-name="server" p:port="6379"/>
91+
@Bean
92+
public JedisConnectionFactory redisConnectionFactory() {
93+
return new JedisConnectionFactory();
94+
}
95+
}
96+
----
9497

95-
</beans>
98+
For production use however, one might want to tweak the settings such as the host or password:
99+
100+
[source,java]
96101
----
102+
@Configuration
103+
class RedisConfiguration {
97104
98-
There are also a few Lettuce-specific connection parameters that can be tweaked. By default, all `LettuceConnection` s created by the `LettuceConnectionFactory` share the same thread-safe native connection for all non-blocking and non-transactional operations. Set `shareNativeConnection` to false to use a dedicated connection each time. `LettuceConnectionFactory` can also be configured with a `LettucePool` to use for pooling blocking and transactional connections, or all connections if `shareNativeConnection` is set to false.
105+
@Bean
106+
public JedisConnectionFactory redisConnectionFactory() {
107+
108+
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("server", 6379);
109+
return new JedisConnectionFactory(config);
110+
}
111+
}
112+
----
99113

100114
[[redis:sentinel]]
101115
== Redis Sentinel Support
@@ -105,7 +119,7 @@ For dealing with high available Redis there is support for http://redis.io/topic
105119
[source,java]
106120
----
107121
/**
108-
* jedis
122+
* Jedis
109123
*/
110124
@Bean
111125
public RedisConnectionFactory jedisConnectionFactory() {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright 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+
package org.springframework.data.redis.connection;
17+
18+
import org.springframework.util.Assert;
19+
20+
/**
21+
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} by connecting to a
22+
* single node <a href="http://redis.io/">Redis</a> using a local unix domain socket.
23+
*
24+
* @author Mark Paluch
25+
* @since 2.1
26+
*/
27+
public class RedisSocketConfiguration {
28+
29+
private static final String DEFAULT_SOCKET = "/tmp/redis.sock";
30+
31+
private String socket = DEFAULT_SOCKET;
32+
private int database;
33+
private RedisPassword password = RedisPassword.none();
34+
35+
/**
36+
* Create a new default {@link RedisSocketConfiguration}.
37+
*/
38+
public RedisSocketConfiguration() {}
39+
40+
/**
41+
* Create a new {@link RedisSocketConfiguration} given {@code socket}.
42+
*
43+
* @param socket must not be {@literal null} or empty.
44+
*/
45+
public RedisSocketConfiguration(String socket) {
46+
47+
Assert.hasText(socket, "Socket path must not be null or empty!");
48+
49+
this.socket = socket;
50+
}
51+
52+
/**
53+
* @return path to the Redis socket.
54+
*/
55+
public String getSocket() {
56+
return socket;
57+
}
58+
59+
/**
60+
* @param socket path to the Redis socket.
61+
*/
62+
public void setSocket(String socket) {
63+
this.socket = socket;
64+
}
65+
66+
/**
67+
* @return the db index.
68+
*/
69+
public int getDatabase() {
70+
return database;
71+
}
72+
73+
/**
74+
* Sets the index of the database used by this connection factory. Default is 0.
75+
*
76+
* @param index database index.
77+
*/
78+
public void setDatabase(int index) {
79+
80+
Assert.isTrue(index >= 0, () -> String.format("Invalid DB index '%s' (a positive index required)", index));
81+
82+
this.database = index;
83+
}
84+
85+
/**
86+
* @return never {@literal null}.
87+
*/
88+
public RedisPassword getPassword() {
89+
return password;
90+
}
91+
92+
/**
93+
* @param password must not be {@literal null}.
94+
*/
95+
public void setPassword(RedisPassword password) {
96+
97+
Assert.notNull(password, "RedisPassword must not be null!");
98+
99+
this.password = password;
100+
}
101+
}

0 commit comments

Comments
 (0)