Ejemplo n.º 1
0
        public proxyNettyServer(string cid, string mapHttpPort, string _ServiceUrl, string mapHttpsPort = null, int failtime = 100, int _maxPerfData = 100) : base(cid, failtime, _maxPerfData)
        {
            this.port      = mapHttpPort;  //http转发服务的端口,不是集群服务api的端口
            this.httpsPort = mapHttpsPort; //同上

            mapPortGroup_dic = new Dictionary <string, mapPortGroup>(StringComparer.OrdinalIgnoreCase);
            maphttpGroup_dic = new Dictionary <string, mapPortGroup>(StringComparer.OrdinalIgnoreCase);
            checkGroup       = new MultithreadEventLoopGroup();
            httpClient       = new HttpHelper(new TimeSpan(0, 0, 3));


            if (!string.IsNullOrEmpty(cid)) //如果cid==null,非本地服务,不需要启动dotnett服务
            {
                bossGroup       = new MultithreadEventLoopGroup(1);
                workerGroup     = new MultithreadEventLoopGroup();
                SERVER_HANDLER  = new ProxyServerHandler();
                portbootstrap   = new ServerBootstrap();
                httpbossGroup   = new MultithreadEventLoopGroup(1);
                httpworkerGroup = new MultithreadEventLoopGroup();
                httpbootstrap   = new ServerBootstrap();
            }
            this.serviceUrl       = _ServiceUrl;
            this.id               = (cid + this.serviceUrl).ToMD5(); //保证在同一个zone中,同一个服务url肯定的是同样的id;
            this.needReportChange = true;
        }
Ejemplo n.º 2
0
        static async Task RunServerAsync()
        {
            commSetting.SetConsoleLogger();
            var bossGroup   = new MultithreadEventLoopGroup(1);
            var workerGroup = new MultithreadEventLoopGroup();


            var SERVER_HANDLER = new ProxyServerHandler();

            X509Certificate2 tlsCertificate = null;

            if (ServerSettings.IsSsl)
            {
                tlsCertificate = new X509Certificate2(Path.Combine(commSetting.ProcessDirectory, "dotnetty.com.pfx"), "password");
            }
            try
            {
                var bootstrap = new ServerBootstrap();
                bootstrap
                .Group(bossGroup, workerGroup)
                .Channel <CustTcpServerSocketChannel>()
                .Option(ChannelOption.SoBacklog, 100)
                .Handler(new LoggingHandler(LogLevel.INFO))
                .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    if (tlsCertificate != null)
                    {
                        pipeline.AddLast(TlsHandler.Server(tlsCertificate));
                    }

                    pipeline.AddLast(new LengthFieldBasedFrameDecoder(commSetting.MAX_FRAME_LENGTH, commSetting.LENGTH_FIELD_OFFSET, commSetting.LENGTH_FIELD_LENGTH, commSetting.LENGTH_ADJUSTMENT, commSetting.INITIAL_BYTES_TO_STRIP, false));
                    pipeline.AddLast(SERVER_HANDLER);
                }));

                IChannel bootstrapChannel = await bootstrap.BindAsync(ServerSettings.Port);

                Console.ReadLine();

                await bootstrapChannel.CloseAsync();
            }
            finally
            {
                Task.WaitAll(bossGroup.ShutdownGracefullyAsync(), workerGroup.ShutdownGracefullyAsync());
            }
        }