public class PrimaryKeyGenerator
{
private static readonly NedisClient client = new NedisClient(GetRedisConfig()); /// <summary>
/// 获取当前Redis配置实例
/// </summary>
/// <returns></returns>
private static RedisConfiguration GetRedisConfig()
{
string strRedisConfiguration = getRedisConfiguration();
RedisConfiguration redisConfig = JsonConvert.DeserializeObject<RedisConfiguration>(strRedisConfiguration);
return redisConfig;
} /// <summary>
/// 获取RedisConfiguration配置字符串
/// </summary>
/// <returns></returns>
private static string getRedisConfiguration()
{
return "{\"MasterNumber\":1,\"Sentinel\":\"192.168.47.131:26379,192.168.47.140:26379,192.168.47.140:26380,192.168.47.139:26379\",\"SentinelConnectTimeout\":1000,\"ReadWriteSeparate\":true,\"MasterName\":\"masterredis131\",\"DbNumber\":2,\"PoolSizeMultiplier\":0,\"PoolTimeOutSeconds\":0}";
//using (var client = new ConsulClient())
//{
// var kvPair = client.KV.Get("redis/cluster/default/RedisConfig").Result; // if (kvPair.Response != null && kvPair.Response.Value != null)
// {
// return Encoding.UTF8.GetString(kvPair.Response.Value, 0, kvPair.Response.Value.Length);
// } // return string.Empty;
//}
} /// <summary>
/// 计算主键时的增量
/// </summary>
private uint increment = 1; /// <summary>
/// 业务IdKey
/// </summary>
private string busnessIdKey = string.Empty; /// <summary>
/// 使用业务ID的key,ID增量初始化
/// </summary>
/// <param name="BusnessIdKey">业务IdKey</param>
/// <param name="Increment">id增量</param>
public PrimaryKeyGenerator(string BusnessIdKey, uint Increment)
{
increment = Increment;
busnessIdKey = BusnessIdKey;
} /// <summary>
/// 获取下一个主键ID
/// </summary>
/// <returns></returns>
public Int64 GetNextID()
{
return client.Increment(busnessIdKey, increment);
}
}

  测试代码:

class Program
{
static void Main(string[] args)
{
//设work IOCP min值
ThreadPool.SetMinThreads(300, 200); #region Id生成工具测试 //单线程测试
//GeneratorIDTest();
//多线程测试
for (int i = 0; i < 1000; i++)
{
ThreadStart num = new ThreadStart(GeneratorIDTest);
Thread numThread = new Thread(num);
numThread.Start();
}
#endregion Console.WriteLine("开始" + ids.Count()+ "mainID:" + Thread.CurrentThread.ManagedThreadId.ToString());
Thread.Sleep(30000);
Console.WriteLine("结束未去重:"+ids.Count()+"去重:"+ ids.Distinct().Count());
Console.WriteLine("结束未去重:" + safeIds.Count() + "去重:" + safeIds.Distinct().Count());
//Console.WriteLine("结束未去重:" + safeIds2.Count() + "去重:" + safeIds2.Distinct().Count()); Console.ReadKey();
} static List<long> ids = new List<long>();
//https://blog.csdn.net/liunianqingshi/article/details/79025818
static ConcurrentQueue<long> safeIds = new ConcurrentQueue<long>();
static Queue<long> safeIds2 = new Queue<long>(); private static void GeneratorIdTest()
{
var primaryKey = new PrimaryKeyGenerator("blog_id", 1);
for (int i = 0; i < 50; i++)
{
var id = primaryKey.GetNextID();
ids.Add(id);
safeIds.Enqueue(id);
//safeIds2.Enqueue(id);//System.ArgumentException:“目标数组的长度不够。请检查 destIndex 和长度以及数组的下限。”
Console.WriteLine("线程ID"+Thread.CurrentThread.ManagedThreadId.ToString() +":"+id);
}
}
}

  

05-29 00:52