ruby ftp封装实例详解
最近自己用ruby 封装了一个Net::FTP的工具类.
Redis集群是一个提供在多个Redis节点间共享数据的程序集。 Redis集群中不支持处理多个keys的命令。 Redis集群通过分区来提供一定程度的可用性。在某个节点宕机或者不可用的时候可以继续处理命令。 Redis集群数据分片 在Redis集群中,使用数据分片(sharding)而不是一致性hash(consistency hashing)来实现,一个Redis集群包含16384个哈希槽(hash slot),数据库中的每个键都存在这些哈希槽中的某一个,通过CRC16校验后对16384取模来决定。 加入说现在集群中有三个节点,那么 ①节点A包含0到5500号哈希槽。 ②节点B包含5501到11000号哈希槽。 ③节点C包含11001到16384号哈希槽。 然后如果我们要增加一个节点的时候,会从ABC三个节点分别得到一部分槽到D上。如果我们移除一个节点的时候,就会把移除的节点的分隔槽移到剩下的槽上面。 Redis主从复制模型 为了实现Redis的集群的高可用性。Redis提供了一个主从复制模型。每个节点都会有N-1个复制品。 如果在创建集群的时候,我们为每个节点添加了一个从节点,这时候,如果其中的某个主节点挂掉了,便会把它的从节点做为新的主节点,继续提供服务。但是如果主节点和从节点都挂掉了,那就不可以继续使用了。 Redis集群搭建 Redis集群由多个云新在集群模式下的Redis实例组成。实例的集群模式需要通过配置来开启。 下面是一个包含了最少选项的集群配置文件实例:
port指定了节点的端口号 cluster-enabled yes选项用于开启实例的集群模式 cluster-config-file nodes.conf设置了保存节点的配置文件路径,这个文件无须认为修改,是在集群启动的时候创建。 cluster-node-timeout 5000:设置了方式失败的等待时间。即5秒还访问不了就认为这个节点不可用。 appendonly yes:用于开启aof持久化 pidfile /var/run/redis_xxxx.pid设置pid文件的位置,其中xxxx为端口号 dir /usr/local/redis-cluster/xxxx设置工作目录,其中xxxx为端口号。 此时目录中的文件是这样的
下面启动这六个节点
查看启动后的进程:
再次查看文件目录:
可以看到生成了aof和nodes文件,保存的时候还会有dump文件生成。 在看看pid
接下来,我们就需要使用redis-trib工具和这六个节点来创建集群了。 redis-trib位于redis源码的src目录下。 我们复制一份到redis-cluster目录下。
开始启动集群:
下面是本人失败的安装版本,后面也有成功的版本。 因为是ruby的程序,所以我们需要安装ruby,
继续启动集群:
还是报错了,说需要rubygems,我们继续安装:
继续启动集群:
依然还是报错了...,我们需要安装更高版本的ruby。 所以,我决定,先卸载掉1.8的ruby。
然后重新安装
然后查看ruby的版本:
最后,为了运行redis-trib我们需要安装redis gem。 如果出现下面的错误
我们继续开启集群:依然还是报错,几经折腾,还是没解决掉 最好,找到了下面这种方法。 成功版本 通过rvm这个ruby的管理工具来安装ruby。 首先安装rvm,如果下面的命令提示找不到curl,可以通过yum install -y crul来安装crul。
发现失败了,我们按照提示进行操作。
发现rvm已经安装成功了,为了让配置立即生效,我们需要用source命令导入rvm的配置文件。 先查看下配置文件的路径,然后导入。
接下来就可以安装高版本的ruby了,建议安装2.2.3版本,不然的话,等下gen install redis的时候,可能会报下面的错误:
废话少说,接下来安装2.2.3版本的ruby
可以看到,不进ruby安装好了2.3.3版本,还把rubygems也安装好了。 如果要删除掉某个版本的ruby可以用这个命令: 我们继续安装:
看到这里,折腾了两个晚上终于弄好了。接下来就是使用我们的redis-trib开启集群了。 为了方便,我把redis-trib.rb这个文件复制了一份到/usr/local/bin这个目录下,因为这个目录在PATH下面,里面的命令可以直接执行。 再次确认下六个redis节点的运行是ok的。
在创建的过程中,redis会给出一份预想的配置给我们看,确定没问题就输入yes并回车。系统就给我们创建了三个主,三个从的集群结构。 可以看到All 16384 slots covered。 redis-cli redis-cli默认情况下连接的是本地的6379端口的redis服务器。 现在有六个端口,所以需要指定参数。 redis-cli -h xxx -p xxx -a xxx -h:指定服务器 -p:指定端口号 -a:指定密码 -c:开启集群模式
可以看到,已经可以共享数据了。 但是,如果我们把某一个主节点杀掉呢?
可以看到7005端口的节点已经变成主节点了。那如果继续把7005节点也宕掉呢?
可以看到集群已经出现问题了。 追记: 在搭建多机多点集群的时候,遇到以下问题: 1、一直在等待... >>> Nodes configuration updated 解决办法:确认端口是否开放。如果没有开发,可以修改/etc/sysconfig/iptables文件,开放端口(复制一行前面开放端口的来修改),然后service iptables restart重启防火墙。 极端的可以service iptables stop来关闭防火墙 2、槽被利用
解决办法:删除掉在redis.conf的dir目录下的几个文件redis.conf文件除外。 如果还是有相同的问题,可以考虑对所有节点执行下面两个命令。 redis-cli -p 端口号-h 主机物理地址 FLUSHALL redis-cli -p 端口号-h 主机物理地址 CLUSTER RESET SOFT |
# 获取指定格式的文件名称列表
# 例如: source = "test/*.txt"
# 返回: [source/file_name.txt]
1 2 3 4 5 6 7 8 9 10 11 |
|
# 获取服务器上确切名称的文件
# 例如: get("test/test.txt")
# 文件将被保存到本地 tmp/test/test.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
# 上传文件到指定的路径
# 例如: put("tmp\\test\\test.txt", "/test/")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
# 关闭ftp
1 2 3 4 5 6 7 |
|
# 服务器copy文件
1 2 3 4 5 6 7 |
|
#1. 到本地
1 2 3 4 5 |
|
#2. 到服务器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
# 服务器上移动文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
# 重命名服务器文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
# 删除服务器上的文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
# ftp 是否关闭
1 2 3 4 5 6 7 8 9 |
|
# 文件编码转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
#生成ftp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
#本地路径
1 2 3 4 5 6 7 8 9 10 11 |
|
# 删除本地文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
# 重命名本地文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
#初始化参数
1 2 3 4 5 6 7 8 9 10 11 |
|