Service Fabric Cluster Manager

作者:潘罡 (Van Pan)@ Microsoft

 

我们回到Service Fabric最底层的话题,谈谈Service Fabric是怎么工作的。

 

首先,我们回到下面的文档,看看Service Fabric的整体架构

https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-architecture

《Service Fabric Cluster Manager》

 

文档中有一段对于CM的定义:CM和Failover Manager进行交互,主要负责可靠性和服务运行位置分配。Resource Manager保证约束不被打破,CM管理Application的整个生命周期。同时它还和Health Manger进行交互并确保application在升级过程中在语义层面不会出现可用性问题。

 

CM工作流程

我们回到一个场景,SF管理员更新一个Application。他会使用下面的步骤

https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-automate-powershell

按步解释,他需要对SF进行如下操作:

1. 上传SF Application的package

2. 注册Application type

3. 命令SF升级

 

从顺序而言,SF的各个系统服务是这样工作的

1. 上传SF package,SF Image Store Service会将package储存至本地

2. 注册Application type,命令发至CM,CM进行初始化处理

3. 升级SF第一步,创建Application。CM会和Naming Service通讯并告知新的type以及版本号的一系列信息。

4. 升级SF第二步,创建Service。CM同样会先和Naming Service通讯。随后Naming Service会和FM通讯并告知服务信息,FM根据算法统计Service需要跑在哪些Node上,随后FM通知Node进行Service部署。Node根据需要部署的Service信息,从Image Store中获取Service exe文件等运行数据,并且启动服务。

5. 如果是升级Application操作,CM同样会做初始化操作。随后CM直接和FM通讯升级操作,FM随后将升级Service的工作部署到各个Node。

 

CM配置信息解析

CM是如何读取Application和Service的详细信息的呢?

它是读取ServiceManifest.xml和ApplicationManifest.xml

 

你可以在本地源代码中找到这两个文件

《Service Fabric Cluster Manager》

 

 

我们先看ServiceManifest.xml,默认情况下它是这样子的

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="Stateless1Pkg"
                 Version="1.0.0"
                 xmlns="http://schemas.microsoft.com/2011/01/fabric"
                 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ServiceTypes>
    <!-- This is the name of your ServiceType. 
         This name must match the string used in RegisterServiceType call in Program.cs. -->
    <StatelessServiceType ServiceTypeName="Stateless1Type" />
  </ServiceTypes>

  <!-- Code package is your service executable. -->
  <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
      <ExeHost>
        <Program>Stateless1.exe</Program>
      </ExeHost>
    </EntryPoint>
  </CodePackage>

  <!-- Config package is the contents of the Config directoy under PackageRoot that contains an 
       independently-updateable and versioned set of custom configuration settings for your service. -->
  <ConfigPackage Name="Config" Version="1.0.0" />

  <Resources>
    <Endpoints>
      <!-- This endpoint is used by the communication listener to obtain the port on which to 
           listen. Please note that if your service is partitioned, this port is shared with 
           replicas of different partitions that are placed in your code. -->
      <Endpoint Name="ServiceEndpoint" />
    </Endpoints>
  </Resources>
</ServiceManifest>

请注意以下描述:

在ServiceTypes中,其实你可以添加多个ServiceType。也就是说,你完全可以在同一个Service的源代码中定义多个Micro-Service。

CodePackage定义了以上Service的Exe入口,你同样可以添加多个EXE。

 

在Program.cs中,定义了如何将Exe和ServiceType进行连接。

ServiceRuntime.RegisterServiceAsync("Stateless1Type",
                    context => new Stateless1(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(Stateless1).Name);

换言之,我们完全可以在同一个项目中定义多种Service并让他们Host在同一个EXE中。

 

再回到ApplicationManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="Application1Type"
                     ApplicationTypeVersion="1.0.0"
                     xmlns="http://schemas.microsoft.com/2011/01/fabric"
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Parameters>
    <Parameter Name="Stateless1_InstanceCount" DefaultValue="-1" />
  </Parameters>
  <!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion 
       should match the Name and Version attributes of the ServiceManifest element defined in the 
       ServiceManifest.xml file. -->
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="Stateless1Pkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides />
  </ServiceManifestImport>
  <DefaultServices>
    <!-- The section below creates instances of service types, when an instance of this 
         application type is created. You can also create one or more instances of service type using the 
         ServiceFabric PowerShell module.
         
         The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
    <Service Name="Stateless1">
      <StatelessService ServiceTypeName="Stateless1Type" InstanceCount="[Stateless1_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
</ApplicationManifest>

CM通过ServiceManifestName和ServiceTypeName找到Service的定义。

    原文作者:PackageManagerService
    原文地址: http://www.cnblogs.com/mschiefevangelist/p/6599970.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞