我这里搭建的是一个伪分布式集群,六个Redis,三主三从
daemonize yes(设置启动为后台进程)
port 800*(设置目录的端口号【可以按照自己的规则设置,只要不被占用】)
dir /opt/moreredis/redis-cluster*/800*/data(给每个配置文件设置一个存放生成文件数据的目录,不然会丢失数据)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes-8001.conf(集群节点信息文件,这里800x最好和port对应上)
cluster-node-timeout 5000(设置节点超时时间)
# bind 127.0.0.1(去掉bind绑定访问ip信息)
protected-mode no (关闭保护模式)
appendonly yes(使用AOF模式)
requirepass password (设置redis访问密码)
masterauth password (设置集群节点间访问密码,跟上面一致)
cd /opt/moreredis/redis-5.0.2
redis-server ../redis-cluster1/8001/redis.conf
redis-server ../redis-cluster1/8004/redis.conf
redis-server ../redis-cluster2/8002/redis.conf
redis-server ../redis-cluster2/8005/redis.conf
redis-server ../redis-cluster3/8003/redis.conf
redis-server ../redis-cluster3/8006/redis.conf
这里我们看到报了一个错误,这个错误是因为我之前启动过,生成的文件数据没有删除,所有导致已经存在的错误。
root@ubuntu:/opt/moreredis/redis-5.0.2# redis-cli -a password --cluster create --cluster-replicas 1 192.168.1.182:8001 192.168.1.182:8002 192.168.1.182:8003 192.168.1.182:8004 192.168.1.182:8005 192.168.1.182:8006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[ERR] Node 192.168.1.182:8001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
删除文件之前重新启动redis,然后再创建这个集群,会出现下面这个情况,然后我们输入yes(注意:不可以输入y)
注意:这里的ip一定不可以写127.0.0.1或者是localhost,一定要写真实ip。不然会导致你的Jedis连接不上;
启动成功后使用这行命令连接端口号为8001的客户端
redis-cli -c -a wss -h 192.168.1.182 -p 8001
使用cluster info查看集群信息
IP:8002> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:2
cluster_stats_messages_ping_sent:555
cluster_stats_messages_pong_sent:543
cluster_stats_messages_meet_sent:4
cluster_stats_messages_sent:1102
cluster_stats_messages_ping_received:540
cluster_stats_messages_pong_received:559
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:1102
使用cluster nodes查看节点信息,我们可以看到哪些是主节点,哪些是从节点。
192.168.1.182:8002> cluster nodes
34a0eb7b06a7442a53e6f9f8008f6efcd7dff898 192.168.1.182:8001@18001 master - 0 1562636777000 1 connected 0-5460
06f13f552664fa844eb29d71185a5760bdfa8864 192.168.1.182:8005@18005 slave 8becfae925137fa32d3505c272a9a434cab1f991 0 1562636776133 5 connected
f0e3f9cd1fd53d4ae6efe60894b809c9f1753b84 192.168.1.182:8006@18006 slave e2470282f8ec7723084d42f89d36fa555831ab2e 0 1562636777140 6 connected
e2470282f8ec7723084d42f89d36fa555831ab2e 192.168.1.182:8003@18003 master - 0 1562636777542 3 connected 10923-16383
861216e86cd59dc7924004debca676eb02e33bf2 192.168.1.182:8004@18004 slave 34a0eb7b06a7442a53e6f9f8008f6efcd7dff898 0 1562636776000 4 connected
8becfae925137fa32d3505c272a9a434cab1f991 192.168.1.182:8002@18002 myself,master - 0 1562636775000 2 connected 5461-10922
192.168.1.182:8001> set name xiaoming
-> Redirected to slot [5798] located at 192.168.1.182:8002
OK
192.168.1.182:8002> get name
"xiaoming"
使用这个就是测试redis是否可用。
redis中总共设置了有16833个节点,在请求的时候会出现重定向,这里把命令重定向到8002端口。
redis.clients
jedis
2.9.0
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class RedisCluster {
public static void main(String[] args) throws IOException {
Set jedisClusterNode = new HashSet();
jedisClusterNode.add(new HostAndPort("192.168.1.182",8001));
jedisClusterNode.add(new HostAndPort("192.168.1.182",8002));
jedisClusterNode.add(new HostAndPort("192.168.1.182",8003));
jedisClusterNode.add(new HostAndPort("192.168.1.182",8004));
jedisClusterNode.add(new HostAndPort("192.168.1.182",8005));
jedisClusterNode.add(new HostAndPort("192.168.1.182",8006));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(50);
config.setTestOnBorrow(true);
config.setMaxWaitMillis(6000);
JedisCluster jedisCluster = new JedisCluster(jedisClusterNode,6000,5000,10,"wss",config);
System.out.println(jedisCluster.set("article","dog"));
System.out.println(jedisCluster.get("article"));
System.out.println(jedisCluster.get("name"));
jedisCluster.close();
}
}