コード例 #1
0
ファイル: SerializerExtension.cs プロジェクト: jjjyyy12/Jy
        public static IServiceCollection AddSerializerServices(this IServiceCollection services)
        {
            var jsonSerializer            = new JsonSerializer();                          //ISerializer<string>
            var stringByteArraySerializer = new StringByteArraySerializer(jsonSerializer); //ISerializer<byte[]>
            var stringObjectSerializer    = new StringObjectSerializer(jsonSerializer);    //ISerializer<object>

            services.AddSingleton(jsonSerializer);
            services.AddSingleton(stringByteArraySerializer);
            return(services.AddSingleton(stringObjectSerializer));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: swatabc/Rpc
        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();
            }
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: xiongeee/Rpc
        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,objectSerializer, 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();
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: swatabc/Rpc
        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>());
            IServiceInstanceFactory serviceInstanceFactory = new DefaultServiceInstanceFactory(new ConsoleLogger <DefaultServiceInstanceFactory>());
            ITypeConvertibleService typeConvertibleService = new DefaultTypeConvertibleService(new[] { new DefaultTypeConvertibleProvider(objectSerializer) }, new NullLogger <DefaultTypeConvertibleService>());
            IClrServiceEntryFactory clrServiceEntryFactory = new ClrServiceEntryFactory(serviceInstanceFactory, serviceIdGenerator, typeConvertibleService);
            var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetExportedTypes());
            var serviceEntryProvider = new AttributeServiceEntryProvider(types, clrServiceEntryFactory, new ConsoleLogger <AttributeServiceEntryProvider>());
            IServiceEntryManager serviceEntryManager = new DefaultServiceEntryManager(new IServiceEntryProvider[] { serviceEntryProvider });
            IServiceEntryLocate  serviceEntryLocate  = new DefaultServiceEntryLocate(serviceEntryManager);

            //自动生成服务路由(这边的文件与Echo.Client为强制约束)
            {
                var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute
                {
                    Address = new[] { new IpAddressModel {
                                          Ip = "127.0.0.1", Port = 9981
                                      } },
                    ServiceDescriptor = i.Descriptor
                });

                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>());
                serviceRouteManager.AddRoutesAsync(addressDescriptors).Wait();
            }

            IServiceHost serviceHost = new DotNettyServiceHost(new DefaultServiceExecutor(serviceEntryLocate, byteArraySerializer, new ConsoleLogger <DefaultServiceExecutor>()), new ConsoleLogger <DotNettyServiceHost>(), byteArraySerializer);

            Task.Factory.StartNew(async() =>
            {
                //启动主机
                await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9981));
                Console.WriteLine($"服务端启动成功,{DateTime.Now}。");
            });
            Console.ReadLine();
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: yhhno/Rpc
        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>());
            IServiceInstanceFactory serviceInstanceFactory = new DefaultServiceInstanceFactory(new ConsoleLogger<DefaultServiceInstanceFactory>());
            ITypeConvertibleService typeConvertibleService = new DefaultTypeConvertibleService(new[] { new DefaultTypeConvertibleProvider(objectSerializer) }, new NullLogger<DefaultTypeConvertibleService>());
            IClrServiceEntryFactory clrServiceEntryFactory = new ClrServiceEntryFactory(serviceInstanceFactory, serviceIdGenerator, typeConvertibleService);
            var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetExportedTypes());
            var serviceEntryProvider = new AttributeServiceEntryProvider(types, clrServiceEntryFactory, new ConsoleLogger<AttributeServiceEntryProvider>());
            IServiceEntryManager serviceEntryManager = new DefaultServiceEntryManager(new IServiceEntryProvider[] { serviceEntryProvider });
            IServiceEntryLocate serviceEntryLocate = new DefaultServiceEntryLocate(serviceEntryManager);

            //自动生成服务路由(这边的文件与Echo.Client为强制约束)
            {
                var addressDescriptors = serviceEntryManager.GetEntries().Select(i => new ServiceRoute
                {
                    Address = new[] { new IpAddressModel { Ip = "127.0.0.1", Port = 9981 } },
                    ServiceDescriptor = i.Descriptor
                });

                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>());
                serviceRouteManager.AddRoutesAsync(addressDescriptors).Wait();
            }

            IServiceHost serviceHost = new NettyServiceHost(new DefaultServiceExecutor(serviceEntryLocate,byteArraySerializer,new ConsoleLogger<DefaultServiceExecutor>()) ,new ConsoleLogger<NettyServiceHost>());

            Task.Factory.StartNew(async () =>
            {
                //启动主机
                await serviceHost.StartAsync(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9981));
                Console.WriteLine($"服务端启动成功,{DateTime.Now}。");
            });
            Console.ReadLine();
        }