zoukankan      html  css  js  c++  java
  • SuperSocket 2.0学习08:多服务器实例

    官方学习资料:多服务器实例

    本文开发环境:Win10 + VS2019 + .NetCore 3.1 + SuperSocket 2.0.0-beta.8。

    Gitee:SuperSocketV2Sample

    创建多服务器实例,首先需要定义多服务器配置,在serverOptions节点定义多个服务器的完整参数。其次是使用MultipleServerHostBuilder类的AddServer方法添加不同的服务器,AddServer方法的泛型参数需要完整的SuperSocketService类型、包类型以及命令协议类型。

    完整的项目代码请参考Gitee项目源码。

    需要注意的是,多服务器实例在.NET Core中是使用一个进程承载的,每个实例以HostedService的形式在进程(Host)中运行。如果要实现多进程或多服务器运行,需要使用Docker等容器技术。

    1、创建项目

    使用VS2019创建.NET Core控制台程序,选择.Net Core 3.1,通过NuGet引入SuperSocket(2.0.0-beta.8)。

    2、添加配置文件

    在项目根目录添加appsettings.json配置文件,并设置其文件属性为“如果较新则复制”。

    3、配置文件

    {
      "serverOptions": {
        "TelnetServerA": {
          "name": "TelnetServiceA",
          "listeners": [
            {
              "ip": "Any",
              "port": "4040"
            }
          ]
        },
        "TelnetServerB": {
          "name": "TelnetServiceB",
          "listeners": [
            {
              "ip": "Any",
              "port": "4041"
            }
          ]
        }
      }
    }

    4、测试代码

    using System;
    using System.Threading.Tasks;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using SuperSocket;
    using SuperSocket.Command;
    using SuperSocket.ProtoBase;
    using SuperSocket.Server;
    using SuperSocketV2Sample.MultipleServer.Commands;
    using SuperSocketV2Sample.MultipleServer.Server;
    
    namespace SuperSocketV2Sample.MultipleServer
    {
        class Program
        {
            static async Task Main()
            {
                var host = MultipleServerHostBuilder.Create()
                    .AddServer<TelnetServiceA<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(builder =>
                    {
                        builder.ConfigureServerOptions((ctx, config) =>
                            {
                                //获取服务配置
                                // ReSharper disable once ConvertToLambdaExpression
                                return config.GetSection("TelnetServerA");
                            })
                            .UseSession<TelnetSessionA>()
                            .UseCommand(commandOptions =>
                            {
                                commandOptions.AddCommand<AddCommand>();
                                commandOptions.AddCommand<DivCommand>();
                                commandOptions.AddCommand<EchoCommand>();
                            });
                    })
                    .AddServer<TelnetServiceB<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(builder =>
                    {
                        builder.ConfigureServerOptions((ctx, config) =>
                            {
                                //获取服务配置
                                // ReSharper disable once ConvertToLambdaExpression
                                return config.GetSection("TelnetServerB");
                            })
                            .UseSession<TelnetSessionB>()
                            .UseCommand(commandOptions =>
                            {
                                commandOptions.AddCommand<MulCommand>();
                                commandOptions.AddCommand<SubCommand>();
                                commandOptions.AddCommand<EchoCommand>();
                            });
                    })
                    //配置日志
                    .ConfigureLogging((hostCtx, loggingBuilder) =>
                    {
                        //添加控制台输出
                        loggingBuilder.AddConsole();
                    })
                    .Build();
                try
                {
                    await host.RunAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
    }
  • 相关阅读:
    [GO] go使用etcd和watch方法进行实时的配置变更
    [GO]go context的deadline方法
    [GO]go使用contextCancel
    [GO]go使用etcd
    js控制复选框checkbox 只能单选
    JQuery.Ajax之错误调试帮助信息
    SQLServer2005创建定时作业任务
    JS/JQuery针对不同类型元素的操作(radio、select、checkbox)
    SQL Server跨库查询
    javax.net.ssl.SSLHandshakeException(Cas导入证书)
  • 原文地址:https://www.cnblogs.com/xhubobo/p/14627545.html
Copyright © 2011-2022 走看看