Skip to content

Commit 55ff415

Browse files
christophstroblThomas Darimont
authored andcommitted
DATAREDIS-324 - Add Redis Sentinel support.
We’ve added RedisSentinelConfiguration holding required information for connecting to redis sentinels. This can be used to set up ConnectionFeactory for HA environments. **Using Jedis** Providing RedisSentinelConfiguration will force the JedisConnectionFactory to use JedisSentinelPool for managing resources. **Using Lettuce/JRedis/SRP** There’s currently no support for sentinel in those clients. **CI Build** We’ve added makefile to build and set up redis instances for testing sentinel support on travis-ci. There’s already a section for redis cluster. The cluster section is for whatever reason currently not working as the cluster nodes won’t start. This will be fixed when we add redis-cluster support. _side note:_ there’s an alternative fork of lettuce at mp911de/lettuce that already has sentinel support.
1 parent c36a58d commit 55ff415

File tree

13 files changed

+898
-12
lines changed

13 files changed

+898
-12
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ pom.xml
1414
.settings
1515
.idea
1616
out
17+
work
18+
*.rdb

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ jdk:
33
- oraclejdk7
44
- openjdk6
55
- oraclejdk8
6-
services:
7-
- redis-server
8-
script: "gradle clean build -DrunLongTests=true"
6+
script: make test

Makefile

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Copyright 2011-2014 the original author or authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
REDIS_VERSION:=2.8.13
16+
17+
#######
18+
# Redis
19+
#######
20+
.PRECIOUS: work/redis-%.conf
21+
22+
work/redis-%.conf:
23+
@mkdir -p $(@D)
24+
25+
echo port $* >> $@
26+
echo daemonize yes >> $@
27+
echo pidfile $(shell pwd)/work/redis-$*.pid >> $@
28+
echo logfile $(shell pwd)/work/redis-$*.log >> $@
29+
echo save \"\" >> $@
30+
echo slaveof 127.0.0.1 6379 >> $@
31+
32+
# Handled separately because it's the master and all others are slaves
33+
work/redis-6379.conf:
34+
@mkdir -p $(@D)
35+
36+
echo port 6379 >> $@
37+
echo daemonize yes >> $@
38+
echo pidfile $(shell pwd)/work/redis-6379.pid >> $@
39+
echo logfile $(shell pwd)/work/redis-6379.log >> $@
40+
echo save \"\" >> $@
41+
42+
work/redis-%.pid: work/redis-%.conf work/redis/bin/redis-server
43+
work/redis/bin/redis-server $<
44+
45+
redis-start: work/redis-6379.pid work/redis-6380.pid work/redis-6381.pid
46+
47+
redis-stop: stop-6379 stop-6380 stop-6381
48+
49+
##########
50+
# Sentinel
51+
##########
52+
.PRECIOUS: work/sentinel-%.conf
53+
54+
work/sentinel-%.conf:
55+
@mkdir -p $(@D)
56+
57+
echo port $* >> $@
58+
echo daemonize yes >> $@
59+
echo pidfile $(shell pwd)/work/sentinel-$*.pid >> $@
60+
echo logfile $(shell pwd)/work/sentinel-$*.log >> $@
61+
echo save \"\" >> $@
62+
echo sentinel monitor mymaster 127.0.0.1 6379 2 >> $@
63+
64+
work/sentinel-%.pid: work/sentinel-%.conf work/redis-6379.pid work/redis/bin/redis-server
65+
work/redis/bin/redis-server $< --sentinel
66+
67+
sentinel-start: work/sentinel-26379.pid work/sentinel-26380.pid work/sentinel-26381.pid
68+
69+
sentinel-stop: stop-26379 stop-26380 stop-26381
70+
71+
########
72+
# Global
73+
########
74+
clean:
75+
rm -rf work/*.conf work/*.log
76+
77+
clobber:
78+
rm -rf work
79+
80+
work/redis/bin/redis-cli work/redis/bin/redis-server:
81+
@mkdir -p work/redis
82+
83+
curl -sSL https://github.com/antirez/redis/archive/$(REDIS_VERSION).tar.gz | tar xzf - -C work
84+
$(MAKE) -C work/redis-$(REDIS_VERSION) -j
85+
$(MAKE) -C work/redis-$(REDIS_VERSION) PREFIX=$(shell pwd)/work/redis install
86+
rm -rf work/redis-$(REDIS_VERSION)
87+
88+
start: redis-start sentinel-start
89+
90+
stop-%: work/redis/bin/redis-cli
91+
-work/redis/bin/redis-cli -p $* shutdown
92+
93+
stop: redis-stop sentinel-stop
94+
95+
test:
96+
$(MAKE) start
97+
sleep 2
98+
$(PWD)/gradlew clean build -DrunLongTests=true
99+
$(MAKE) stop

docs/src/reference/docbook/reference/redis.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@
209209

210210
</section>
211211

212+
<section id="redis:sentinel">
213+
<title>Redis Sentinel Support</title>
214+
<para>For dealing with high available Redis there is support for <ulink url="http://redis.io/topics/sentinel">Redis Sentinel</ulink> using <classname>RedisSentinelConfiguration</classname>.</para>
215+
<note>Please note that currently only <ulink url="http://github.com/xetorthio/jedis">Jedis</ulink> supports Redis Sentinel.</note>
216+
<programlisting language="java"><![CDATA[@Bean
217+
public RedisConnectionFactory jedisConnectionFactory() {
218+
RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
219+
.master("mymaster")
220+
.sentinel("127.0.0.1", 26379)
221+
.sentinel("127.0.0.1", 26380);
222+
return new JedisConnectionFactory(sentinelConfig);
223+
}
224+
]]></programlisting>
225+
</section>
226+
212227
<section id="redis:template">
213228
<title>Working with Objects through <classname>RedisTemplate</classname></title>
214229

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2014 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+
/**
19+
* @author Christoph Strobl
20+
* @since 1.4
21+
*/
22+
public interface NamedNode {
23+
24+
String getName();
25+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2014 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.ObjectUtils;
19+
20+
/**
21+
* @author Christoph Strobl
22+
* @since 1.4
23+
*/
24+
public class RedisNode {
25+
26+
private final String host;
27+
private final int port;
28+
29+
public RedisNode(String host, int port) {
30+
this.host = host;
31+
this.port = port;
32+
}
33+
34+
public String getHost() {
35+
return host;
36+
}
37+
38+
public Integer getPort() {
39+
return port;
40+
}
41+
42+
public String asString() {
43+
return host + ":" + port;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return asString();
49+
}
50+
51+
@Override
52+
public int hashCode() {
53+
final int prime = 31;
54+
int result = 1;
55+
result = prime * result + ObjectUtils.nullSafeHashCode(host);
56+
result = prime * result + ObjectUtils.nullSafeHashCode(port);
57+
return result;
58+
}
59+
60+
@Override
61+
public boolean equals(Object obj) {
62+
63+
if (this == obj) {
64+
return true;
65+
}
66+
if (obj == null || !(obj instanceof RedisNode)) {
67+
return false;
68+
}
69+
70+
RedisNode other = (RedisNode) obj;
71+
72+
if (!ObjectUtils.nullSafeEquals(this.host, other.host)) {
73+
return false;
74+
}
75+
76+
if (!ObjectUtils.nullSafeEquals(this.port, other.port)) {
77+
return false;
78+
}
79+
80+
return true;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)