Redis
集群配置和部署
1.
下载redis3.0
2.
上传服务器,解压,编译(前两步与之前相同)http://blog.csdn.net/liguangyan_neu/article/details/78026932 参考2.1节
3.
创建集群需要的目录
mkdir /usr/local/cluster
cd /usr/local/cluster
mkdir 7000 7001 7002 7003 7004 7005
4.
修改配置文件redis.conf
cp /usr/local/redis3.0.5/redis.conf /usr/local/cluster
vi redis.conf
修改配置文件中如下选项
port 7000
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
修改完配置文件后将redis.conf
文件分别拷贝至
7000
、
7001……7005
目录下
cp /usr/local/cluster/redis.conf /usr/local/cluster/7000
cp /usr/local/cluster/redis.conf /usr/local/cluster/7001
cp /usr/local/cluster/redis.conf /usr/local/cluster/7002
cp /usr/local/cluster/redis.conf /usr/local/cluster/7003
cp /usr/local/cluster/redis.conf /usr/local/cluster/7004
cp /usr/local/cluster/redis.conf /usr/local/cluster/7005
拷贝结束之后分别修改对应的配置文件中的端口port
为对应的端口号
5.
分别启动六个redis
实例
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7000/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7001/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7002/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7003/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7004/redis.conf
/usr/local/redis-3.0.5/src/redis-server /usr/local/cluster/7005/redis.conf
查看启动情况
ps -ef|grep redis
6.
升级ruby
安装
gem
http://jingyan.baidu.com/article/b7001fe173fe9a0e7382dd57.html
7. gem
安装
redis ruby
接口
http://storysky.blog.51cto.com/628458/1155353/
8.
执行redis
创建集群命令创建集群
/usr/local/redis-3.0.5/src/redis-trib.rb create --replicas 1 192.168.1.191:7000 192.168.1.191:7001 192.168.1.191:7002 192.168.1.191:7003
192.168.1.191:7004
192.168.1.191:7005
9.
进入集群环境使用redis-cli
/usr/local/redis-3.0.5/src/redis-cli -c -h 192.168.1.191 -p 7000
10. RedisCluster
客户端(
Jedis
)
使用spring
集成
JedisCluster
连接
Redis3.0
集群
Ø
Maven
依赖
redis.clients
2.7.2
Ø
增加spring
配置
"genericObjectPoolConfig"
class
=
"org.apache.commons.pool2.impl.GenericObjectPoolConfig"
>
"maxWaitMillis"
value=
"-1"
/>
"maxTotal"
value=
"1000"
/>
"maxIdle"
value=
"100"
/>
"jedisCluster"
class
=
"xxx.JedisClusterFactory"
>
classpath:connect-redis.properties
"addressKeyPrefix"
value=
"address"
/>
"timeout"
value=
"300000"
/>
"maxRedirections"
value=
"6"
/>
"genericObjectPoolConfig"
ref=
"genericObjectPoolConfig"
/>
Ø
增加
connect-redis.properties
配置文件
address1=
192.168.1.191:7000
address2=
1
92.168.1.191:7001
address3=
1
92.168.1.191:7002
address4=
1
92.168.1.191:7003
address5=
1
92.168.1.191:7004
address6=
1
92.168.1.191:7005
Ø
增加java
类
JedisClusterFactory
1. import
java.util.HashSet;
2. import
java.util.Properties;
3. import
java.util.Set;
4. import
java.util.regex.Pattern;
5.
6. import
org.apache.commons.pool2.impl.GenericObjectPoolConfig;
7. import
org.springframework.beans.factory.FactoryBean;
8. import
org.springframework.beans.factory.InitializingBean;
9. import
org.springframework.core.io.Resource;
10.
11. import
redis.clients.jedis.HostAndPort;
12. import
redis.clients.jedis.JedisCluster;
13.
14. public
class
JedisClusterFactory
implements
FactoryBean, InitializingBean {
15.
16.
private
Resource addressConfig;
17.
private
String addressKeyPrefix ;
18.
19.
private
JedisCluster jedisCluster;
20.
private
Integer timeout;
21.
private
Integer maxRedirections;
22.
private
GenericObjectPoolConfig genericObjectPoolConfig;
23.
24.
private
Pattern p = Pattern.compile(
"^.+[:]\\d{1,5}\\s*$"
);
25.
26.
@Override
27.
public
JedisCluster getObject()
throws
Exception {
28.
return
jedisCluster;
29. }
30.
31.
@Override
32.
public
Class
extends
JedisCluster> getObjectType() {
33.
return
(
this
.jedisCluster !=
null
?
this
.jedisCluster.getClass() : JedisCluster.
class
);
34. }
35.
36.
@Override
37.
public
boolean
isSingleton() {
38.
return
true
;
39. }
40.
41.
42.
43.
private
Set parseHostAndPort()
throws
Exception {
44.
try
{
45. Properties prop =
new
Properties();
46. prop.load(
this
.addressConfig.getInputStream());
47.
48. Set haps =
new
HashSet();
49.
for
(Object key : prop.keySet()) {
50.
51.
if
(!((String) key).startsWith(addressKeyPrefix)) {
52.
continue
;
53. }
54.
55. String val = (String) prop.get(key);
56.
57.
boolean
isIpPort = p.matcher(val).matches();
58.
59.
if
(!isIpPort) {
60.
throw
new
IllegalArgumentException(
"ip
或
port
不合法
"
);
61. }
62. String[] ipAndPort = val.split(
":"
);
63.
64. HostAndPort hap =
new
HostAndPort(ipAndPort[
0
], Integer.parseInt(ipAndPort[
1
]));
65. haps.add(hap);
66. }
67.
68.
return
haps;
69. }
catch
(IllegalArgumentException ex) {
70.
throw
ex;
71. }
catch
(Exception ex) {
72.
throw
new
Exception(
"
解析
jedis
配置文件失败
"
, ex);
73. }
74. }
75.
76.
@Override
77.
public
void
afterPropertiesSet()
throws
Exception {
78. Set haps =
this
.parseHostAndPort();
79.
80. jedisCluster =
new
JedisCluster(haps, timeout, maxRedirections,genericObjectPoolConfig);
81.
82. }
83.
public
void
setAddressConfig(Resource addressConfig) {
84.
this
.addressConfig = addressConfig;
85. }
86.
87.
public
void
setTimeout(
int
timeout) {
88.
this
.timeout = timeout;
89. }
90.
91.
public
void
setMaxRedirections(
int
maxRedirections) {
92.
this
.maxRedirections = maxRedirections;
93. }
94.
95.
public
void
setAddressKeyPrefix(String addressKeyPrefix) {
96.
this
.addressKeyPrefix = addressKeyPrefix;
97. }
98.
99.
public
void
setGenericObjectPoolConfig(GenericObjectPoolConfig genericObjectPoolConfig) {
100.
this
.genericObjectPoolConfig = genericObjectPoolConfig;
101. }
102.
103. }
Ø
使用时直接注入
@Autowired
JedisCluster jedisCluster
;