Fork me on GitHub

SuperSocket 1.5 Documentation译文 19 ----- 在Windows Azure中运行SuperSocket

在Windows Azure中运行SuperSocket

Windows Azure是什么?

Windows Azure是微软的云计算平台!微软的Windows Azure提供按需计算能力和存储能力,由它的数据中心管理互联网上的应用开发。

在Windows Azure上运行的应用程序,具有很高的可靠性和可扩展性。基于SuperSocket的服务器程序可以很容易地运行在Windows Azure平台。

SuperSocket配置

SuperSocket与独立应用程序配置文件(app.config)中,它与用于Windows Azure的主机是相同的。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="superSocket" type="SuperSocket.SocketEngine.Configuration.SocketServiceConfig, SuperSocket.SocketEngine"/>
  </configSections>
  <superSocket>
    <servers>
      <server name="RemoteProcessServer"
          serverTypeName="remoteProcess"
          ip="Any" port="2012" />
    </servers>
    <serverTypes>
      <add name="remoteProcess"
       type="SuperSocket.QuickStart.RemoteProcessService.RemoteProcessServer, SuperSocket.QuickStart.RemoteProcessService" />
    </serverTypes>
  </superSocket>
  <system.diagnostics>
    <trace>
      <listeners>
        <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="AzureDiagnostics">
        </add>
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

在WorkRole项目中添加SuperSocket启动代码

与其他正常SuperSocket应用相同,启动的代码应该写在Windows Azure WorkRole项目应用程序入口点的OnStart()方法中:

public override bool OnStart()
{
    // 设置最大并发连接数
    ServicePointManager.DefaultConnectionLimit = 100;

    // 处理配置更改的信息
    // 请参阅MSDN主题 http://go.microsoft.com/fwlink/?LinkId=166357.

    m_Bootstrap = BootstrapFactory.CreateBootstrap();

    if (!m_Bootstrap.Initialize())
    {
        Trace.WriteLine("Failed to initialize SuperSocket!", "Error");
        return false;
    }

    var result = m_Bootstrap.Start();

    switch (result)
    {
        case (StartResult.None):
            Trace.WriteLine("No server is configured, please check you configuration!");
            return false;

        case (StartResult.Success):
            Trace.WriteLine("The server has been started!");
            break;

        case (StartResult.Failed):
            Trace.WriteLine("Failed to start SuperSocket server! Please check error log for more information!");
            return false;

        case (StartResult.PartialSuccess):
            Trace.WriteLine("Some server instances were started successfully, but the others failed to start! Please check error log for more information!");
            break;
    }

    return base.OnStart();
}

 

配置输入终结点,然后使用它

由于Windows Azure的内部网络的信息基础设施,你无法直接帧听你配置的IP /port。在这种情况下,你需要在Windows Azure项目输入端点配置:

endpoint

终结点的命名规则是“AppServerName_ConfiguredListenPort”。例如,我们有一个服务器命名为“Remote Process Server”配置如下图:

<server name="RemoteProcessServer"
          serverTypeName="remoteProcess"
          ip="Any" port="2012" />

然后,我们要创建一个终结点的名称“RemoteProcessServer_2012”。

在代码中,我们可以得到输入端点真正​​的编程端口:

var instanceEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[serverConfig.Name + "Endpoint"].Port;

但您并不需要自己做,因为SuperSocket内替换侦听端口已实现。你应该做的是通过在输入端点字典初始化引导时,最终的代码看起来应该像下面的代码:

public override bool OnStart()
{
    // Set the maximum number of concurrent connections 
    ServicePointManager.DefaultConnectionLimit = 100;

    // For information on handling configuration changes
    // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

    m_Bootstrap = BootstrapFactory.CreateBootstrap();

    if (!m_Bootstrap.Initialize(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints.ToDictionary(p => p.Key, p => p.Value.IPEndpoint)))
    {
        Trace.WriteLine("Failed to initialize SuperSocket!", "Error");
        return false;
    }

    var result = m_Bootstrap.Start();

    switch (result)
    {
        case (StartResult.None):
            Trace.WriteLine("No server is configured, please check you configuration!");
            return false;

        case (StartResult.Success):
            Trace.WriteLine("The server has been started!");
            break;

        case (StartResult.Failed):
            Trace.WriteLine("Failed to start SuperSocket server! Please check error log for more information!");
            return false;

        case (StartResult.PartialSuccess):
            Trace.WriteLine("Some server instances were started successfully, but the others failed to start! Please check error log for more information!");
            break;
    }

    return base.OnStart();
}

最后,您可以尝试启动Windows Azure实例。

posted @ 2013-05-12 21:42  逗豆豆  阅读(340)  评论(0编辑  收藏  举报