public virtual void AddHeadlthCheck(ICollection <Service> services) { if (services.Count == 0) { return; } IServiceHealthCheckCreator checkCreator = GlobalSetting.GetService <IServiceHealthCheckCreator>(); if (checkCreator == null) { throw new NullReferenceException("IServiceHealthCheckCreator接口不能为空"); } AgentCheckRegistration check = checkCreator.CreateCheck(); if (check == null) { throw new NullReferenceException("无法创建健康检查实例"); } var task = _client.Agent.CheckRegister(check); task.ConfigureAwait(false); task.Wait(_waitConsulTime); if (task.Result.StatusCode != System.Net.HttpStatusCode.OK) { throw new Exception("添加健康检查信息失败"); } checkCreator.EnableHealthCheckInterface(); }
private void Test() { int threadCount = 2000; int count = 0; for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(() => { UserService.Iface userService = GlobalSetting.GetService <UserService.Iface>(); for (int j = 0; j < 10; j++) { UserInfo user = userService.GetUser(10); } IDisposable dispose = userService as IDisposable; dispose.Dispose(); Interlocked.Increment(ref count); }); thread.Start(); } bool timeout = false; //Timer timer = new Timer((state) => timeout = true,null,10000,-1); while (count < threadCount && !timeout) { } if (timeout) { Console.WriteLine($"超时:执行成功{count}次"); } else { Console.WriteLine($"正常结束:执行成功{count}次"); } }
protected virtual object CreateInstance(Service service, TTransport transport) { ThriftService thriftService = service as ThriftService; if (thriftService == null) { throw new InvalidCastException("无法转换成ThriftService"); } if (transport == null) { throw new ArgumentNullException(nameof(transport)); } if (!transport.IsOpen) { transport.Open(); } TProtocol protocol = new TBinaryProtocol(transport); TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, thriftService.Name); object instance = Activator.CreateInstance(thriftService.ServiceType, multiplexedProtocol); IDynamicProxyBuilder proxyBuilder = GlobalSetting.GetService <IDynamicProxyBuilder>(); if (proxyBuilder == null) { return(instance); } Type proxyType = proxyBuilder.CreateProxy(thriftService.ServiceInterfaceType); return(Activator.CreateInstance(proxyType, instance)); }
public void 客户端新增一条数据() { UserService.Iface userService = GlobalSetting.GetService <UserService.Iface>(); Assert.AreEqual(true, userService.Add(new UserInfo { UserID = 10, UserName = "******", Sex = true })); IDisposable dis = userService as IDisposable; dis?.Dispose(); }
public void 使用连接池内缓存的TTransport进行请求() { Test(); ThriftConnectionPool pool = GlobalSetting.GetService <IThriftConnectionPool>() as ThriftConnectionPool; foreach (var item in pool.ConnectionStore.ConnectionPool) { Console.WriteLine("连接池内的TTransport:" + item.Value.Count); } }
public void 客户端单个请求() { UserService.Iface userService = GlobalSetting.GetService <UserService.Iface>(); for (int j = 0; j < 1000; j++) { UserInfo user = userService.GetUser(10); Assert.IsNotNull(user); Assert.AreEqual(10, user.UserID); } IDisposable dis = userService as IDisposable; dis?.Dispose(); }
public void 使用连接池内缓存的TTransport请求_线程延时以达到最大的缓存限制() { int threadCount = 1000; int count = 0; for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(() => { UserService.Iface userService = GlobalSetting.GetService <UserService.Iface>(); for (int j = 0; j < 10; j++) { UserInfo user = userService.GetUser(10); } Thread.Sleep(1000); IDisposable dispose = userService as IDisposable; dispose.Dispose(); Interlocked.Increment(ref count); }); thread.Start(); } bool timeout = false; //Timer timer = new Timer((state) => timeout = true,null,10000,-1); while (count < threadCount && !timeout) { } if (timeout) { Console.WriteLine($"超时:执行成功{count}次"); } else { Console.WriteLine($"正常结束:执行成功{count}次"); } ThriftConnectionPool pool = GlobalSetting.GetService <IThriftConnectionPool>() as ThriftConnectionPool; foreach (var item in pool.ConnectionStore.ConnectionPool) { Console.WriteLine("连接池内的TTransport:" + item.Value.Count); } }
public static Type GetTypeFromAssemblies(string serviceInterfaceStr) { IServiceAssembliesResolver assembliesResolver = GlobalSetting.GetService <IServiceAssembliesResolver>(); if (assembliesResolver == null) { throw new NullReferenceException("IServiceAssembliesResolver接口不能为空"); } IEnumerable <Assembly> assemblies = assembliesResolver.GetAssemblies(); foreach (Assembly assembly in assemblies.Where(tmp => tmp != null)) { Type interfaceType = assembly.GetType(serviceInterfaceStr); if (interfaceType != null) { return(interfaceType); } } return(null); }
static void Main(string[] args) { ThriftServiceContainer serviceContainer = new ThriftServiceContainer(); //serviceContainer.AddActionFilter(new TestActionFilter()); GlobalSetting.Start(serviceContainer); //UserService.Iface tmpService = GlobalSetting.GetService<UserService.Iface>(); //tmpService.Add(new UserInfo { UserID = 10, UserName = "******", Sex = true }); //UserInfo tmp123 = tmpService.GetUser(10); //Console.WriteLine(JsonConvert.SerializeObject(tmp123)); //return; //container.Reaplce(typeof(IThriftConnectionPool),new FreshConnectionPool()); //container.AddActionFilter(new TestActionFilter()); //SelfServiceAssembliesResolver resolver = new SelfServiceAssembliesResolver(); //container.Reaplce(typeof(IServiceAssembliesResolver), resolver); int threadCount = 2000; int count = 0; Stopwatch watch = new Stopwatch(); watch.Start(); for (int i = 0; i < threadCount; i++) { Thread thread = new Thread(() => { IDisposable dis = null; try { UserService.Iface userService = GlobalSetting.GetService <UserService.Iface>(); for (int j = 0; j < 10; j++) { UserInfo user = userService.GetUser(10); } dis = userService as IDisposable; Console.WriteLine("OK"); } catch (Exception err) { Console.WriteLine(err.Message); } finally { dis?.Dispose(); Interlocked.Increment(ref count); } }); thread.Start(); } bool timeout = false; //Timer timer = new Timer((state) => timeout = true,null,10000,-1); while (count < threadCount && !timeout) { } if (timeout) { Console.WriteLine($"超时:执行成功{count}次"); } else { Console.WriteLine($"正常结束:执行成功{count}次"); } watch.Stop(); Console.WriteLine("耗时" + watch.ElapsedMilliseconds); ThriftConnectionPool pool = GlobalSetting.GetService <IThriftConnectionPool>() as ThriftConnectionPool; if (pool == null) { return; } foreach (var item in pool.ConnectionStore.ConnectionPool) { Console.WriteLine("连接池内的TTransport:" + item.Value.Count); int index = 0; foreach (var tmp in item.Value) { index++; Console.WriteLine(index + " " + (tmp.IsFree ? "空闲":"忙碌")); } } Thread.Sleep(9 * 1000); UserService.Iface face = GlobalSetting.GetService <UserService.Iface>(); Console.WriteLine("剩余连接:" + pool.ConnectionStore.ConnectionPool.First().Value.Count); Console.ReadKey(); }