Пример #1
0
        public async Task <object> GetToken(TokenRequst request)
        {
            var client = await _clientProvider.FindGrpcClient <Auth.AuthClient>("zero");

            var user = await client.LoginAsync(request);

            var config = new AppConfig();
            var token  = new JwtSecurityToken(
                audience: config.Gateway.Key,
                claims: new[]
            {
                new Claim(ClaimTypes.Sid, user.UserId.ToString()),
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Role, user.RoleId.ToString())
            },
                issuer: config.Gateway.Key,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddSeconds(7200),
                signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.Gateway.Secret)), SecurityAlgorithms.HmacSha256)
                );

            return(new KetchupResponse(new
            {
                AccessToken = new JwtSecurityTokenHandler().WriteToken(token),
                UserId = user.UserId,
                UserName = user.UserName,
                Expired = config.Gateway.AuthExpired,
                RoleId = user.RoleId
            }));
        }
Пример #2
0
        public async Task <object> GetToken(TokenRequst request)
        {
            var client = await _clientProvider.FindGrpcClient <Auth.AuthClient>("zero");

            var result = await client.TokenAsync(request);

            return(new KetchupReponse(result));
        }
Пример #3
0
        public async Task <object> GetService(string path)
        {
            var serverName = HttpContext.Request.Query;

            var client = await _clientProvider.FindGrpcClient <RpcTest.RpcTestClient>("sample");

            var request = new HelloRequest()
            {
                Age = 28, Name = "simple"
            };

            //await client.SayHelloAsync(request);

            var method = client.GetType().GetMethods().Where(item => item.Name == "SayHello")?.LastOrDefault();

            //var invoke = FastInvoke.GetMethodInvoker(method);

            //await (Task)invoke(client.GetType(), new[] { request });

            var result = method?.Invoke(client.GetType(), new object[] { request });

            return(Task.FromResult("ok"));
        }
Пример #4
0
        public static void Testing(IGrpcClientProvider provider)
        {
            Task.Run(async() =>
            {
                var count = 10000;

                //拦截器测试
                //var client = await provider.FindGrpcClient<RpcTest.RpcTestClient>("sample");

                //var request = new HelloRequest() { Age = 28, Name = "simple" };
                //var result = await client.AddPollyExecuteAsync(request, async () => await client.SayHelloAsync(request));

                //var result = await client.SayHelloAsync(new HelloRequest() { Age = 28, Name = "simple" });

                //await client.SayHelloEventAsync(new HelloRequest() { Age = 28, Name = "simple" });

                //Stopwatch sw1 = new Stopwatch();
                //sw1.Start();
                //Console.WriteLine($"开始执行获取客户端{count}次测试");

                //for (int i = 0; i < count; i++)
                //{
                //    await provider.FindGrpcClient<RpcTest.RpcTestClient>("sample");
                //}


                //sw1.Stop();
                //TimeSpan ts1 = sw1.Elapsed;
                //Console.WriteLine("执行获取客户端总共花费{0}ms.", ts1.TotalMilliseconds);


                var client   = await provider.FindGrpcClient <RpcTest.RpcTestClient>("sample");
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Console.WriteLine($"开始执行{count}次测试");
                for (int i = 0; i < count; i++)
                {
                    var result = await client.SayHelloAsync(new HelloRequest()
                    {
                        Age = 28, Name = "simple"
                    });
                    //Console.WriteLine($"{result.Msg}========{result.Code}==========={result.Result}");
                }
                sw.Stop();
                TimeSpan ts = sw.Elapsed;
                Console.WriteLine("执行总共花费{0}ms.", ts.TotalMilliseconds);

                ////todo 3

                //Stopwatch sw3 = new Stopwatch();
                //sw3.Start();
                //Console.WriteLine($"开始执行{count}次测试");

                //for (int i = 0; i < count; i++)
                //{
                //    var client = await provider.FindGrpcClient<RpcTest.RpcTestClient>("sample");
                //    var result = await client.SayHelloAsync(new HelloRequest() { Age = 28, Name = "simple" });
                //}


                //sw3.Stop();
                //TimeSpan ts3 = sw3.Elapsed;
                //Console.WriteLine("执行获取客户端跟消息通讯总共花费{0}ms.", ts3.TotalMilliseconds);

                Console.ReadKey();
            }).Wait();
        }