1.下载redis5.0.2
redis-5.0.2
2.安装
tar -zxvf redis-5.0.2.tar.gz
cd redis-5.0.2
make && make install
3.创建并使用集群配置
创建redis-cluster
目录和相应的配置目录
mkdir redis-cluster
cd reids-cluster
mkdir 7000 70001 7002 7003 7004 7005
redis-cluster
在每个目录中创建一个文件,从7000到7005配置文件的模板,只需使用上面的小例子,但请确保7000
根据目录名称用正确的端口号替换端口号,模版如下:
## 7000-7005端口
port 7000
## 后台启动
daemonize yes
## 如果是在单机模拟集群必须指定bind的IP,这里为192.168.4.115
bind 192.168.4.115
## 开启redis-cluster集群
cluster-enabled yes
## 每个实例还包含存储此节点配置的文件的路径,默认情况下为nodes.conf,自动创建
cluster-config-file nodes_7000.conf
## 超时
cluster-node-timeout 500
## 开启aof
appendonly yes
请注意,按预期工作的最小群集需要包含至少三个主节点。对于您的第一次测试,强烈建议启动具有三个主服务器和三个从服务器的六节点集群。
4.启动redis集群
进入redis根目录
cd src
cp redis-cli /usr/local/bin
cp redis-server /usr/local/bin
redis-server ./redis-cluster/7000/redis.conf
redis-server ./redis-cluster/7001/redis.conf
redis-server ./redis-cluster/7002/redis.conf
redis-server ./redis-cluster/7003/redis.conf
redis-server ./redis-cluster/7004/redis.conf
redis-server ./redis-cluster/7005/redis.conf
查看redis启动的进程
501 88365 1 0 4:52下午 ?? 0:08.52 redis-server 192.168.4.115:7000 [cluster]
501 88367 1 0 4:52下午 ?? 0:08.53 redis-server 192.168.4.115:7001 [cluster]
501 88369 1 0 4:52下午 ?? 0:08.50 redis-server 192.168.4.115:7002 [cluster]
501 88371 1 0 4:52下午 ?? 0:08.46 redis-server 192.168.4.115:7003 [cluster]
501 88373 1 0 4:52下午 ?? 0:08.44 redis-server 192.168.4.115:7004 [cluster]
501 88375 1 0 4:52下午 ?? 0:08.40 redis-server 192.168.4.115:7005 [cluster]
执行redis5新增的集群启动方式
redis-cli --cluster create 192.168.4.115:7000 192.168.4.115:7001 \
192.168.4.115:7002 192.168.4.115:7003 192.168.4.115:7004 192.168.4.115:7005 \
--cluster-replicas 1
这里使用的命令是create,因为我们要创建一个新的集群。
该选项--cluster-replicas 1
意味着我们希望每个创建的主服务器都有一个从服 其他参数是我要用于创建新集群的实例的地址列表。
输出结果:
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.4.115:7003 to 192.168.4.115:7000
Adding replica 192.168.4.115:7004 to 192.168.4.115:7001
Adding replica 192.168.4.115:7005 to 192.168.4.115:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 4d3203ec44f8090ac52cd46b240782c2002e3bac 192.168.4.115:7000
slots:[0-5460] (5461 slots) master
M: 25b0adb22a69de53e6dc261f0bd83f9c94e9ce53 192.168.4.115:7001
slots:[5461-10922] (5462 slots) master
M: c7e12a476431ee892b0c66c62bdef12cde06d2df 192.168.4.115:7002
slots:[10923-16383] (5461 slots) master
S: 23dee0b82d6ef32bfcdfb1eb9f3dc3a9f5beddb9 192.168.4.115:7003
replicates 25b0adb22a69de53e6dc261f0bd83f9c94e9ce53
S: 3fca8f25d28bf0fcf4fd2b1b11d9bc9bd084d6c6 192.168.4.115:7004
replicates c7e12a476431ee892b0c66c62bdef12cde06d2df
S: 9bc42f176e6c4c421c26b55b3e5edc984fed0992 192.168.4.115:7005
replicates 4d3203ec44f8090ac52cd46b240782c2002e3bac
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
>>> Performing Cluster Check (using node 192.168.4.115:7000)
M: 4d3203ec44f8090ac52cd46b240782c2002e3bac 192.168.4.115:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 9bc42f176e6c4c421c26b55b3e5edc984fed0992 192.168.4.115:7005
slots: (0 slots) slave
replicates 4d3203ec44f8090ac52cd46b240782c2002e3bac
M: 25b0adb22a69de53e6dc261f0bd83f9c94e9ce53 192.168.4.115:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: c7e12a476431ee892b0c66c62bdef12cde06d2df 192.168.4.115:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 23dee0b82d6ef32bfcdfb1eb9f3dc3a9f5beddb9 192.168.4.115:7003
slots: (0 slots) slave
replicates 25b0adb22a69de53e6dc261f0bd83f9c94e9ce53
S: 3fca8f25d28bf0fcf4fd2b1b11d9bc9bd084d6c6 192.168.4.115:7004
slots: (0 slots) slave
replicates c7e12a476431ee892b0c66c62bdef12cde06d2df
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
确认redis-cluster
状态,cluster_state:ok
说明成功
redis-cli -h 192.168.4.115 -p 7000
192.168.4.115:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:53115
cluster_stats_messages_pong_sent:53112
cluster_stats_messages_sent:106227
cluster_stats_messages_ping_received:53107
cluster_stats_messages_pong_received:53115
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:106227
还可以使用官方写好的redis-cluster脚本,进入redis根目录,里面有一个脚本(与它所包含的目录同名),它是一个简单的bash脚本。要启动具有3个主服务器和3个从服务器的6节点集群,只需键入以下命令
cd utils/create-cluster
## 启动
create-cluster start
## 创建集群
create-cluster create
您现在可以与群集交互,默认情况下,第一个节点将从端口30001开始,关闭命令为
## 关闭
create-cluster stop
5.整合spring-boot
添加maven
依赖
org.springframework.boot
spring-boot-starter-data-redis
2.0.3.RELEASE
添加redis
配置信息
spring:
redis:
cluster:
nodes: 192.168.4.115:7000,192.168.4.115:7001,192.168.4.115:7002,192.168.4.115:7003,192.168.4.115:7004,192.168.4.115:7005
添加SpringBootTest
测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestDemo {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test() {
redisTemplate.opsForValue().set("redis-cluster-state", "success");
System.out.println(redisTemplate.opsForValue().get("redis-cluster-state"));
}
}
输出结果:
2018-12-03 16:54:41.431 INFO 88393 --- [ main] io.lettuce.core.EpollProvider : Starting without optional epoll library
2018-12-03 16:54:41.433 INFO 88393 --- [ main] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
success