redis3.2.8 集群安装

# tar -zxvf redis-3.2.8.tar.gz
# cd redis-3.2.8
#make
#cd src
# make install
# mkdir /opt/cluster/redis_bin
# cp redis* /opt/cluster/redis_bin/
# ls /opt/cluster/redis_bin/
redis-check-aof redis-check-aof.o redis-check-rdb.c redis-cli redis-server
redis-benchmark redis-check-rdb redis-sentinel redis-trib.rb
# cd redis_bin/
# ls
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb
# chmod +x redis*
# ll
总用量 34032
-rwxr-xr-x 1 root root 5574263 9月 12 16:18 redis-benchmark
-rwxr-xr-x 1 root root 22225 9月 12 16:18 redis-check-aof
-rwxr-xr-x 1 root root 7823906 9月 12 16:18 redis-check-rdb
-rwxr-xr-x 1 root root 5703131 9月 12 16:18 redis-cli
-rwxr-xr-x 1 root root 7823906 9月 12 16:18 redis-sentinel
-rwxr-xr-x 1 root root 7823906 9月 12 16:18 redis-server
-rwxr-xr-x 1 root root 60852 9月 12 16:18 redis-trib.rb
# cd ..
# ls
7000 7001 7002 7003 7004 7005 redis-3.2.8 redis-3.2.8.tar.gz redis_bin
# mv 7005 redis_bin/
# ll
总用量 1520
drwxrwxr-x 6 root root 4096 2月 12 2017 redis-3.2.8
-rw-r--r-- 1 root root 1547237 9月 12 15:41 redis-3.2.8.tar.gz
drwxr-xr-x 8 root root 4096 9月 12 16:24 redis_bin
#
#
# cd redis_bin
#make 7000
#cp /opt/cluster/redis-3.2.8/redis.conf /opt/cluster/redis_bin/7000/redis.conf
修改redis.conf 中的内容如下:
daemonize yes //redis后台运行
pidfile /var/run/redis_7000.pid //pidfile文件对应7000,7001,7002,7003
port 7000 //端口7000,7002,7003
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7000.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout 5000 //请求超时 设置5秒够了
bind 192.168.6.24 127.0.0.1 //绑定id (注意本机id一定是第一个)
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
logfile "/var/log/redis/7000.log"

7001 7002 7003 7004 7005 操作与7000相同
# ls
7000 7001 7002 7003 7004 7005 redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb
#
redis-sentinel redis-server
# ./redis-server 7000/redis.conf
# ./redis-server 7001/redis.conf
# ./redis-server 7002/redis.conf
# ./redis-server 7003/redis.conf
# ./redis-server 7004/redis.conf
# ./redis-server 7005/redis.conf
#
# ps -ef|grep redis
root 23389 1 0 16:24 ? 00:00:00 ./redis-server 192.168.6.24:7000 [cluster]
root 23393 1 0 16:24 ? 00:00:00 ./redis-server 192.168.6.24:7001 [cluster]
root 23424 1 0 16:25 ? 00:00:00 ./redis-server 192.168.6.24:7002 [cluster]
root 23428 1 0 16:25 ? 00:00:00 ./redis-server 192.168.6.24:7003 [cluster]
root 23432 1 0 16:25 ? 00:00:00 ./redis-server 192.168.6.24:7004 [cluster]
root 23436 1 0 16:25 ? 00:00:00 ./redis-server 192.168.6.24:7005 [cluster]
root 23440 16719 0 16:25 pts/10 00:00:00 grep redis
# netstat -tnlp | grep redis

# yum -y install ruby ruby-devel rubygems rpm-build
# gem install redis
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
# rvm list known
#
#
# sudo yum install curl

# curl -L get.rvm.io | bash -s stable
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24090 100 24090 0 0 10411 0 0:00:02 0:00:02 --:--:-- 185k
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 无法检查签名:No public key
Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found. Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).

GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.3.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

or if it fails:

command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -

the key can be compared with:

https://rvm.io/mpapis.asc
https://keybase.io/mpapis

NOTE: GPG version 2.1.17 have a bug which cause failures during fetching keys from remote server. Please downgrade or upgrade to newer version (if available) or use the second method described above.

# command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import
gpg: 密钥 D39DC0E3:公钥“Michal Papis (RVM signing) ”已导入
gpg: 合计被处理的数量:1
gpg: 已导入:1 (RSA: 1)
gpg: 没有找到任何绝对信任的密钥
# curl -L get.rvm.io | bash -s stable
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 24090 100 24090 0 0 11593 0 0:00:02 0:00:02 --:--:-- 11593
Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc
gpg: 于 2017年09月11日 星期一 04时59分21秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17
gpg: 完好的签名,来自于“Michal Papis (RVM signing)
gpg: 亦即“Michal Papis
gpg: 亦即“[jpeg image of size 5015]”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg: 没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 409B 6B17 96C2 7546 2A17 0311 3804 BB82 D39D C0E3
子钥指纹: 62C9 E5F4 DA30 0D94 AC36 166B E206 C29F BF04 FF17
GPG verified '/usr/local/rvm/archives/rvm-1.29.3.tgz'
Creating group 'rvm'

Installing RVM to /usr/local/rvm/
source /etc/profile
Installation of RVM in /usr/local/rvm/ is almost complete:

* First you need to add all users that will be using rvm to 'rvm' group,
and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

* To start using RVM you need to run `source /etc/profile.d/rvm.sh`
in all your open shell windows, in rare cases you need to reopen all shell windows.
#


# source /usr/local/rvm/scripts/rvm
查看ruby 版本
# rvm list known
# MRI Rubies

查看当前版本
#ruby -v
插入 ruby2.3.3
# rvm install 2.3.3
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/6/x86_64/ruby-2.3.3.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: libffi-devel, libyaml-devel.......
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.3.3, this may take a while depending on your cpu(s)...
ruby-2.3.3 - #downloading ruby-2.3.3, this may take a while depending on your connection...

curl: (35) SSL connect error
There was an error(35).
Checking fallback: https://ftp.ruby-lang.org/pub/ruby/2.3/ruby-2.3.3.tar.bz2
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
This may have known and unaccounted for security vulnerabilities.
Please consider upgrading to ruby-2.4.1 which will have all of the latest security patches.
Ruby was built without documentation, to build it run: rvm docs generate-ri

使用ruby 2.3.3
# rvm use 2.3.3 或者 rvm use 2.3.3 --default
Using /usr/local/rvm/gems/ruby-2.3.3

# rvm remove 1.8.7
查看当前版本
# ruby --version
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-linux]
#
执行集群命令
# gem install redis
Fetching: redis-4.0.0.gem (100%)
Successfully installed redis-4.0.0
Parsing documentation for redis-4.0.0
Installing ri documentation for redis-4.0.0
Done installing documentation for redis after 2 seconds
1 gem installed
# cd ../redis-3.2.8/src
#
建立集群
 解释下, --replicas  1  表示 自动为每一个master节点分配一个slave节点    上面有6个节点,程序会按照一定规则生成 3个master(主)3个slave(从)

# ./redis-trib.rb create --replicas 1 192.168.6.24:7000 192.168.6.24:7001 192.168.6.24:7002 192.168.6.24:7003 192.168.6.24:7004 192.168.6.24:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.6.24:7000
192.168.6.24:7001
192.168.6.24:7002
Adding replica 192.168.6.24:7003 to 192.168.6.24:7000
Adding replica 192.168.6.24:7004 to 192.168.6.24:7001
Adding replica 192.168.6.24:7005 to 192.168.6.24:7002
M: 82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb 192.168.6.24:7000
slots:0-5460 (5461 slots) master
M: 4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3 192.168.6.24:7001
slots:5461-10922 (5462 slots) master
M: 1f9bea3df531b159b294e32ca9d75e9f8ee34d0a 192.168.6.24:7002
slots:10923-16383 (5461 slots) master
S: 96f4f6da5b172544fd90e18990f64868bd18a14c 192.168.6.24:7003
replicates 82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb
S: dfd4d0e5dc242533af6cb7699e7052e11b10d619 192.168.6.24:7004
replicates 4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3
S: e83888d36f4b8c56e150015be38d7dd7a0ddcc39 192.168.6.24:7005
replicates 1f9bea3df531b159b294e32ca9d75e9f8ee34d0a
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.6.24:7000)
M: 82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb 192.168.6.24:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: e83888d36f4b8c56e150015be38d7dd7a0ddcc39 127.0.0.1:7005
slots: (0 slots) slave
replicates 1f9bea3df531b159b294e32ca9d75e9f8ee34d0a
S: 96f4f6da5b172544fd90e18990f64868bd18a14c 127.0.0.1:7003
slots: (0 slots) slave
replicates 82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb
M: 1f9bea3df531b159b294e32ca9d75e9f8ee34d0a 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: dfd4d0e5dc242533af6cb7699e7052e11b10d619 127.0.0.1:7004
slots: (0 slots) slave
replicates 4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

安装完毕.

测试redis集群

# ls
7000 7003 appendonly.aof nodes-7001.conf nodes-7004.conf redis-check-aof redis-sentinel
7001 7004 dump.rdb nodes-7002.conf nodes-7005.conf redis-check-rdb redis-server
7002 7005 nodes-7000.conf nodes-7003.conf redis-benchmark redis-cli redis-trib.rb
#
#
#
#
# ./redis-cli -c -p 7000
127.0.0.1:7000> set test1 bocehellowolrd
OK
127.0.0.1:7000> get test1
"bocehellowolrd"
127.0.0.1:7000> cluster nodes
e83888d36f4b8c56e150015be38d7dd7a0ddcc39 192.168.6.24:7005 slave 1f9bea3df531b159b294e32ca9d75e9f8ee34d0a 0 1505283170587 6 connected
82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb 192.168.6.24:7000 myself,master - 0 0 1 connected 0-5460
dfd4d0e5dc242533af6cb7699e7052e11b10d619 192.168.6.24:7004 slave 4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3 0 1505283171088 5 connected
1f9bea3df531b159b294e32ca9d75e9f8ee34d0a 192.168.6.24:7002 master - 0 1505283171589 3 connected 10923-16383
96f4f6da5b172544fd90e18990f64868bd18a14c 192.168.6.24:7003 slave 82eeb3e0350bbdc3639f12df48a6fe2ed70dd9fb 0 1505283172592 4 connected
4f8a24da4b6cf5bf5aeff6fea3cee97f1d243cc3 192.168.6.24:7001 master - 0 1505283169584 2 connected 5461-10922
127.0.0.1:7000>
127.0.0.1:7000> quit
# ./redis-cli -c -p 7001
127.0.0.1:7001> get test1
-> Redirected to slot [4768] located at 192.168.6.24:7000
"bocehellowolrd"
192.168.6.24:7000>


Java测试代码:


package com.alibaba.dubbo.demo.consumer;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

public class JedisClusterTest {


JedisCluster jedisCluster = null;

private String nameKey="test1";

/**
* 因为是测试,这里没有写单例
*/

public void before(){
Set nodes = new HashSet();
nodes.add(new HostAndPort("192.168.6.24",7000));
nodes.add(new HostAndPort("192.168.6.24",7001));
nodes.add(new HostAndPort("192.168.6.24",7002));
nodes.add(new HostAndPort("192.168.6.24",7003));
nodes.add(new HostAndPort("192.168.6.24",7004));
nodes.add(new HostAndPort("192.168.6.24",7005));
//注意:这里超时时间不要太短,他会有超时重试机制。而且其他像httpclient、dubbo等RPC框架也要注意这点
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxIdle(200);
config.setMaxTotal(300);
config.setTestOnReturn(true);


jedisCluster = new JedisCluster(nodes, 10000, 1000, 1,
config);

System.out.println(jedisCluster+"===");
System.out.println(jedisCluster.set("test1", "张三"));
System.out.println(jedisCluster.get("test1"));
}

/**
* 简单字符串读写
*/

public void setStringData(){

// System.out.println(jedisCluster.set("test1", "张三"));
System.out.println(jedisCluster.get("test1"));
}

/**
* setnx : 如果key存在,返回0,如果不存在,则设置成功。
* setnx的意思是set if not exist.
*/

public void setnxTest(){
// System.out.println(jedisCluster.setnx(nameKey, "张三"));//key不存在,返回值为1
System.out.println(jedisCluster.get(nameKey));

// System.out.println(jedisCluster.setnx(nameKey, "张三"));//已经存在,返回值为0
System.out.println(jedisCluster.get(nameKey));
}





public static void main(String[] args) {

JedisClusterTest test = new JedisClusterTest();
test.before();
test.setStringData();

test.setnxTest();
}


}


redis cluster配置好,并运行一段时间后,我们想添加节点,或者删除节点
可以参照:
http://blog.51yip.com/nosql/1726.html

你可能感兴趣的:(redis)