1.简述
redis版本需要大于3
集群6个节点,3个master,3个slave
生产环境的话,建议6个物理机上搭建
2.下载ruby和redis,安装过程中把复选框都选上
ruby
链接: https://pan.baidu.com/s/19JuhBMc9uEai4i3hsdTbsw 密码: e6je
redis
链接: https://pan.baidu.com/s/1d6E56jD11KIwZRloBeVV6Q 密码: ex4h
redis管理端
链接: https://pan.baidu.com/s/18WMQdLiSySDzUEHjDGmpmg 密码: msrh
3.下载culster目录,直接放到redis安装目录
集群配置
链接: https://pan.baidu.com/s/1Q1lMhINSv13f5LUD16i3xA 密码: s7ci
4.下载已经写好的util下的全部bat文件和rb文件,把bat,rb文件全部放在redis目录下
批处理文件
链接: https://pan.baidu.com/s/1OvbM4lOMdgo65CyV6gSbSQ 密码: c1pm
5.按照顺序执行bat文件
install.bat
把6个redis****服务全部启动
install_module.bat
install_culster.bat
出现 16383 slot 说明分片集群已经搭建完成
6.集群客户端,stackexchange.redis
public class RedisCulsterHelper
{
private ConnectionMultiplexer _connectionMultiplexer;
private IDatabase _database;
public RedisCulsterHelper()
{
Init();
}
private void Init()
{
ConfigurationOptions configurationOptions = ConfigurationOptions.Parse("127.0.0.1:6379,127.0.0.1:6390,127.0.0.1:6381,connectTimeout=2000");
_connectionMultiplexer = ConnectionMultiplexer.Connect(configurationOptions);
_database = _connectionMultiplexer.GetDatabase();
}
public bool StringSet(string key, string value)
{
return _database.StringSet(key, value);
}
public string StringGet(string key)
{
RedisValue value = _database.StringGet(key);
return value.IsNullOrEmpty ? null : value.ToString();
}
public bool HashSet(string hashKey,string fieldKey,string fieldValue)
{
return _database.HashSet(hashKey, fieldKey, fieldValue);
}
public string HashGet(string hashKey, string fieldKey)
{
RedisValue value = _database.HashGet(hashKey, fieldKey);
return value.IsNullOrEmpty ? null : value.ToString();
}
public bool StackSet(string listKey, string value)
{
return _database.ListRightPush(listKey, value) > 0;
}
public string StackGet(string listKey)
{
RedisValue value = _database.ListRightPop(listKey);
return value.IsNullOrEmpty ? null : value.ToString();
}
public bool QueueSet(string listKey, string value)
{
return _database.ListLeftPush(listKey, value) > 0;
}
public string QueueGet(string listKey)
{
RedisValue value = _database.ListLeftPop(listKey);
return value.IsNullOrEmpty ? null : value.ToString();
}
public bool SetSet(string setKey,string value)
{
return _database.SetAdd(setKey, value);
}
public string SetGet(string setKey)
{
RedisValue value = _database.SetPop(setKey);
return value.IsNullOrEmpty ? null : value.ToString();
}
public bool SortedSetSet(string setKey, string value,double score = 0)
{
return _database.SortedSetAdd(setKey, value, score);
}
public string[] SortedSetGet(string setKey,long start,long stop)
{
RedisValue[] value = _database.SortedSetRangeByRank(setKey,start,stop,Order.Ascending);
return value.ToStringArray();
}
}
7.非集群客户端也可以选择下面的,servicestack.redis
public class RedisHelper
{
private IRedisClient _redisClient;
private IRedisClient _readonlyRedisClient;
///
/// redis
///
///
redis://clientid:password@localhost:6380?ssl=true&db=1
///
redis://clientid:password@localhost:6380?ssl=true&db=1
public RedisHelper(string[] readWriteHosts, string[] readOnlyHosts = null)
{
if (readOnlyHosts == null)
{
readOnlyHosts = readWriteHosts;
}
InitConfig(readWriteHosts, readOnlyHosts);
}
private void InitConfig(string[] readWriteHosts,string[] readOnlyHosts)
{
RedisClientManagerConfig config = new RedisClientManagerConfig()
{
MaxWritePoolSize = 1000,
MaxReadPoolSize = 1000,
AutoStart = true,
DefaultDb = 0
};
PooledRedisClientManager pooledRedisClientManager =new PooledRedisClientManager(readWriteHosts, readOnlyHosts, config)
{
SocketSendTimeout = 3 * 1000,
SocketReceiveTimeout = 3 * 1000
};
_redisClient = pooledRedisClientManager.GetClient();
_readonlyRedisClient = pooledRedisClientManager.GetClient();
}
public bool SetString(string key,string value,DateTime dateTime)
{
return _redisClient.Set(key, value, dateTime);
}
public T GetString
(string key)
{
return _readonlyRedisClient.Get(key);
}
public void DeleteString(string key)
{
_redisClient.Delete(key);
}
public bool SetHash(string hashId,string key,string value)
{
return _redisClient.SetEntryInHash(hashId, key, value);
}
public Dictionary GetHash(string hashId)
{
return _readonlyRedisClient.GetAllEntriesFromHash(hashId);
}
public void SetStack(string stackId,string value)
{
_redisClient.PushItemToList(stackId,value);
}
public string GetStack(string stackId)
{
return _readonlyRedisClient.PopItemFromList(stackId);
}
public void SetQueue(string queueId, string value)
{
_redisClient.EnqueueItemOnList(queueId, value);
}
public string GetQueue(string queueId)
{
return _readonlyRedisClient.DequeueItemFromList(queueId);
}
public void SetSet(string setId, string value)
{
_redisClient.AddItemToSet(setId, value);
}
public HashSet GetSet(string setId)
{
return _readonlyRedisClient.GetAllItemsFromSet(setId);
}
public bool SetSortedSet(string sortedSetId, string value,double score = 0)
{
return _redisClient.AddItemToSortedSet(sortedSetId, value, score);
}
public List GetSortedSet(string sortedSetId)
{
return _readonlyRedisClient.GetAllItemsFromSortedSet(sortedSetId);
}
///
/// 执行锁操作
///
///
///
///
///
public bool ExecuteLock(Func command,string lockKey,int timeout = 3)
{
using (_redisClient.AcquireLock(lockKey, new TimeSpan(0,0,0,timeout)))
{
command(_redisClient);
}
return true;
}
///
/// 执行事务
///
///
///
public bool ExecuteTransaction(Func command)
{
using (IRedisTransaction transaction = _redisClient.CreateTransaction())
{
transaction.QueueCommand(command);
transaction.Commit();
}
return true;
}
}