java使用Redis8--3.0集群

Redis集群至少需要3个主节点

# cd /usr/redis

创建一个目录

# mkdir cluster

# cd cluster

1、复制一个配置文件

# cp ../redis.conf 9001.conf

修改配置文件

# vim 9001.conf

port 9001

appendonly yes

cluster-enabled yes

cluster-config-file nodes-9001.conf

cluster-node-timeout 5000

2、复制两份配置文件,并修改

# cp 9001.conf 9002.conf

# cp 9001.conf 9003.conf

3、启动3个redis实例

# redis-server 9001.conf &

# redis-server 9002.conf &

# redis-server 9003.conf &

4、启用集群(0 表示不启用副本, 如果设置成数值,例如1,表示每个主机启用一个从机,集群至少需要6个节点)

redis-trib.rb create --replicas 0 192.168.77.136:9001 192.168.77.136:9002 192.168.77.136:9003

4.1 执行上面的命令的时候会报错,因为是执行的ruby的脚本,需要ruby的环境

错误内容:/usr/bin/env: ruby: No such file or directory

所以需要安装ruby的环境,这里推荐使用yum install ruby安装

# yum install ruby

4.2 然后再执行创建集群命令,还会报错,提示缺少rubygems组件,使用yum安装

 错误内容:

./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)

from ./redis-trib.rb:24

# yum install rubygems

4.3 再次执行集群创建命令,还会报错,提示不能加载redis,是因为缺少redis和ruby的接口,使用gem 安装

错误内容:

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)

from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'

from ./redis-trib.rb:25

# yum install redis

因为源的原因,会安装失败,需要更改源信息

# gem sources --remove https://rubygems.org/

# gem source -a https://ruby.taobao.org/

# gem source -l 查看列表下是不是只有 https://ruby.taobao.org/ 这一个源,如果不是重复执行下。

源切换成功后,重新执行yum install redis

# yum install redis

安装完成后,再次执行集群命令,正常运行

>>> Creating cluster
Connecting to node 192.168.77.136:9001: OK
Connecting to node 192.168.77.136:9002: OK
Connecting to node 192.168.77.136:9003: OK
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
192.168.77.136:9001
192.168.77.136:9002
192.168.77.136:9003
M: b32e955c04b37d4170860edf8ab1da0275811fe3 192.168.77.136:9001
   slots:0-5460 (5461 slots) master
M: b4d0df0bec6740cf5680d14637aaa1f6f6283adb 192.168.77.136:9002
   slots:5461-10922 (5462 slots) master
M: 5a834200f28caad158ef1fd5fe4ff4ea4b00622b 192.168.77.136:9003
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept): 

输入yes

>>> Nodes configuration updated
>>> Assign a different config epoch to each node
3605:M 08 Aug 20:24:53.112 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
3608:M 08 Aug 20:24:53.112 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
3612:M 08 Aug 20:24:53.113 # configEpoch set to 3 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster
3605:M 08 Aug 20:24:53.183 # IP address for this node updated to 192.168.77.136
3608:M 08 Aug 20:24:53.284 # IP address for this node updated to 192.168.77.136
3612:M 08 Aug 20:24:53.285 # IP address for this node updated to 192.168.77.136
Waiting for the cluster to join..
>>> Performing Cluster Check (using node 192.168.77.136:9001)
M: b32e955c04b37d4170860edf8ab1da0275811fe3 192.168.77.136:9001
   slots:0-5460 (5461 slots) master
M: b4d0df0bec6740cf5680d14637aaa1f6f6283adb 192.168.77.136:9002
   slots:5461-10922 (5462 slots) master
M: 5a834200f28caad158ef1fd5fe4ff4ea4b00622b 192.168.77.136:9003
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@Slave1 cluster]# 3605:M 08 Aug 20:24:58.025 # Cluster state changed: ok
3612:M 08 Aug 20:24:58.125 # Cluster state changed: ok
3608:M 08 Aug 20:24:58.125 # Cluster state changed: ok

5、登陆集群

# redis-cli -c -p 9001

5.1 查看集群运行情况 cluster info

127.0.0.1:9001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_sent:6275
cluster_stats_messages_received:6275

5.2 查看集群节点情况 cluster nodes

127.0.0.1:9001> cluster nodes
b32e955c04b37d4170860edf8ab1da0275811fe3 192.168.77.136:9001 myself,master - 0 0 1 connected 0-5460
5a834200f28caad158ef1fd5fe4ff4ea4b00622b 192.168.77.136:9003 master - 0 1439094158089 3 connected 10923-16383
b4d0df0bec6740cf5680d14637aaa1f6f6283adb 192.168.77.136:9002 master - 0 1439094157080 2 connected 5461-10922

5.3 简单验证

127.0.0.1:9001> set a abc
-> Redirected to slot [15495] located at 192.168.77.136:9003
OK
192.168.77.136:9003> get a
"abc"

6 java程序连接集群

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

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

public class TestRedisCluster {

    public static void main(String[] args) {
        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("192.168.77.136", 9001));
     // 使用redis集群中的任一节点即可 JedisCluster jedis
= new JedisCluster(jedisClusterNodes); // 开始前,先移除所有的内容 jedis.del("java framework"); System.out.println(jedis.lrange("java framework", 0, -1)); // 先向key java framework中存放三条数据 jedis.lpush("java framework", "spring"); jedis.lpush("java framework", "struts"); jedis.lpush("java framework", "hibernate"); // 再取出所有数据jedis.lrange是按范围取出, // 第一个是key,第二个是起始位置,第三个是结束位置,jedis.llen获取长度 -1表示取得所有 System.out.println(jedis.lrange("java framework", 0, -1)); jedis.del("java framework"); jedis.rpush("java framework", "spring"); jedis.rpush("java framework", "struts"); jedis.rpush("java framework", "hibernate"); System.out.println(jedis.lrange("java framework", 0, -1)); jedis.close(); } }

 

 

 

 

 

 

127.0.0.1:9001> cluster infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:3cluster_size:3cluster_current_epoch:3cluster_my_epoch:1cluster_stats_messages_sent:6275cluster_stats_messages_received:6275

posted @ 2015-08-09 12:31  一粒沙的世界  阅读(1611)  评论(0编辑  收藏  举报