繁体   English   中英

如何在Azure Service Fabric中迁移Windows Service

[英]How To migrate windows service in Azure service fabric

我想使用Service Fabric将以.net编写的典型Windows服务迁移到Azure。

为实现此目的,我正在创建一个服务结构应用程序,其中包含一个作为来宾可执行文件的微服务,该服务使用Windows服务的.exe,并将应用程序包部署到服务结构群集。

在群集上部署Service Fabric应用程序后,我希望Windows Service应该在所有节点上自动安装并启动,但是应用程序随时都可以在任何单个节点上运行。 我希望Windows服务一次只能在一个节点上运行。

请帮助实现这一目标。

您当然可以将其服务作为来宾可执行文件运行。 通过将清单中的实例计数设置为1,可以确保仅在一个节点上运行,如下所示:

  <Parameters>
    <Parameter Name="GuestService_InstanceCount" DefaultValue="-1" />
  </Parameters>
  ...
  <DefaultServices>
    <Service Name="GuestService">
      <StatelessService ServiceTypeName="GuestServiceType" 
                        InstanceCount="[GuestService_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>

或者,您实际上可以迁移它,而不仅仅是在SF环境中重新托管它...

如果您的Windows服务是用.NET编写的,而您又不想从Service Fabric中受益,那么将代码从Windows服务迁移到Service Fabric中的可靠服务的工作就不会太大。

基本服务示例:

如果首先在Service Fabric应用程序中创建无状态服务,那么最终将获得如下服务实现(已删除注释):

 internal sealed class MigratedService : StatelessService { public MigratedService(StatelessServiceContext context) : base(context) { } protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() { return new ServiceInstanceListener[0]; } protected override async Task RunAsync(CancellationToken cancellationToken) { // TODO: Replace the following sample code with your own logic // or remove this RunAsync override if it's not needed in your service. long iterations = 0; while (true) { cancellationToken.ThrowIfCancellationRequested(); ServiceEventSource.Current.ServiceMessage(this.Context, "Working-{0}", ++iterations); await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); } } 

服务启动并在群集中的节点上运行后, RunAsync方法即开始运行。 它会继续运行,直到群集出于某种原因决定停止该服务或将其移至另一个节点。

在Windows服务代码中,您应该有一个在启动时运行的方法。 通常,您可以在其中设置一个Timer或类似的东西来连续开始做某事:

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    while (true)
    {
        cancellationToken.ThrowIfCancellationRequested();
        DoServiceStuff();
        ServiceEventSource.Current.ServiceMessage(this.Context, 
            "Reliable Service says hello");
        await Task.Delay(TimeSpan.FromSeconds(60), cancellationToken);
    }
}

现在,在OnTimer获取该代码并将其放入您的RunAsync方法(以及您需要的任何其他代码)中:

 protected override async Task RunAsync(CancellationToken cancellationToken) { while (true) { cancellationToken.ThrowIfCancellationRequested(); DoServiceStuff(); ServiceEventSource.Current.ServiceMessage(this.Context, "Reliable Service says hello"); await Task.Delay(TimeSpan.FromSeconds(60), cancellationToken); } } 

注意Task.Delay(...) ,应将其设置为与Windows服务的Timer间隔相同。

现在,如果您已经登录Windows服务并且使用了ETW,那么应该可以立即使用。 您只需要设置某种方式立即从Azure查看这些日志,例如使用Log Analytics( https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-service-fabric )。

您可能还需要迁移的其他事情是,如果在关闭,继续时运行特定的代码,并且在启动时有任何参数发送给服务(例如,数据库的连接字符串)。 那些需要转换为服务的配置设置,请参阅SO 33928204作为起点。

服务结构背后的想法是,它可以从部署和运行中管理您的服务。 将服务/应用程序部署到服务结构实例后,就像运行Windows服务(kinda)一样,因此您无需安装Windows服务。 如果您使用的是TopShelf之类的工具,则只需运行exe,一切就可以在服务结构中正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM