服务结构:ServiceManifest.xml中允许使用多种服务类型 [英] Service Fabric: are multiple service types allowed in ServiceManifest.xml

查看:66
本文介绍了服务结构:ServiceManifest.xml中允许使用多种服务类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在单个exe(ServiceRuntime.RegisterServiceAsync)中注册多个服务结构服务.支持这个吗?如果是这样,我将如何配置它们?

I'm trying to register multiple service-fabric services within a single exe (ServiceRuntime.RegisterServiceAsync). Is this supported? If so, how would I configure them?

例如:ServiceManifest.xml在ServiceTypes中支持多个StatelessServiceType元素:

Eg: ServiceManifest.xml supports multiple StatelessServiceType elements within ServiceTypes:

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="EchoGatewayPkg"
                 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>
    <StatelessServiceType ServiceTypeName="Service1Type" >
    </StatelessServiceType>

    <StatelessServiceType ServiceTypeName="Service2Type" >
    </StatelessServiceType>
  </ServiceTypes>
...

和ApplicationManifest.xml在DefaultServices/Service中不支持多个StatelessService元素:

and ApplicationManifest.xml does not support multiple StatelessService elements within DefaultServices/Service:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="EchoServiceType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="Service1_InstanceCount" DefaultValue="1" />
    <Parameter Name="Service2_InstanceCount" DefaultValue="1" />
  </Parameters>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides />
  </ServiceManifestImport>
  <DefaultServices>
    <Service Name="MyService1">
      <StatelessService ServiceTypeName="Service1Type" InstanceCount="[Service1_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
    <Service Name="MyService2">
      <StatelessService ServiceTypeName="Service2Type" InstanceCount="[Service2_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
</ApplicationManifest>

因此,这实际上产生了2个进程,并且每个进程的激活上下文都列出了两种默认服务类型(在这种配置下,我希望只有一种).

Hence, this effectively spawns 2 processes, and each process' activation-context has both default service types listed (I would have expected only one with this configuration).

欢迎提出任何建议(关于如何在单个exe中配置多种服务类型)或进行澄清.

Any suggestions (on how to configure multiple service types within a single exe) or clarifications are welcome.

推荐答案

暂时忽略默认服务.

,这是您获取多种服务类型以共享主机进程的方式.您将看到2个进程,因为Service Fabric将这两个服务实例放置在不同的节点上,并且每个实例都需要一个主机进程.尝试使用InstanceCount =-1"创建每个服务,以查看两种类型共享主机进程.

Yes, this is how you get multiple service types to share a host process. You're seeing 2 processes because Service Fabric is placing those two service instances on different nodes and each one needs a host process. Try creating each service with InstanceCount="-1" to see the two types share host processes.

好,返回默认服务.这本质上是令人困惑的,因为应用程序清单和服务清单仅用于描述 type 信息,而不是 instance 信息.但是,为了在创建应用程序实例时默认情况下创建服务实例,您必须声明性地指定实例及其参数.因此,您在示例中的默认服务下看到的是XML等效项:

OK, back to default services. This is inherently confusing because application manifest and service manifest are only meant to describe type information, not instance information. But in order to create service instances by default when an application instance is created, you have to specify, declaratively, the instances and their parameters. So what you see under default services there in your example is the XML equivalent of:

PS > New-ServiceFabricService -Stateless -PartitionSchemeSingleton -InstanceCount 1 -ApplicationName fabric:/EchoService -ServiceName fabric:/EchoService/MyService1 -ServiceTypeName Service1Type

PS > New-ServiceFabricService -Stateless -PartitionSchemeSingleton -InstanceCount 1 -ApplicationName fabric:/EchoService -ServiceName fabric:/EchoService/MyService2 -ServiceTypeName Service2Type

因此,在Service元素中包含多个StatelessService元素是没有意义的,因为您要定义要创建的服务 instances 以及服务类型和版本服务实例.

So it doesn't make sense to have more than one StatelessService element in the Service element, because you're defining the service instances you want to create, along with the type and version of the service instance.

为了后代,这是您发布的服务清单所附带的注册码:

And just for posterity, this is the registration code that goes with the service manifest you posted:

internal static class Program
{
    private static void Main()
    {
        try
        {
            ServiceRuntime.RegisterServiceAsync("Service1Type",
                context => new Stateless1(context)).GetAwaiter().GetResult();

            ServiceRuntime.RegisterServiceAsync("Service2Type",
                context => new Stateless2(context)).GetAwaiter().GetResult();

            Thread.Sleep(Timeout.Infinite);
        }
        catch (Exception e)
        {
            ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
            throw;
        }
    }
}
}

这篇关于服务结构:ServiceManifest.xml中允许使用多种服务类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆