redis集群配置(密码认证)

一、部署环境

单节点多实例

IP端口
10.0.0.707000
10.0.0.707001
10.0.0.707002
10.0.0.707003
10.0.0.707004
10.0.0.707005

二、安装ruby环境

步骤一:安装ruby环境,创建集群时的脚本需要ruby环境。

[root@redis-cluster01 ~]# yum install ruby rubygems -y

步骤二:安装国内gem源

[root@redis-cluster01 ~]# gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
https://gems.ruby-china.com/ added to sources
https://rubygems.org/ removed from sources
[root@redis-cluster01 ~]# gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/

步骤三:gem安装redis

[root@redis-cluster01 ~]# gem install redis
Fetching: redis-4.1.0.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
报错:提示版本过低
解决方法:手动下载
[root@redis-cluster01 ~]# curl -O -L https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.7.tar.gz
[root@redis-cluster01 ~]# tar zxf ruby-2.2.7.tar.gz
[root@redis-cluster01 ~]# cd ruby-2.2.7
[root@redis-cluster01 ruby-2.2.7]# ./configure --prefix=/usr/local/ruby-2.2.7
[root@redis-cluster01 ruby-2.2.7]# make && make install
[root@redis-cluster01 ruby-2.2.7]# cp /usr/local/ruby-2.2.7/bin/ruby /usr/bin/ruby
[root@redis-cluster01 ruby-2.2.7]# ruby --version
ruby 2.2.7p470 (2017-03-28 revision 58194) [x86_64-linux]

[root@redis-cluster01 ruby-2.2.7]# gem install redis
ERROR: Loading command: install (LoadError)
cannot load such file – zlib
ERROR: While executing gem … (NoMethodError)
undefined method `invoke_with_build_args’ for nil:NilClass
报错:缺少zlib依赖
解决:安装依赖
[root@redis-cluster01 zlib]# cd ext/zlib
[root@redis-cluster01 zlib]# ruby ./extconf.rb
checking for deflateReset() in -lz… yes
checking for zlib.h… yes
checking for crc32_combine() in zlib.h… yes
checking for adler32_combine() in zlib.h… yes
checking for z_crc_t in zlib.h… yes
creating Makefile
[root@redis-cluster01 zlib]# make
compiling zlib.c
linking shared-object zlib.so
[root@redis-cluster01 zlib]# make install
/usr/bin/install -c -m 0755 zlib.so /usr/local/ruby-2.2.7/lib/ruby/site_ruby/2.2.0/x86_64-linux

[root@redis-cluster01 zlib]# gem install redis
ERROR: While executing gem … (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
报错:缺少openssl依赖
解决:安装依赖
yum install openssl-devel
[root@redis-cluster01 zlib]# cd …/openssl/
[root@redis-cluster01 openssl]# ruby ./extconf.rb
#修改Makefile中的KaTeX parse error: Expected 'EOF', got '#' at position 50: …ster01 openssl]#̲ sed -i 's#(top_srcdir)/include/ruby.h#$(top_srcdir)…/…#g’ Makefile
[root@redis-cluster01 openssl]# make && make install

[root@redis-cluster01 openssl]# gem install redis
Fetching: redis-4.1.0.gem (100%)
Successfully installed redis-4.1.0
Parsing documentation for redis-4.1.0
Installing ri documentation for redis-4.1.0
Done installing documentation for redis after 1 seconds
1 gem installed

三、部署redis集群环境

步骤一:配置单节点多实例环境

[root@redis-cluster01 ~]# mkdir /application/redis-cluster
[root@redis-cluster01 ~]# mkdir /application/redis-cluster/{7000,7001,7002,7003,7004,7005}
[root@redis-cluster01 ~]# for num in seq 7000 7005;do cp /application/redis/{redis.conf,redis-cli,redis-server} /application/redis-cluster/${num}/;done
[root@redis-cluster01 ~]# cat redis-cluster.sh
#!/bin/bash
Redis_Dir=/application/redis-cluster/
Redis_Config=redis.conf
cd ${Redis_Dir}
for num in seq 7000 7005
do
sed -i “s/port 6379/port $num/g” n u m / {num}/ num/{Redis_Config}
sed -i “s#/var/run/redis_6379.pid#/var/run/redis_${num}.pid#g” n u m / {num}/ num/{Redis_Config}
sed -i “s/appendonly no/appendonly yes/g” n u m / {num}/ num/{Redis_Config}
echo “cluster-enabled yes” >> n u m / {num}/ num/{Redis_Config}
echo "cluster-config-file n u m n o d e s . c o n f " > > {num}_nodes.conf" >> numnodes.conf">>{num}/ R e d i s C o n f i g e c h o " c l u s t e r − n o d e − t i m e o u t 5000 " > > {Redis_Config} echo "cluster-node-timeout 5000" >> RedisConfigecho"clusternodetimeout5000">>{num}/${Redis_Config}
done
[root@redis-cluster01 ~]# bash redis-cluster.sh

步骤二:启动所有实例

[root@redis-cluster01 ~]# cat rcluster_start.sh
#!/bin/bash
Redis_Dir=/application/redis-cluster/
Redis_Config=redis.conf
cd ${Redis_Dir}
for num in seq 7000 7005
do
${num}/redis-server ${num}/redis.conf
done
[root@redis-cluster01 ~]# bash rcluster_start.sh
31495:C 22 Apr 20:37:52.783 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
31495:C 22 Apr 20:37:52.784 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=31495, just started
31495:C 22 Apr 20:37:52.784 # Configura

.
.

步骤三:查看端口

[root@redis-cluster01 ~]# ss -lntup|grep 700*
tcp LISTEN 0 128 10.0.0.70:17000 : users:((“redis-server”,pid=31496,fd=9))
tcp LISTEN 0 128 10.0.0.70:17001 : users:((“redis-server”,pid=31498,fd=9))
tcp LISTEN 0 128 10.0.0.70:17002 : users:((“redis-server”,pid=31500,fd=9))
tcp LISTEN 0 128 10.0.0.70:17003 : users:((“redis-server”,pid=31508,fd=9))
tcp LISTEN 0 128 10.0.0.70:17004 : users:((“redis-server”,pid=31513,fd=9))
tcp LISTEN 0 128 10.0.0.70:17005 : users:((“redis-server”,pid=31518,fd=9))
tcp LISTEN 0 128 10.0.0.70:7000 : users:((“redis-server”,pid=31496,fd=6))
tcp LISTEN 0 128 10.0.0.70:7001 : users:((“redis-server”,pid=31498,fd=6))
tcp LISTEN 0 128 10.0.0.70:7002 : users:((“redis-server”,pid=31500,fd=6))
tcp LISTEN 0 128 10.0.0.70:7003 : users:((“redis-server”,pid=31508,fd=6))
tcp LISTEN 0 128 10.0.0.70:7004 : users:((“redis-server”,pid=31513,fd=6))
tcp LISTEN 0 128 10.0.0.70:7005 : users:((“redis-server”,pid=31518,fd=6))

步骤四:拷贝redis-trib.rb脚本,部署集群

[root@redis-cluster01 ~]# cp redis-4.0.9/src/redis-trib.rb /application/redis-cluster/
[root@redis-cluster01 ~]# ruby /application/redis-cluster/redis-trib.rb create --replicas 1 10.0.0.70:7000 10.0.0.70:7001 10.0.0.70:7002 10.0.0.70:7003 10.0.0.70:7004 10.0.0.70:7005
Creating cluster
[ERR] Sorry, can’t connect to node 10.0.0.70:7000
解决方法:在client.rb中添加密码
[root@redis-cluster01 ~]# find / -name client.rb
/root/ruby-2.2.7/lib/xmlrpc/client.rb
/usr/share/ruby/xmlrpc/client.rb
/usr/local/ruby-2.2.7/lib/ruby/2.2.0/xmlrpc/client.rb
/usr/local/ruby-2.2.7/lib/ruby/gems/2.2.0/gems/redis-4.1.0/lib/redis/client.rb

[root@redis-cluster01 ~]# grep “:password =>” /usr/local/ruby-2.2.7/lib/ruby/gems/2.2.0/gems/redis-4.1.0/lib/redis/client.rb
:password => nil,
[root@redis-cluster01 ~]# grep “:password =>” /usr/local/ruby-2.2.7/lib/ruby/gems/2.2.0/gems/redis-4.1.0/lib/redis/client.rb
:password => “000000”,

[root@redis-cluster01 ~]# ruby /application/redis-cluster/redis-trib.rb create --replicas 1 10.0.0.70:7000 10.0.0.70:7001 10.0.0.70:7002 10.0.0.70:7003 10.0.0.70:7004 10.0.0.70:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes…
Using 3 masters:
10.0.0.70:7000
10.0.0.70:7001
10.0.0.70:7002
Adding replica 10.0.0.70:7004 to 10.0.0.70:7000
Adding replica 10.0.0.70:7005 to 10.0.0.70:7001
Adding replica 10.0.0.70:7003 to 10.0.0.70:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0c81f4cc063b9d93b871ef1101dbd5cf7ab6b62d 10.0.0.70:7000
slots:0-5460 (5461 slots) master
M: 3686833312e5d8579e6769ea102a722c218d6e80 10.0.0.70:7001
slots:5461-10922 (5462 slots) master
M: e351397ed6db615a85bdf118b1b2272ac04d084d 10.0.0.70:7002
slots:10923-16383 (5461 slots) master
S: fb3449137208ac212bc0e61403523b076a671efa 10.0.0.70:7003
replicates 3686833312e5d8579e6769ea102a722c218d6e80
S: 0a18ed047615d869237836d749c5cfa45dd003c7 10.0.0.70:7004
replicates e351397ed6db615a85bdf118b1b2272ac04d084d
S: 3df229bcd1369a38c1b686904ae44b1707810257 10.0.0.70:7005
replicates 0c81f4cc063b9d93b871ef1101dbd5cf7ab6b62d
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 10.0.0.70:7000)
M: 0c81f4cc063b9d93b871ef1101dbd5cf7ab6b62d 10.0.0.70:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 3686833312e5d8579e6769ea102a722c218d6e80 10.0.0.70:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: e351397ed6db615a85bdf118b1b2272ac04d084d 10.0.0.70:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 3df229bcd1369a38c1b686904ae44b1707810257 10.0.0.70:7005
slots: (0 slots) slave
replicates 0c81f4cc063b9d93b871ef1101dbd5cf7ab6b62d
S: 0a18ed047615d869237836d749c5cfa45dd003c7 10.0.0.70:7004
slots: (0 slots) slave
replicates e351397ed6db615a85bdf118b1b2272ac04d084d
S: fb3449137208ac212bc0e61403523b076a671efa 10.0.0.70:7003
slots: (0 slots) slave
replicates 3686833312e5d8579e6769ea102a722c218d6e80
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.

步骤五:登录查看

[root@redis-cluster01 ~]# /application/redis/redis-cli -c -h 10.0.0.70 -p 7000 -a 000000
10.0.0.70:7000> info replication
# Replication
role:master
connected_slaves:1
min_slaves_good_slaves:1
slave0:ip=10.0.0.70,port=7005,state=online,offset=28,lag=1 ###7005端口的实例为7000实例的从节点
master_replid:3260ce1209f497c9a3fc79051be604eacc562d9f
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:28

步骤六:创建数据测试

10.0.0.70:7000> KEYS *
(empty list or set)
10.0.0.70:7000> set name liang
-> Redirected to slot [5798] located at 10.0.0.70:7001 ###数据被分配到70001实例上
OK
10.0.0.70:7001> KEYS *###查看的时候自动跳到70001实例。
\1) “name”

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Spring Boot 中使用 Redis 集群,需要在 `application.yml` 或 `application.properties` 文件中配置 Redis 集群的节点信息。下面是一个示例 `application.yml` 文件: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 ``` 在这个示例中,Redis 集群由三个节点组成,它们的 IP 地址和端口号分别为 `192.168.0.1:6379`、`192.168.0.2:6379` 和 `192.168.0.3:6379`。 另外,如果 Redis 集群中启用了密码认证,还需要在配置文件中设置密码: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 password: yourpassword ``` 此外,还可以配置 Redis 连接池的一些参数,例如最大连接数、最大空闲连接数、连接超间等: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 password: yourpassword pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: -1ms ``` 这里的参数含义如下: - `max-active`:连接池中最大的活跃连接数,默认值为 8。 - `max-idle`:连接池中最大的空闲连接数,默认值为 8。 - `min-idle`:连接池中最小的空闲连接数,默认值为 0。 - `max-wait`:从连接池中获取连接的最大等待间,默认值为 -1ms,表示永不超。如果超了,则会抛出 `RedisConnectionFailureException` 异常。 注意:以上配置只适用于使用 Redis 集群的情况,在单机 Redis 中,应该使用 `spring.redis.host` 和 `spring.redis.port` 配置主机地址和端口号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值