1. 下载redis安装源码包,下载链接地址:redis download。
2. 集群中redis集群节点端口号7000~7005,端口号即集群下各实例文件夹,数据存储在 端口号/data 文件夹中。如图1:
3. 复制执行脚本。如图2:
4. 创建一个新的redis实例。如图3:
5. 修改redis.conf。修改配置如下:
port 7000(每个节点的端口号)
daemonize yes
bind 127.0.0.1(绑定当前机器 IP)
dir /home/xiaodongsun/Documents/redis-cluster/7000/data/(数据文件存放位置)
pidfile /var/run/redis_7000.pid(pid 7000和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes7000.conf(7000和port要对应)
cluster-node-timeout 15000
appendonly yes
6. 复制5个实例。如图4:
7. 修改7001~7005的redis.conf配置文件。如图5:
vim redis.conf
#全局替换
:%s/7000/7001/g
检查主要是替换4行:
port 7002
dir /usr/local/redis-cluster/7002/data/
cluster-config-file nodes-7002.conf
pidfile /var/run/redis_7002.pid
8. 分别启动6台redis实例。如图6:
连接其中一台测试:
CLUSTERDOWN Hash slot not served(不是提供散列的列槽)
安装ruby环境(redis集群命令行工具redis-trib.rb命令需要借助ruby环境)
sudo apt-get install ruby
Ubuntu 16.04在搭建Redis Cluster搭建时,使用gem install redis时出现:ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /var/lib/gems/2.3.0 directory.
解决方式(参考官网安装教程):
sudo apt-get update
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL
rbenv install 2.3.1
rbenv global 2.3.1
ruby -v
重新运行:gem install redis
9. 创建集群。如图7:
创建完成后:
10. 客户端连接redis集群测试
集群写入数据报错:
错误原因: 连接参数需要添加 "-c"(集群连接方式)
到此基本上已完成初步的集群搭建更多信息参考官网: http://www.redis.cn/topics/cluster-tutorial.html
Java操作redis cluster:https://github.com/xiaodongsun-github/tutorials/blob/master/springboot-redis-cluster/src/main/java/com/example/SpringbootRedisClusterApplication.java