品优购电商系统部署 Day 1 十五

114 阅读2分钟
4.3
连接
Redis-Cluster

4.3.1
客户端工具连接

Redis-cli
连接集群:

redis-cli -p
主机
ip -p
端口(集群中任意端口)
-c
-c
:代表连接的是
redis
集群

测试值的存取
:

1
)从本地连接到集群
redis
使用
7001
端口 加
-c
参数

2
)存入
name
值为
abc
,系统提示此值被存入到了
7002
端口所在的
redis
(槽是
5798

3
)提取
name
的值,可以提取。

4
)退出(
quit

5
)再次以
7001
端口进入 ,不带
-c

6
)查询
name
值,无法获取,因为值在
7002
端口的
redis

7
)我们以
7002
端口进入,获取
name
值发现是可以获取的
,
而以其它端口进入均不能获

4.3.2 SpringDataRedis
连接
Redis
集群

修改品优购工程
pinyougou-common
工程添加
spring
配置文件

[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
applicationContext-redis-cluster.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
[url]http://www.springframework.org/schema/beans/spring-beans.xsd[/url]
[url]http://www.springframework.org/schema/context[/url]
[url]http://www.springframework.org/schema/context/spring-context.xsd[/url]">
<!-- 加载配置属性文件 -->
<context:property-placeholder ignore-unresolvable="true"
location="classpath:properties/redis-cluster-config.properties" />
<bean id="redis-clusterConfiguration"
class="org.springframework.data.redis.connection.redis-clusterConfiguration">
<property name="maxRedirects" value="${redis.maxRedirects}"></property>
<property name="clusterNodes">
<set>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.redis-clusterNode">
<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
</bean>
<bean id="jeidsConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<constructor-arg ref="redis-clusterConfiguration" />
<constructor-arg ref="jedisPoolConfig" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jeidsConnectionFactory" />
</bean>
</beans>


添加属性文件
redis-cluster-config.properties

[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
#cluster configuration
redis.host1=192.168.25.140
redis.port1=7001
redis.host2=192.168.25.140
redis.port2=7002
redis.host3=192.168.25.140
redis.port3=7003
redis.host4=192.168.25.140
redis.port4=7004
redis.host5=192.168.25.140
redis.port5=7005
redis.host6=192.168.25.140
redis.port6=7006
redis.maxRedirects=3
redis.maxIdle=100
redis.maxTotal=600


4.4
模拟集群异常测试

关闭节点命令

[AppleScript]
纯文本查看
复制代码
1
./redis-cli -p 端口 shutdown


1
)测试关闭
7001
7004,
看看会发生什么。

2
)测试关闭
7001
7002
7003
会发生什么。

更多学习资料可关注:gzitcast