简体   繁体   English

如何在Azure Service Fabric中迁移Windows Service

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

I want to migrate typical windows service which is written in .net to Azure using Service fabric. 我想使用Service Fabric将以.net编写的典型Windows服务迁移到Azure。

To implement this , I am creating one service fabric application containing one micro service as guest executable which uses .exe of windows service and deploying application package to service fabric cluster. 为实现此目的,我正在创建一个服务结构应用程序,其中包含一个作为来宾可执行文件的微服务,该服务使用Windows服务的.exe,并将应用程序包部署到服务结构群集。

After deploying service fabric application on cluster I want windows service should install & start automatically on all nodes however at any time application is running on any single node. 在群集上部署Service Fabric应用程序后,我希望Windows Service应该在所有节点上自动安装并启动,但是应用程序随时都可以在任何单个节点上运行。 I want windows service should run on only one node at a time. 我希望Windows服务一次只能在一个节点上运行。

Please kindly help to implement this. 请帮助实现这一目标。

You can certainly run your service as a guest executable. 您当然可以将其服务作为来宾可执行文件运行。 Making sure it only runs on one node can be done by setting the instance count to 1 in the manifest, like so: 通过将清单中的实例计数设置为1,可以确保仅在一个节点上运行,如下所示:

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

Or, you could actually migrate it, not just re-host it in the SF environment... 或者,您实际上可以迁移它,而不仅仅是在SF环境中重新托管它...

If your Windows Service is written in .NET and the you wan't to benefit from Service Fabric then the job of migrating the code from a Windows Service to a Reliable Service in Service Fabric should not be to big. 如果您的Windows服务是用.NET编写的,而您又不想从Service Fabric中受益,那么将代码从Windows服务迁移到Service Fabric中的可靠服务的工作就不会太大。

Example for a basic service: 基本服务示例:

If you start by creating a Stateless Service in a Service Fabric application you end up with a service implementation that looks like (comments removed): 如果首先在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); } } 

The RunAsync method starts running as soon as the Service is up and running on a node in the cluster. 服务启动并在群集中的节点上运行后, RunAsync方法即开始运行。 It will continue to run until the cluster, for some reason, decides to stop the service, or move it to another node. 它会继续运行,直到群集出于某种原因决定停止该服务或将其移至另一个节点。

In your Windows Service code you should have a method that is run on start. 在Windows服务代码中,您应该有一个在启动时运行的方法。 This is usually where you set up a Timer or similar to start doing something on a continuous basis: 通常,您可以在其中设置一个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);
    }
}

Now grab that code in OnTimer and put it in your RunAsync method (and any other code you need): 现在,在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); } } 

Note the Task.Delay(...) , it should be set to the same interval as your Windows Service had for it's Timer . 注意Task.Delay(...) ,应将其设置为与Windows服务的Timer间隔相同。

Now, if you have logging in your Windows Service and you use ETW, then that should work out of the box for you. 现在,如果您已经登录Windows服务并且使用了ETW,那么应该可以立即使用。 You simply need to set up some way of looking at those logs from Azure now, for instance using Log Analytics ( https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-service-fabric ). 您只需要设置某种方式立即从Azure查看这些日志,例如使用Log Analytics( https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-service-fabric )。

Other things you might have to migrate is if you run specific code on shut down, on continue, and if you have any parameters sent to the service on startup (for instance connection strings to databases). 您可能还需要迁移的其他事情是,如果在关闭,继续时运行特定的代码,并且在启动时有任何参数发送给服务(例如,数据库的连接字符串)。 Those need to be converted to configuration settings for the service, look at SO 33928204 for a starting point for that. 那些需要转换为服务的配置设置,请参阅SO 33928204作为起点。

The idea behind service fabric is so that it manages your services, from deployment and running. 服务结构背后的想法是,它可以从部署和运行中管理您的服务。 Once you've deployed your service/application to the service fabric instance it will be just like running a windows service (kinda) so you wont need to install your windows service. 将服务/应用程序部署到服务结构实例后,就像运行Windows服务(kinda)一样,因此您无需安装Windows服务。 If you're using something like TopShelf you can just run the exe and everything will run totally fine within service fabric. 如果您使用的是TopShelf之类的工具,则只需运行exe,一切就可以在服务结构中正常运行。

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

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