private static void Main() { //客户端基本服务。 ISerializer <string> serializer = new JsonSerializer(); ISerializer <byte[]> byteArraySerializer = new StringByteArraySerializer(serializer); ISerializer <object> objectSerializer = new StringObjectSerializer(serializer); IServiceIdGenerator serviceIdGenerator = new DefaultServiceIdGenerator(new ConsoleLogger <DefaultServiceIdGenerator>()); var typeConvertibleService = new DefaultTypeConvertibleService(new[] { new DefaultTypeConvertibleProvider(objectSerializer) }, new ConsoleLogger <DefaultTypeConvertibleService>()); var serviceRouteManager = new SharedFileServiceRouteManager("d:\\routes.txt", serializer, new ConsoleLogger <SharedFileServiceRouteManager>()); //zookeeper服务路由管理者。 // var serviceRouteManager = new ZooKeeperServiceRouteManager(new ZooKeeperServiceRouteManager.ZookeeperConfigInfo("172.18.20.132:2181"), serializer, new ConsoleLogger<ZooKeeperServiceRouteManager>()); // IAddressSelector addressSelector = new RandomAddressSelector(); IAddressSelector addressSelector = new PollingAddressSelector(); IAddressResolver addressResolver = new DefaultAddressResolver(serviceRouteManager, new ConsoleLogger <DefaultAddressResolver>(), addressSelector); ITransportClientFactory transportClientFactory = new DotNettyTransportClientFactory(byteArraySerializer, new ConsoleLogger <DotNettyTransportClientFactory>()); var remoteInvokeService = new RemoteInvokeService(addressResolver, transportClientFactory, new ConsoleLogger <RemoteInvokeService>()); //服务代理相关。 IServiceProxyGenerater serviceProxyGenerater = new ServiceProxyGenerater(serviceIdGenerator); var services = serviceProxyGenerater.GenerateProxys(new[] { typeof(IUserService) }).ToArray(); IServiceProxyFactory serviceProxyFactory = new ServiceProxyFactory(remoteInvokeService, typeConvertibleService); //创建IUserService的代理。 var userService = serviceProxyFactory.CreateProxy <IUserService>(services.Single(typeof(IUserService).IsAssignableFrom)); var logger = new ConsoleLogger(); while (true) { Task.Run(async() => { try { Console.WriteLine($"userService.GetUserName:{await userService.GetUserName(1)}"); Console.WriteLine($"userService.GetUserId:{await userService.GetUserId("rabbit")}"); Console.WriteLine($"userService.GetUserLastSignInTime:{await userService.GetUserLastSignInTime(1)}"); Console.WriteLine($"userService.Exists:{await userService.Exists(1)}"); var user = await userService.GetUser(1); Console.WriteLine($"userService.GetUser:name={user.Name},age={user.Age}"); Console.WriteLine($"userService.Update:{await userService.Update(1, user)}"); Console.WriteLine($"userService.GetDictionary:{(await userService.GetDictionary())["key"]}"); await userService.TryThrowException(); } catch (RpcRemoteException remoteException) { logger.Error(remoteException.Message); } }).Wait(); Console.ReadLine(); } }
/// <summary> /// Инициализирует сервисы. /// </summary> /// <param name="method_service">Сервис для работы и хранения методов.</param> /// <param name="assembly_service">Сервис для работы с библиотеками C#.</param> /// <param name="coordination_service">Сервис координации.</param> /// <param name="data_service">Сервис хранения данных.</param> /// <param name="web_server_service"></param> public InvokeServiceFactory(IMethodService method_service, IAssemblyService assembly_service, ICoordinationService coordination_service, IDataService <DataInvoke> data_service, IWebServerService web_server_service, RemoteInvokeService remote_invoke_service) { _methodService = method_service; _assemblyService = assembly_service; _coordinationService = coordination_service; _webServerService = web_server_service; _remoteInvokeService = remote_invoke_service; // new RemoteInvokeService(_coordinationService, _webServerService); var invoke_c_sharp_method = new InvokeCSharpService(_assemblyService, _methodService, data_service); var invoke_executable_file = new InvokeExecutableFileService(new ExecutableFileMethodService(), data_service); _serviceDictionary = new Dictionary <Type, IInvokeService> { /*{typeof(RemoteInvokeService), remote_invoke_service}, * {typeof(InvokeCSharpService), invoke_c_sharp_method},*/ { typeof(CSharpMethod), invoke_c_sharp_method }, { typeof(ExecutableFileMethod), invoke_executable_file } }; }