public IList <KeyValueInfo <string, string> > Get() { var list = new List <KeyValueInfo <string, string> >(); var req = new HelloRequest(); for (var i = 0; i < 100; i++) { var kv = new KeyValueInfo <string, string>(); req.Name = "黄华英" + i; try { GRpcChannelUtil.GetGRpcClientFormStrategy <GreeterClient>("GRpcServiceExampleA", (channel) => { kv.Key = channel.Target; return(new GreeterClient(channel)); }, (client, header) => { var res = client.SayHello(req); kv.Value = res.Message; }); } catch (Exception ex) { kv.Value = ex.Message; } list.Add(kv); Console.WriteLine($"{DateTime.Now.ToFullFixedDateTime()} 请求后结果:" + kv.ToJsonString()); Thread.Sleep(1000); } return(list); }
/// <summary> /// 创建基本的通用数据 /// </summary> /// <param name="context">上下文</param> /// <param name="key">键</param> /// <param name="menuCode">菜单编码</param> /// <param name="authToken">授权票据</param> /// <param name="functionCodes">功能编码数组</param> /// <returns>基本的通用数据</returns> public static CommonUseData CreateBasicCommonUseData(this HttpContext context, string key = null, string menuCode = null, IAuthToken authToken = null, params string[] functionCodes) { var result = new CommonUseData() { Key = key, MenuCode = menuCode, FunctionCodes = functionCodes }; if (context != null && context.Request != null) { result.Path = context.Request.Path.Value.ToLower(); var routeValue = context.Request.RouteValues; var routes = routeValue.GetControllerAction(); if (routes != null && routes.Length > 1) { result.Controller = routes[0]; result.Action = routes[1]; } result.Token = authToken == null?context.Request.GetBearerOriginTokenFromHeader() : authToken.GetToken(); if (context.Request.Headers != null && context.Request.Headers.ContainsKey(App.EVENT_ID_KEY)) { result.EventId = context.Request.Headers[App.EVENT_ID_KEY]; } result.CommMode = GRpcChannelUtil.IsRequestGRpc(context.Request.ContentType) ? CommunicationMode.GRPC : CommunicationMode.HTTP; } return(result); }
public void Test() { GRpcChannelUtil.GetGRpcClientFormStrategy <Greeter.GreeterClient>("service1", channel => { return(new Greeter.GreeterClient(channel)); }, (client, header) => { var msg = client.SayHello(new HelloRequest() { Name = "李" }); }); }
private static void Example1() { var serviceProvider = new ConsulServicesProviderMemory(); var serviceOptions = new UnityServicesOptionsCache(); var unityServicesBuilder = new UnityServicesBuilder(serviceProvider, serviceOptions); for (var i = 0; i < 1000; i++) { var url = unityServicesBuilder.BuilderAsync("GRpcServiceExampleA").Result; GRpcChannelUtil.CreateChannel(url, (channel, header) => { var client = new Greeter.GreeterClient(channel); var res = client.SayHello(new HelloRequest() { Name = "张三" + i }); Console.WriteLine($"第{i}次请求[{url}]:{res.ToJsonString()}"); Thread.Sleep(1000); }); } }
/// <summary> /// 执行 /// </summary> /// <param name="context">http上下文</param> /// <returns>任务</returns> public async Task InvokeAsync(HttpContext context) { var routeValue = context.Request.RouteValues; var routes = routeValue.GetControllerAction(); // 过滤掉非控制器(排除掉GRpc) if (routes.IsNullOrLength0() && !GRpcChannelUtil.IsRequestGRpc(context.Request.ContentType)) { await next(context); return; } var stop = new Stopwatch(); stop.Start(); var path = context.Request.Path.Value.ToLower(); await next(context); stop.Stop(); var msg = new StringBuilder($"请求:{path} method:{context.Request.Method} "); string controller = null, action = null; if (routes != null && routes.Length == 2) { controller = routes[0]; action = routes[1]; msg.AppendFormat("controller:{0},action:{1}.", controller, action); } msg.Append($"耗时:{stop.ElapsedMilliseconds}ms"); var msgStr = msg.ToString(); string eventId = theOperation != null ? theOperation.EventId : null; switch (options.LogLevel) { case LogLevelEnum.TRACE: _ = log.TraceAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.DEBUG: _ = log.DebugAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.WRAN: _ = log.WranAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.INFO: _ = log.InfoAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.ERROR: _ = log.ErrorAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; case LogLevelEnum.FATAL: _ = log.FatalAsync(msgStr, null, "RequestLogMiddleware", eventId: eventId, path, controller, action); break; } }