【问题标题】:How to configure a remote actor configuration in an Azure worker role (AKKA.NET)如何在 Azure 辅助角色中配置远程参与者配置 (AKKA.NET)
【发布时间】:2017-10-13 15:29:07
【问题描述】:

过去 2 天我一直在苦苦挣扎,试图弄清楚如何将基本控制台应用程序部署到 Azure 辅助角色中,并使其可以从某种基于客户端的应用程序远程访问,例如MVC 网络应用程序。下面回答

【问题讨论】:

    标签: c# azure azure-worker-roles akka.net


    【解决方案1】:

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

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

    选择工人角色

    我的 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 设置。

    部署 Worker 角色后,您应该能够通过其 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 应用程序服务)。如果我能以任何方式改进答案,请告诉我。

    【讨论】:

      猜你喜欢
      • 2015-04-12
      • 2012-11-29
      • 1970-01-01
      • 2014-09-17
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2012-08-05
      相关资源
      最近更新 更多