Redis 4.0集群配置

Redis 集群,官方方案需要6个节点,3个主3个从。

  1. 安装依赖软件
安装:ruby
  yum -y install ruby
安装:rubygems-2.7.4.tgz
  https://rubygems.org/rubygems/rubygems-2.7.4.tgz
  tar -xzvf rubygems-2.7.4.tgz
  cd rubygems-2.7.4
  ruby setup.rb
安装:redis-4.0.1.gem
  https://rubygems.org/downloads/redis-4.0.1.gem
  gem install -l ./redis-4.0.1.gem
  1. 安装redis
安装:redis
  http://download.redis.io/releases/redis-4.0.6.tar.gz
  tar -xzvf redis-4.0.6.tar.gz
  cd redis-4.0.6
  make
  1. 集群配置
  cd redis-4.0.6
  mkdir -p clus/6401 clus/6402 clus/6403 clus/6404 clus/6405 clus/6406
  cp redis.conf clus/6401  其它目录个一份配置文件
  vi clus/6401/redis.conf
    daemonize yes
    port 6401
    bind 127.0.0.1
    cluster-enabled yes
    cluster-config-file /opt/redis-4.0.6/clus/6401/nodes-6401.conf
    cluster-node-timeout 5000
    slave-serve-stale-data yes
    dir /opt/redis-4.0.6/clus/6401/
    dbfilename dump.rdb
    appendonly yes
    appendfilename "appendonly.aof"
  其它几个端口的配置文件,参照以上替换6401为6402...6406的端口即可
  1. 创建集群
启动节点:节点需要加入进去,必须先启动
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6401/redis.conf
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6402/redis.conf
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6403/redis.conf
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6404/redis.conf
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6405/redis.conf
  /opt/redis-4.0.6/src/redis-server /opt/redis-4.0.6/clus/6406/redis.conf

创建集群:集群只需创建一次,以后只需启动节点即可。正常情况,执行以下命令后,会提示集群创建的结果,其中包含三个master和三个slave节点。
  /opt/redis-4.0.6/src/redis-trib.rb create --replicas 1 127.0.0.1:6401 127.0.0.1:6402 127.0.0.1:6403 127.0.0.1:6404 127.0.0.1:6405 127.0.0.1:6406
  1. 集群测试
连接其中一台redis,要带 -c 参数,进行设置值,再连接另外任意节点进行查看,测试成功,连接命令:
  /opt/redis-4.0.6/src/redis-cli -c -p 6401
  1. JAVA连接集群
maven配置依赖:
<dependency>
    <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
    <version>2.9.0</version>
 </dependency>
客户端代码:
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(100000);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000 * 100);
config.setTestOnBorrow(true);
// 定义集群信息
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo("127.0.0.1", 6401));
shards.add(new JedisShardInfo("127.0.0.1", 6402));
shards.add(new JedisShardInfo("127.0.0.1", 6403));
shards.add(new JedisShardInfo("127.0.0.1", 6404));
shards.add(new JedisShardInfo("127.0.0.1", 6405));
shards.add(new JedisShardInfo("127.0.0.1", 6406));
 // 定义集群连接接池
ShardedJedisPool shardedJedisPool = new ShardedJedisPool(config,shards);
// 连接池中获取连接
ShardedJedis redis = pool.getResource();
redis.set("name", "finish");
String val = redis.get("name");
redis.close();
System.out.println(val);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,374评论 0 36
  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 4,080评论 2 27
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,026评论 19 139
  • A1、朋友结婚,我因为路途太远去不了现场,于是编了各种借口不想去。最终我跟朋友说,因为我们公司刚好那天要来一波外宾...
    305号阳光阅读 200评论 3 0
  • 审心 让心站在一条水平线的尽头, 找到孤独的边缘; 抛弃所有的悲伤,抛弃所有的空虚, 忘掉不可重演的过去; 让心站...
    贺卿茵阅读 431评论 3 4