Linux集群项目有启动顺序,linux搭建redis cluster,并使用springboot项目访问

1.环境准备

三台linux环境,并且都安装好redis

本人配置环境分别为:192.168.78.137  192.168.78.138  192.168.78.139

注意redis早起的一些版本需要安装ruby环境,因为要使用ruby脚本(redis-trib.rb)进行创建cluster,本人使用的是5.0.8的版本,使用redis-cli进行创建即可,无需安装ruby环境。

2.搭建redis cluster配置

2.1.前言

因为redis cluster使用不同于哈希的形式分片,集群中有16384个哈希槽,集群中的每个节点负责一部分哈希槽的子集,通常redis节点设置为3个。

又因为单节点存在故障的问题,所以redis cluster又使用主从模式,为每个哈希槽创建一个主节点和多个备份节点,本案例只为每个主节点设置一个备份节点

2.2.设置配置

在redis项目目录下创建redis_cluster目录并在其中创建对应端口的目录

linux机器(137)操作如下:

cd redis-5.0.8

mkdir redis_cluster

cd redis_cluster

mkdir 7000 7001   (其他机器分别创建7002,7003,7004,7005.当然也可以创建和139机器一样的端口号,本人为了通过端口区分出机器才使用递增多口号来搭建配置)

cp redis.conf redis_cluster/7000 (将reids目录下的redis.conf一次复制到创建的每个端口目录下)

vi redis_cluster/7000/redis.conf(修改redis配置,需要修改的配置项如下:)

port 7000 (其他配置依次对应目录名称)

bind 192.168.78.137 (bind对应的机器ip)

daemonize yes

cluster-enable yes

cluster-config-file nodes-7000.conf (名称后缀与端口一致)

cluster-node-timeout 15000 (节点超时时间)

pidfile /var/run/redis_7000.pid (名称后缀与端口一致)

到此配置已修改完成。。。

3.启动redis cluster集群

使用修改的配置启动各个节点的redis服务

redis-server redis_cluster/7000/redis.conf

所有节点的redis服务启动成功之后,使用以下命令创建cluster集群

redis-cli --cluster create 192.168.78.139:7000 192.168.78.139:7001 192.168.78.138:7002 192.168.78.138:7003 192.168.78.137:7004 192.168.78.137:7005 --cluster-replicas 1

当出现如下日志表示cluster创建成功:

>>> 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.78.138:7003 to 192.168.78.139:7000

Adding replica 192.168.78.137:7005 to 192.168.78.138:7002

Adding replica 192.168.78.139:7001 to 192.168.78.137:7004

M: 3a6c59ced1d9d9d570d97e8830df65d925b6dfc0 192.168.78.139:7000

slots:[0-5460] (5461 slots) master

S: 77a6bb5cf88dd4018299b92df0e4278249efb9d1 192.168.78.139:7001

replicates ae8d0f2040783bc8e6d6a32f9a2d9c12a8d24587

M: 969d27ca5a93598b250e32b49ca2087b7dc016be 192.168.78.138:7002

slots:[5461-10922] (5462 slots) master

S: 895903d59fd7299709a522e5bbfd83fb2f6149ba 192.168.78.138:7003

replicates 3a6c59ced1d9d9d570d97e8830df65d925b6dfc0

M: ae8d0f2040783bc8e6d6a32f9a2d9c12a8d24587 192.168.78.137:7004

slots:[10923-16383] (5461 slots) master

S: b912b6251ab46c7ec0411eff5c81c3718a79c30d 192.168.78.137:7005

replicates 969d27ca5a93598b250e32b49ca2087b7dc016be

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.78.139:7000)

M: 3a6c59ced1d9d9d570d97e8830df65d925b6dfc0 192.168.78.139:7000

slots:[0-5460] (5461 slots) master

1 additional replica(s)

S: b912b6251ab46c7ec0411eff5c81c3718a79c30d 192.168.78.137:7005

slots: (0 slots) slave

replicates 969d27ca5a93598b250e32b49ca2087b7dc016be

M: 969d27ca5a93598b250e32b49ca2087b7dc016be 192.168.78.138:7002

slots:[5461-10922] (5462 slots) master

1 additional replica(s)

S: 77a6bb5cf88dd4018299b92df0e4278249efb9d1 192.168.78.139:7001

slots: (0 slots) slave

replicates ae8d0f2040783bc8e6d6a32f9a2d9c12a8d24587

M: ae8d0f2040783bc8e6d6a32f9a2d9c12a8d24587 192.168.78.137:7004

slots:[10923-16383] (5461 slots) master

1 additional replica(s)

S: 895903d59fd7299709a522e5bbfd83fb2f6149ba 192.168.78.138:7003

slots: (0 slots) slave

replicates 3a6c59ced1d9d9d570d97e8830df65d925b6dfc0

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

4.验证cluster集群

创建连接任意redis节点的客户端

redis-cli -c -h 192.168.78.137 -p 7000

使用cluster nodes命令查看当前cluster的节点,当返回当前cluster6个节点的信息的时候表示当前cluster运行正常。

5.spring项目访问redis cluster

pom.xml中添加springboot的reidsjar包

74872a8f6b93

application.yml配置文件中添加redis集群配置

74872a8f6b93

项目中直接使用reids模版类操作缓存数据即可

74872a8f6b93

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值