/// <summary> /// init /// </summary> /// <param name="config"></param> static public void Init(Config.ThriftConfigSection config) { if (config == null) throw new ArgumentNullException("config"); foreach (Config.ServiceConfig serviceConfig in config.Services) { var objThriftService = Type.GetType(serviceConfig.ServiceType) .GetConstructor(new Type[0]) .Invoke(new object[0]); var objThriftProcessor = Type.GetType(serviceConfig.ProcessorType) .GetConstructor(new Type[] { objThriftService.GetType() }) .Invoke(new object[] { objThriftService }) as Thrift.IAsyncProcessor; var socketServer = new ThriftServer(serviceConfig.Port, new ThriftService(objThriftProcessor), serviceConfig.SocketBufferSize, serviceConfig.MessageBufferSize, serviceConfig.MaxMessageSize, serviceConfig.MaxConnections); socketServer.ApplyConfig(serviceConfig); _serverList.Add(socketServer); } }
public ServerTester Invoke() { ThriftServiceProcessor processor = new ThriftServiceProcessor(loggerFactory: null, services: new SimpleService()); bossExecutor = new MultithreadEventLoopGroup(1); ioWorkerExecutor = new MultithreadEventLoopGroup(1); ThriftServerDef serverDef = new ThriftServerDef(processor, new TBinaryProtocol.Factory()); NettyServerConfig serverConfig = new NettyServerConfig(bossGroup: bossExecutor, workerGroup: ioWorkerExecutor); Server = new ThriftServer(serverConfig, serverDef); return(this); }
/// <summary> /// 服务启动 /// </summary> /// <returns></returns> internal Task <ThriftyServer> StartAsync() { Contract.Assert(this._config != null && this._config.Port != 0, "swifty server port is null"); Contract.Assert(this._serviceTypes != null && this._serviceTypes.Any(), "No service can be used to start swifty server."); Contract.Assert( this._state == ThriftyServerStatus.Init, "swifty server can not start, the state is out of control"); this._state = ThriftyServerStatus.Starting; Task task; try { ThriftCodecManager thriftCodecManager; thriftCodecManager = this._codescs == null ? new ThriftCodecManager() : new ThriftCodecManager(this._codescs); INiftyProcessor process = new ThriftServiceProcessor( this._serviceLocator, codecManager: thriftCodecManager, eventHandlers: this._handlers, serviceTypes: this._serviceTypes, loggerFactory: this._loggerFactory); this._server = new ThriftServer(process, this._config, _sslConfig, this._loggerFactory); String addres = String.IsNullOrWhiteSpace(_config.BindingAddress) ? "0.0.0.0" : _config.BindingAddress; this._logger.LogDebug($"server is ready for starting: {addres}:{_config.Port}"); task = this._server.StartAsync(); return(task.ContinueWith(t => { if (t.Exception == null) { this._state = ThriftyServerStatus.Running; } this._state = (t.Exception == null) ? ThriftyServerStatus.Running : ThriftyServerStatus.Error; return this; })); } catch (Exception e) { this._state = ThriftyServerStatus.Error; throw new ThriftyException(e.Message); } }
private static void StartServerByAnnotaions() { var factory = new LoggerFactory(); factory.AddConsole(LogLevel.Debug); var processor = new ThriftServiceProcessor(factory, new Thrifty.ScribeTest()); var config = new ThriftServerConfig() { BindingAddress = "0.0.0.0", Port = 9999, IdleConnectionTimeout = TimeSpan.FromMinutes(10), QueueTimeout = TimeSpan.FromMinutes(10), TaskExpirationTimeout = TimeSpan.FromMinutes(10) }; var server = new ThriftServer(processor, config, loggerFactory: factory); server.StartAsync(); }
static void Main(string[] args) { try { var section = ConfigurationManager.GetSection("thrift.hosts") as HostSetion; foreach (var host in section.Hosts) { Console.WriteLine("{0} {1} {2} {3} {4}", host.Name, host.Port, host.MinThreadPoolSize, host.MaxThreadPoolSize, host.Services.Count); foreach (var service in host.Services) { Console.WriteLine("{0} {1}", service.Contract, service.Handler); } } ThriftServer serverHost = new ThriftServer(); serverHost.Start(); TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor(); multiplexedProcessor.RegisterProcessor(typeof(AddressRpc).FullName, new AddressRpc.Processor(new AddressRpcImpl())); multiplexedProcessor.RegisterProcessor(typeof(SmsSendShortMessageRpc).FullName, new SmsSendShortMessageRpc.Processor(new SmsSendShortMessageRpcImpl())); TServerTransport transport = new TServerSocket(6011); TPrototypeProcessorFactory <AddressRpc.Processor, AddressRpcImpl> factory = new TPrototypeProcessorFactory <AddressRpc.Processor, AddressRpcImpl>(); TProcessor processor = factory.GetProcessor(null); TSimpleServer server = new TSimpleServer(multiplexedProcessor, transport, info => { Console.WriteLine(info); }); Console.WriteLine("Begin service..."); server.Serve(); } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadLine(); }
public static bool Execute(string[] args) { try { bool useBufferedSockets = false, useFramed = false, useEncryption = false, compact = false, json = false; int port = 9090; string pipe = null; string certPath = ""; var dic = Common.GetArgs(args); if (dic.ContainsKey("pipe")) { pipe = dic["pipe"]; } if (dic.ContainsKey("port")) { port = int.Parse(dic["port"]); } if (dic.ContainsKey("b") || dic.ContainsKey("buffered")) { useBufferedSockets = true; } if (dic.ContainsKey("f") || dic.ContainsKey("framed")) { useFramed = true; } if (dic.ContainsKey("protocol")) { compact = dic["protocol"] == "compact"; json = dic["protocol"] == "json"; } if (dic.ContainsKey("ssl")) { useEncryption = true; } if (dic.ContainsKey("cert")) { certPath = dic["cert"]; } // Processor DemoServiceImpl demoService = new DemoServiceImpl(); RPCDemoService.Processor demoProcessor = new RPCDemoService.Processor(demoService); // Transport TServerTransport trans; if (pipe != null) { trans = new TNamedPipeServerTransport(pipe); } else { if (useEncryption) { trans = new TTLSServerSocket(port, 0, useBufferedSockets, new X509Certificate2(certPath)); } else { trans = new TServerSocket(port, 0, useBufferedSockets); } } TProtocolFactory proto; if (compact) { proto = new TCompactProtocol.Factory(); } else if (json) { proto = new TJSONProtocol.Factory(); } else { proto = new TBinaryProtocol.Factory(); } // Simple Server TServer serverEngine; if (useFramed) { serverEngine = new ThriftServer(demoProcessor, trans, new TFramedTransport.Factory(), proto); } else { serverEngine = new ThriftServer(demoProcessor, trans, new TTransportFactory(), proto); } //Server event handler TradeServiceEventHandler serverEvents = new TradeServiceEventHandler(); serverEngine.setEventHandler(serverEvents); // Run it string where = (pipe != null ? "on pipe " + pipe : "on port " + port); Console.WriteLine("Starting the server " + where + (useBufferedSockets ? " with buffered socket" : "") + (useFramed ? " with framed transport" : "") + (useEncryption ? " with encryption" : "") + (compact ? " with compact protocol" : "") + (json ? " with json protocol" : "") + "..."); Console.WriteLine("Thrift Service started. Press Ctrl+C to shut down."); (serverEngine as IServer).Start(); } catch (Exception x) { Console.Error.Write(x); return(false); } Console.WriteLine("done."); return(true); }