Example #1
0
        public void getId_test()
        {
            var unixTimeMilliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

            Thread.Sleep(1000);
            var worker1 = new SnowflakeId(1, unixTimeMilliseconds);
            var worker2 = new SnowflakeId(2, unixTimeMilliseconds);
            var worker3 = new SnowflakeId(3, unixTimeMilliseconds);
            var worker4 = new SnowflakeId(4, unixTimeMilliseconds);

            HashSet <long> ids = new HashSet <long>();

            for (int i = 0; i < 1000000; i++)
            {
                ids.Add(worker1.NextId());
            }
            for (int i = 0; i < 1000000; i++)
            {
                ids.Add(worker2.NextId());
            }
            for (int i = 0; i < 1000000; i++)
            {
                ids.Add(worker3.NextId());
            }
            for (int i = 0; i < 1000000; i++)
            {
                ids.Add(worker4.NextId());
            }

            ids.Count.ShouldBe(1000000 * 4);

            Should.Throw <ArgumentException>((() => new SnowflakeId(-54, 1)));
            Should.Throw <ArgumentException>((() => new SnowflakeId(1, DateTimeOffset.UtcNow.AddDays(1).ToUnixTimeMilliseconds())));
        }
Example #2
0
        public static void Test2()
        {
            DataTable table = new DataTable();

            table.Columns.Add("Id");
            table.Columns.Add("Name");
            table.Columns.Add("Sort", typeof(long));
            table.Columns.Add("UpdateTime", typeof(DateTime));

            var row1 = table.NewRow();

            row1["Id"]         = Guid.NewGuid().ToString();
            row1["Name"]       = "zhangsan";
            row1["Sort"]       = DateTime.Now.ToTimestampMillisecond();
            row1["UpdateTime"] = DateTime.Now;
            var row2 = table.NewRow();

            row2["Id"]         = Guid.NewGuid().ToString();
            row2["Name"]       = "王五";
            row2["Sort"]       = DateTime.Now.ToTimestampMillisecond();
            row2["UpdateTime"] = DBNull.Value;
            table.Rows.Add(row1);
            table.Rows.Add(row2);

            var list = table.ToEntity <User>();

            var listUser = new List <User>()
            {
                new User {
                    Id = Guid.NewGuid().ToString(), Name = "张安", Sort = 111111, UpdateTime = DateTime.Now
                },
                new User {
                    Id = Guid.NewGuid().ToString(), Name = "wangwu", Sort = 222
                },
            };
            var val2 = listUser.ToDataTable();

            var snowflakeId = new SnowflakeId(2, 1);

            for (int i = 0; i < 1000; i++)
            {
                Console.WriteLine(snowflakeId.NextId());
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var snowId = new SnowflakeId(1, 1);
            var id     = snowId.NextId();

            var serviceProvider = new ConsulServiceProvider(new Uri("http://127.0.0.1:8500"));
            var myServiceA      = serviceProvider.CreateServiceBuilder(builder =>
            {
                builder.ServiceName = "MyServiceA";
                // 指定负载均衡器
                builder.LoadBalancer = TypeLoadBalancer.RoundRobin;
                // 指定Uri方案
                builder.UriScheme = Uri.UriSchemeHttp;
            });

            var httpClient = new HttpClient();
            var polly      = PolicyBuilder.CreatePolly();

            //
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine($"-------------第{i}次请求-------------");
                polly.Execute(() =>
                {
                    try
                    {
                        var uri = myServiceA.BuildAsync("health").Result;
                        Console.WriteLine($"{DateTime.Now} - 正在调用:{uri}");
                        var content = httpClient.GetStringAsync(uri).Result;
                        Console.WriteLine($"调用结果:{content}");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"调用异常:{e.GetType()}");
                    }
                });
                Task.Delay(1000).Wait();
            }
        }
Example #4
0
 /// <summary>
 /// 创建ID
 /// </summary>
 /// <returns></returns>
 public long Create()
 {
     return(_id.NextId());
 }
Example #5
0
 /// <summary>
 /// 生成雪花Id
 /// </summary>
 /// <returns></returns>
 public long Create()
 {
     return(_snowflakeId.NextId());
 }
Example #6
0
 /// <summary>
 /// 创建ID
 /// </summary>
 public long Create() => _id.NextId();
Example #7
0
 public static long GenerateId()
 {
     return(snowflake.NextId());
 }
Example #8
0
 /// <summary>
 /// Returns key
 /// </summary>
 public string Generate()
 {
     return(_snowflakeId.NextId().ToString());
 }