java鬼混笔记:redis 7、redis集群配置

1、先安装Redis(我下的是Redis-x64-3.2.100)

2、接着安装 RubyInstallers(我下的是rubyinstaller-2.4.2-2-x64)

安装redis后,在redis目录创建6个文件夹,3主3从(按官网的来),我这创建的分别是

6810(主)、6811(从)
6820(主)、6821(从)
6830(主)、6831(从)


复制原来redis安装的生成的redis.windows-service.conf分别放在6810-6831的目录里
然后编辑修改:
修改下面如下面,分别对应自己的文件夹名,也就是改改端口和文件名


port 6810
appendonly yes
appendfilename "appendonly6810.aof"
dbfilename dump6810.rdb
logfile "server_log_6810.txt"
slave-read-only no
bind 127.0.0.1 192.168.1.103

#下面是集群配置,找到并去掉 # ,让它启动
cluster-enabled yes
# 百度说明:cluster配置文件的名称,当集群信息发生变更时,node将会自动把这些信息持久化到此配置文件中,以便在启动时重新读取它。对于用户而言,通常不应该认为修改此文件内容。文件中会包含:集群中其他nodes信息、以及他们的状态,还有一些持久性的变量等。
cluster-config-file nodes-6810.conf
# 百度说明:node在被认为失效之前,允许不可用的最长时间。如果Master处于“不可达”的时间超过此值,将会被failover。当然,每个node在此时间内不能与“多数派”nodes通讯,它也将终止接收writes请求,切换到read only模式。这个参数很重要,选择合适的值,对集群的有效性、数据一致性有极大影响
cluster-node-timeout 15000
# 百度说明:用于限定slave于master的失联时长的倍数;如果设置为0,slave总是尝试failover,不管master与slave之间的链接断开多久(即只要slave无法与master通讯,都会尝试进行failover;Cluster有选举机制,有可能会被否决);如果此值为正值,则失联的最大时长为:factor * node timeout,当失联时长超过此值后,slave触发failover。比如node timeout为5秒,factor为10,那么当slave与master失联超过50秒时,slave将会尽力尝试failover。此值需要合理设置,也会对集群有效性、数据一致性产生影响
cluster-slave-validity-factor 10
# 百度说明:适用于“migration”架构场景(稍后介绍);master需要保留的slaves的最少个数;如果一个master拥有比此值更多的slaves,那么Cluster将会使用“migration”机制,将多余的slaves迁移到其他master上,这些master持有的slaves的数量低于此值。最终Cluster希望做到,slaves能够按需迁移,在failover期间,确保每个master都有一定量的slaves,以提高集群整体的可用性。(比如A有三个slaves:A1、A2、A3,而B只有一个slave B1,当B失效后,B1提升为master,但是B1没有任何slave,所以存在风险,那么migration机制,可以将A下的某个slave迁移到B1下,成为B1的slave。)
cluster-migration-barrier 1
# 百度说明:“集群是否需要全量覆盖”,如果设置为yes(默认值),当集群中有slots没有被任何node覆盖(通常是由于某个master失效,但是它也没有slave接管时),集群将会终止接收writes请求;如果设置为no,集群中那些服务正常的nodes(slots)仍然可以继续服务。
cluster-require-full-coverage yes

在redis目录位置cmd执行下面的语句安装redis服务

redis-server --service-install 6810/redis.windows-service.conf --service-name Redis6810 --port 6810
redis-server --service-install 6811/redis.windows-service.conf --service-name Redis6811 --port 6811
redis-server --service-install 6820/redis.windows-service.conf --service-name Redis6820 --port 6820
redis-server --service-install 6821/redis.windows-service.conf --service-name Redis6821 --port 6821
redis-server --service-install 6830/redis.windows-service.conf --service-name Redis6830 --port 6830
redis-server --service-install 6831/redis.windows-service.conf --service-name Redis6831 --port 6831

然后可以在  Redis的服务里可以看到这 Redis6810、Redis6811、Redis6820、Redis6821、Redis6830、Redis6831这6个服务,全部启动。

接着 在redis安装目录下cmd执行

gem install redis
这个是下载相关的组件,要连网的
成功后是这个样子的

C:\Redis>gem install redis
Fetching: redis-4.0.1.gem (100%)
Successfully installed redis-4.0.1
Parsing documentation for redis-4.0.1
Installing ri documentation for redis-4.0.1
Done installing documentation for redis after 1 seconds
1 gem installed

下载redis-trib.rb
地址(目前是3.2的):
https://raw.githubusercontent.com/MSOpenTech/redis/3.2/src/redis-trib.rb
用浏览器打开这个网址,直接保存成文件 redis-trib.rb 就行,然后放在redis安装目录下,我的是C:\Redis\

放进去后,在Redis安装目录下cmd执行

注意,按顺序,先主后从,不要用127.0.0.1,要用ip地址,不然别的服务器访问不了(浪费了我不少时间这坑)

redis-trib.rb create --replicas 1 192.168.1.103:6810 192.168.1.103:6820 192.168.1.103:6830 192.168.1.103:6811 192.168.1.103:6821 192.168.1.103:6831

执行后的结果是这样的

C:\Redis>redis-trib.rb create --replicas 1 192.168.1.103:6810 192.168.1.103:6820 127.0.0
.1:6830 192.168.1.103:6811 192.168.1.103:6821 192.168.1.103:6831
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.103:6810
192.168.1.103:6820
192.168.1.103:6830
Adding replica 192.168.1.103:6811 to 192.168.1.103:6810
Adding replica 192.168.1.103:6821 to 192.168.1.103:6820
Adding replica 192.168.1.103:6831 to 192.168.1.103:6830
M: 09ed27a260d5db6e3aea8fae0f700e59cb43c8d5 192.168.1.103:6810
   slots:0-5460 (5461 slots) master
M: 0a3d65bbb9df8aad4b19759ee1be9f74ddc6fef7 192.168.1.103:6820
   slots:5461-10922 (5462 slots) master
M: 313dba97e6325c422bdcdf80e99864cce66a9d1c 192.168.1.103:6830
   slots:10923-16383 (5461 slots) master
S: 284ec3043718959a13bd0ffca6ba404a272f7d68 192.168.1.103:6811
   replicates 09ed27a260d5db6e3aea8fae0f700e59cb43c8d5
S: 8cfff09e5a8f74edd55e3a45fa4ed0cc72e1ddd3 192.168.1.103:6821
   replicates 0a3d65bbb9df8aad4b19759ee1be9f74ddc6fef7
S: aa54078cc2268bc7634b6944e1cc1dfab039aa44 192.168.1.103:6831
   replicates 313dba97e6325c422bdcdf80e99864cce66a9d1c
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.1.103:6810)
M: 09ed27a260d5db6e3aea8fae0f700e59cb43c8d5 192.168.1.103:6810
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 313dba97e6325c422bdcdf80e99864cce66a9d1c 192.168.1.103:6830
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 284ec3043718959a13bd0ffca6ba404a272f7d68 192.168.1.103:6811
   slots: (0 slots) slave
   replicates 09ed27a260d5db6e3aea8fae0f700e59cb43c8d5
S: 8cfff09e5a8f74edd55e3a45fa4ed0cc72e1ddd3 192.168.1.103:6821
   slots: (0 slots) slave
   replicates 0a3d65bbb9df8aad4b19759ee1be9f74ddc6fef7
M: 0a3d65bbb9df8aad4b19759ee1be9f74ddc6fef7 192.168.1.103:6820
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: aa54078cc2268bc7634b6944e1cc1dfab039aa44 192.168.1.103:6831
   slots: (0 slots) slave
   replicates 313dba97e6325c422bdcdf80e99864cce66a9d1c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

OK,到此完毕

测试一下:登录6810客户机,然后set name ywj,退出后再登录其他的客户机,分别get name,都能获取ywj

客户端登录命令:
redis-cli.exe -c -h 192.168.1.103 -p 6810 # 注意,一定要加上 -c ,不然set值时,会报错 (error) MOVED 5798 192.168.1.103:6820

java测试代码

package com.example.demo.utils;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

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

public class T3 {

	
	
	public static void main(String[] args) throws IOException {
		

        Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6810));  
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6811));    
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6820));  
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6821));  
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6830));
        jedisClusterNodes.add(new HostAndPort("192.168.1.103", 6831));
        JedisCluster jc = new JedisCluster(jedisClusterNodes);
        jc.set("a", "aaa");
        System.out.println(jc.get("a"));
        jc.close();
	}

}

整体完毕。

相关参考:http://redisdoc.com/topic/cluster-tutorial.html#redis-guaran

还有相关的 节点功能后面再学习做笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值