关于Spring Security从单体应用到分布式的第三部分其实已经写了一半,国庆后就被拉去忙项目的事一直到最近才有空,在项目中遇到一些实际问题,我会写成博文分享上来。
1.问题描述
准备要上线的是公司的微服务系统,客户提供了我们微服务包启动的一切,但是从启动服务就开始,服务因为redis而不能正常启动,从现场了解到客户并不是使用单Redis而是使用了Redis Cluster,为了解决服务包不能正常启动,只能撸起袖子先自己搭个环境。
Redis Cluster主要由2部分组成1.单例redis,2.集群串联
2.搭建redis
集群实际就是把多个redis串连在一起,所以为了测试的话实际上是可以吧redis安装在一台通过多个不同的端口进行模拟
下载一个redis
[root@localhost /]# cd home
[root@localhost home]# wget http://download.redis.io/releases/redis-3.2.4.tar.gz
[root@localhost home]# tar -zxvf redis-3.2.4.tar.gz
[root@localhost home]# cd redis-3.2.4/
[root@localhost redis-3.2.4]# make && make install
复制并修改不同端口的配置文件
[root@localhost redis-3.2.4]# cp redis.conf /home/redisconfs/node1
关于redis配置的可以参考https://www.cnblogs.com/qq78292959/archive/2013/09/21/3331032.html,下面是每一个端口配置需要修改的文件
daemonize yes
pidfile /var/run/redis_6370.pid
port 6370
cluster-enabled yes
cluster-config-file nodes_6370.conf
cluster-node-timeout 5000
logfile "/home/redisconfs/node1/logs/redis.log"
bind 192.168.1.116
dbfilename dump_6370.rdb
requirepass redis
masterauth redis
启动redis
[root@localhost src]# ./redis-server ../../redisconfs/node1/redis.conf
[root@localhost src]# ./redis-server ../../redisconfs/node2/redis.conf
[root@localhost src]# ./redis-server ../../redisconfs/node3/redis.conf
[root@localhost src]# ./redis-server ../../redisconfs/node4/redis.conf
[root@localhost src]# ./redis-server ../../redisconfs/node5/redis.conf
[root@localhost src]# ./redis-server ../../redisconfs/node6/redis.conf
[root@localhost src]# ps -ef | grep redis
root 50244 1 0 15:00 ? 00:00:09 ./redis-server 192.168.1.116:6370 [cluster]
root 50258 1 0 15:01 ? 00:00:09 ./redis-server 192.168.1.116:6371 [cluster]
root 50263 1 0 15:01 ? 00:00:09 ./redis-server 192.168.1.116:6372 [cluster]
root 50267 1 0 15:01 ? 00:00:09 ./redis-server 192.168.1.116:6373 [cluster]
root 58292 1 0 19:55 ? 00:00:00 ./redis-server 192.168.1.116:6374 [cluster]
root 58304 1 0 19:55 ? 00:00:00 ./redis-server 192.168.1.116:6375 [cluster]
root 58308 50922 0 19:56 pts/1 00:00:00 grep --color=auto redis
3.搭建redis集群环境
安装ruby相关,因为官方提供的redis-trib.rb是一个ruby程序,所以我们必须安装前置的依赖
[root@localhost /]# yum -y install ruby ruby-devel rubygems rpm-build
安装redis相关
[root@localhost /]# gem install redis
在redis安装中可能会出现2中错误
1.
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
如果是应为ruby版本的关系可以参考https://www.cnblogs.com/carryping/p/7447823.html
2.还有一种情况就是使用gem下载不了redis,可以通过下载redis.gem
gem install -l redis-3.2.1.gem #放置了gem文件的目录进行离线安装
当出现以下的提示就证明了进行串联了
[root@localhost src]# ./redis-trib.rb
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
设置集群启动器的密码
[root@localhost src]# gem environment #查看gem的安装环境
[root@localhost src]# cd /usr/local/rvm/gems/ruby-2.3.3/gems/redis-4.0.3/lib/redis #移动到redis下
[root@localhost redis]# ls
client.rb cluster.rb connection.rb errors.rb pipeline.rb version.rb
cluster connection distributed.rb hash_ring.rb subscribe.rb
[root@localhost redis]# vi client.rb #修改IP和密码
require_relative "errors"
require "socket"
require "cgi"
class Redis
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "192.168.1.116",#IP
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "redispwd",#添加密码
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:reconnect_delay => 0,
:reconnect_delay_max => 0.5,
:inherit_socket => false
建立集群串联
[root@localhost src]# ./redis-trib.rb create --replicas 1 192.168.1.116:6370 192.168.1.116:6371 192.168.1.116:6372 192.168.1.116:6373 192.168.1.116:6374 192.168.1.116:6375
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.116:6370
192.168.1.116:6371
192.168.1.116:6372
Adding replica 192.168.1.116:6373 to 192.168.1.116:6370
Adding replica 192.168.1.116:6374 to 192.168.1.116:6371
Adding replica 192.168.1.116:6375 to 192.168.1.116:6372
M: 0c5662ab54f203155355b61deb0cd546827ad914 192.168.1.116:6370
slots:0-5460 (5461 slots) master
M: f40f44112a404a997d43dce876e4c847c8d7a3b8 192.168.1.116:6371
slots:5461-10922 (5462 slots) master
M: e8d19e006a9282c4049bbdc76787d8d75050e8b0 192.168.1.116:6372
slots:10923-16383 (5461 slots) master
S: 250df805dec2a1ad5a00a8ea68e93e64a87431e6 192.168.1.116:6373
replicates 0c5662ab54f203155355b61deb0cd546827ad914
S: cb4c72e8155c0049e7c91999fa36f008a5981cc6 192.168.1.116:6374
replicates f40f44112a404a997d43dce876e4c847c8d7a3b8
S: 99e3836f977602c2de23c1b036ca6e4ab067848b 192.168.1.116:6375
replicates e8d19e006a9282c4049bbdc76787d8d75050e8b0
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.1.116:6370)
M: 0c5662ab54f203155355b61deb0cd546827ad914 192.168.1.116:6370
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: cb4c72e8155c0049e7c91999fa36f008a5981cc6 192.168.1.116:6374
slots: (0 slots) slave
replicates f40f44112a404a997d43dce876e4c847c8d7a3b8
S: 250df805dec2a1ad5a00a8ea68e93e64a87431e6 192.168.1.116:6373
slots: (0 slots) slave
replicates 0c5662ab54f203155355b61deb0cd546827ad914
S: 99e3836f977602c2de23c1b036ca6e4ab067848b 192.168.1.116:6375
slots: (0 slots) slave
replicates e8d19e006a9282c4049bbdc76787d8d75050e8b0
M: f40f44112a404a997d43dce876e4c847c8d7a3b8 192.168.1.116:6371
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: e8d19e006a9282c4049bbdc76787d8d75050e8b0 192.168.1.116:6372
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
测试集群情况./redis-trib.rb check 192.168.1.116:6370
[root@localhost src]# ./redis-trib.rb check 192.168.1.116:6370
>>> Performing Cluster Check (using node 192.168.1.116:6370)
M: 0c5662ab54f203155355b61deb0cd546827ad914 192.168.1.116:6370
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: cb4c72e8155c0049e7c91999fa36f008a5981cc6 192.168.1.116:6374
slots: (0 slots) slave
replicates f40f44112a404a997d43dce876e4c847c8d7a3b8
S: 250df805dec2a1ad5a00a8ea68e93e64a87431e6 192.168.1.116:6373
slots: (0 slots) slave
replicates 0c5662ab54f203155355b61deb0cd546827ad914
S: 99e3836f977602c2de23c1b036ca6e4ab067848b 192.168.1.116:6375
slots: (0 slots) slave
replicates e8d19e006a9282c4049bbdc76787d8d75050e8b0
M: f40f44112a404a997d43dce876e4c847c8d7a3b8 192.168.1.116:6371
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: e8d19e006a9282c4049bbdc76787d8d75050e8b0 192.168.1.116:6372
slots:10923-16383 (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
总结
其实redis的集群还是比较容易安装,后面我会带来实战中遇到的问题分析,还有带来Spring Security从单体应用到分布式的第三部分。