在过去的两天里,我一直在努力弄清楚如何确切地将基本控制台应用程序部署到Azure辅助角色中以及如何从某种基于客户端的应用程序(例如, MVC网络应用。在下面回答

最佳答案

我首先将我的Actor状态包含在与MVC应用程序进行通信的本地控制台应用程序中。我希望将应用程序与Azure生态系统一起部署,并认为将状态保持在工作角色中,并在应用程序服务中托管“客户端” MVC应用程序是最好的方法。

确保您的Actor系统已从解决方案中解压缩到它自己的项目中。在您的解决方案中创建一个新的Worker角色CloudService项目。

c# - 如何在Azure辅助角色(AKKA.NET)中配置远程角色配置-LMLPHP
c# - 如何在Azure辅助角色(AKKA.NET)中配置远程角色配置-LMLPHP
选择工作人员角色

我将WorkRole配置如下:

public class WorkerRole : RoleEntryPoint
{
    private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
    private readonly ManualResetEvent runCompleteEvent = new ManualResetEvent(false);
    private static ActorSystem ActorSystemInstance;
    public override void Run()
    {
        Trace.TraceInformation("Game.State.WorkerRole is running");

        try
        {
            this.RunAsync(this.cancellationTokenSource.Token).Wait();
        }
        finally
        {
            this.runCompleteEvent.Set();
        }
    }

    public override bool OnStart()
    {
        ActorSystemInstance = ActorSystem.Create("GameSystem");
        // Set the maximum number of concurrent connections
        ServicePointManager.DefaultConnectionLimit = 12;

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

        bool result = base.OnStart();

        Trace.TraceInformation("Game.State.WorkerRole has been started");

        return result;
    }

    public override void OnStop()
    {
        ActorSystemInstance.Terminate();
        Trace.TraceInformation("Game.State.WorkerRole is stopping");

        this.cancellationTokenSource.Cancel();
        this.runCompleteEvent.WaitOne();

        base.OnStop();

        Trace.TraceInformation("Game.State.WorkerRole has stopped");
    }

    private async Task RunAsync(CancellationToken cancellationToken)
    {
        var gameController = ActorSystemInstance.ActorOf<GameControllerActor>("GameController");

        while (!cancellationToken.IsCancellationRequested)
        {
            Trace.TraceInformation("Working");
            await Task.Delay(1000);
        }
    }
}


和我的HOCON文件(在app.config中)如下:

<akka>
<hocon>
  <![CDATA[
  akka {
    loglevel = DEBUG

    actor {
      provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"
      debug {
        receive = on
        autoreceive = on
        lifecycle = on
        event-stream = on
        unhandled = on
      }
    }


    remote {
      helios.tcp {
        transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
        transport-protocol = tcp
        enforce-ip-family = true
        port = xxxx //the port configured in your worker role endpoint
        hostname = "0.0.0.0" //This is the local hostname of the worker role, using 0.0.0.0 will set the Remote actor to "listen" on all available DNS/IP addresses including the loopback (127.0.0.1)
        pulic-hostname = "xx.xx.xx.xx" //IP Address OR DNS name, but whatever is set here is what MUST be used in the Actor Selector path on the client in order for proper "association" to occur. I did find that DNS name was required for my application as I was using SignalR as a bridge between the Actor system and the web client.

      }
    }
  }
  ]]>
</hocon>




我们需要在辅助角色配置中定义端点,以便我们“公开”一个端口,通过该端口我们可以与外界进行通信。因此,转到您的WorkerRole设置。

c# - 如何在Azure辅助角色(AKKA.NET)中配置远程角色配置-LMLPHP

部署工作角色后,您应该能够通过端口的IP和您先前配置的端口远程登录到服务器,从而确认该端口已打开并可用。

从客户端设置ActorSelection的最重要部分是,下面的IP / DNS地址必须与工作角色中HOCON配置中public-hostname设置中设置的IP / DNS相匹配。

 ActorReferences.GameController =
            ActorSystem.ActorSelection("akka.tcp://GameSystem@xx.xxx.xxx.xxx:8091/user/GameController")
                .ResolveOne(TimeSpan.FromSeconds(3))
                .Result;


为了完整起见,这是我的客户端HOCON配置:

akka {
    loglevel = OFF

    actor {
      provider = "Akka.Remote.RemoteActorRefProvider, Akka.Remote"
      debug {
        receive = on
        autoreceive = on
        lifecycle = on
        event-stream = on
        unhandled = on
      }
    }


    remote {
      helios.tcp {
        transport-class = "Akka.Remote.Transport.Helios.HeliosTcpTransport, Akka.Remote"
        transport-protocol = tcp
        enforce-ip-family = true
        port = 0 //A port will be provided for us... not important as we won't be calling into the client externally
        public-hostname = "yoursitename.azurewebsites.net" //Remember this is simply the DNS name of the client machine
        hostname = "127.0.0.1"
      }
    }
  }


我真的希望这可以帮助其他人。我确实没有找到太多文档描述Akka.NET到Azure的部署(没有将Actor系统部署到易失的IIS应用程序服务)。让我知道我是否可以以任何方式改善答案。

关于c# - 如何在Azure辅助角色(AKKA.NET)中配置远程角色配置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46733286/

10-14 12:02
查看更多