c#与redis-window下redis集群安装

第一步:在Redis下载,由于redis(官网 https://redis.io/)是在linux上开发出来的,官网上没有下载windows版本的地址,不过github上有,请按照如图所示进行下载。

登录redis官网https://redis.io/导航到download页面,并找到下面这段话:

Windows

The Redis project does not officially support Windows. However, the Microsoft Open Tech group develops and maintains this Windows port targeting Win64. Learn more

大概意思就是redis项目官方并没支持windows的版本,不过“微软开源技术团队”有维护windows版本,并且只支持64位。点击上方地址进入windows版本的开源。(官方只支持64位版本,如果要32需自己下载源码编译)。

 releases 地址: https://github.com/MSOpenTech/redis/releases             

我下的是压缩文件,解压后文件如下:

 

此时可以进行单机测试,打开redis-server.exe(默认端口为6379),然后打开redis-cli.exe(客户端),此时在可以客户端上进行get,set操作:

第二步,redis Cluster集群环境安装(本文的集群方案采用redis3.0官方自带的cluster方式部署,3.0之前官方不支持集群):

1、先在上述下载的redis程序集根目录下创建6个文件夹:7000 7001 7002 7003 7004 7005

2、修改redis.conf文件,分别放到上述文件夹中,且每个conf的端口号要记的修改(7000~7005),conf内容:

port 7000

cluster-enabled yes

cluster-config-file nodes-7000.conf

cluster-node-timeout 5000

appendonly yes

 

3、启动上述动6个服务端,为方便启动这些 Redis 实例,新建如下 bat 文件:

@echo off
cd c:\Redis
start Redis-Server ./7000/redis.conf
start Redis-Server ./7001/redis.conf
start Redis-Server ./7002/redis.conf
start Redis-Server ./7003/redis.conf
start Redis-Server ./7004/redis.conf
start Redis-Server ./7005/redis.conf

 

4,下载 RubyInstaller
http://rubyinstaller.org/downloads/


安装时,勾选
Install Td/Tk Support
Add Ruby executables to your PATH
Associate .rb and .rbw files with this Ruby installation

 

5,下载 redis-trib.rb , 放到 c:\redis 目录下备用
https://raw.githubusercontent.com/antirez/redis/unstable/src/redis-trib.rb

6,安装 GEM,Redis 的 ruby 支持环境

https://rubygems.org/
点击 setup.rb 安装。
 
由于 GFW的问题, GEM 的源在国内不可用,所以使用镜像:
 打开cmd依次敲入如下命令:
添加:
gem sources -a  http://gems.ruby-china.org
 
查看已存在的源:
gem sources -l
 
删除被墙的源:
gem sources -r https://rubygems.org/
 
安装 Redis 支持环境:
gem install redis
 
7、开始配置cluster集群:

打开 cmd , 执行以下命令:
cd c:\redis    (第一步中下载的redis目录)
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

--replicas 1 即自动分配 Slave , 如果想手动指定 Slave  , 将该值变为 0 即可, 地址列表中,只需要 3个实例即可。

 

由于使用的是 6个实例,自动分配 Slave ,所以前3个为 master , 后3 个为 slave, 并确定3个主节点的 slots 范围。

如果确认没有问题, 输入 yes
如果群集创建成功, 会输出 OK XXXXX
如果出现:
err slot xxx is already busy, 请删除 appendonly.aof 及 nodes-xxx.conf (cluster-config-file 所指的文件) 文件

 

8、测试集群:(注意-c 表示进入的是集群环境)

redis-cli.exe -c -p 7000

第三步,现在开始使用c# 客户端访问集群

流行的.net客户端有ServiceStack.Redis 和 StackExchange.Redis,其中 StackExchange.Redis 最新版已经商业化了,为了避免踩坑,本文 StackExchange.Redis+扩展工具StackExchange.Redis.Extensions.Core。

1、创建测试工程。

2、nuget 安装StackExchange.Redis 和StackExchange.Redis.Extensions.Core.

 StackExchange.Redis.Extensions.Core 支持多种序列化方式:

  • BinarySerialization (Requires SerializableAttribute on top of the class to store into Redis)
  • NewtonSoft (Uses JSon.Net to serialize a class without SerializableAttribute)
  • Jil (Use super fast json serializer)
  • MessagePack CLI (serialization/deserialization for CLI)

本示例使用NewtonSoft,所以继续nuget: StackExchange.Redis.Extensions.Newtonsoft

 

配置文件设置 cachePort可以设置为任意节点的端口号:

<configuration>
  <configSections>
    <section name="redisCacheClient"
           type="StackExchange.Redis.Extensions.Core.Configuration.RedisCachingSectionHandler, StackExchange.Redis.Extensions.Core" />
  </configSections>
  <redisCacheClient allowAdmin="true" ssl="false" connectTimeout="5000" database="0" password="">
    <hosts>
      <add host="127.0.0.1" cachePort="7000"/>
    </hosts>
  </redisCacheClient>
    ..................
</configuration>

开始测试代码:

  //使用Newtonsoft序列化
            var serializer = new NewtonsoftSerializer();
            var redis = new StackExchangeRedisCacheClient(serializer);
            //获取服务器信息
            var info = redis.GetInfo();
            foreach (var item in info)
            {
                Console.WriteLine(string.Format("{0}:{1}",item.Key,item.Value));
            }
            redis.Add<User>("UserInfoKey",new User { Name="uucode",Age=30});
            var user= redis.Get<User>("UserInfoKey");

            var userCollection = new List<User>() {
                new User { Name="user1",Age=1},
                new User { Name="user2",Age=2}
            };
            redis.Add<List<User>>("userColl",userCollection);
            var userColl= redis.Get < List<User>>("userColl");
          
            redis.Add("c2", "fdsafdas");
            redis.Add("c3", "fdsafdas");
            redis.Add("c4", "fdsafdas");
            redis.Add("c5", "fdsafdas");
            Console.Read();

 以上所有的文件及源码点击此处下载

 

posted on 2017-03-01 23:19  柚子代码  阅读(959)  评论(0编辑  收藏  举报