【问题标题】:Service Fabric specify fabric urlService Fabric 指定结构 url
【发布时间】:2017-02-09 15:41:30
【问题描述】:

是否可以定义服务使用哪个 url 而不是标准的fabric:/AppName/ServiceName?

我无法找到这是否可以在应用程序级别进行配置。

【问题讨论】:

    标签: azure-service-fabric


    【解决方案1】:

    是的,您可以将ApplicationManifest.xml 中的服务名称更改为从服务类名称中获取的名称以外的名称。

    简短:只需将该服务的ApplicationManifest.xml 中的name 属性更改为其他内容即可。

    在代码中:如果我有这项服务:

    public interface IJustAnotherStatelessService : IService
    {
        Task<string> SayHelloAsync(string someValue);
    }
    
    internal sealed class JustAnotherStatelessService : StatelessService, IJustAnotherStatelessService
    {
         // Service implementation
    }
    

    像这样在Program.cs注册:

    ServiceRuntime.RegisterServiceAsync("JustAnotherStatelessServiceType",
        context => new JustAnotherStatelessService(context)).GetAwaiter().GetResult();
    

    在ServiceManifest.xml 中用于该服务

    <?xml version="1.0" encoding="utf-8"?>
    <ServiceManifest ...>
      <ServiceTypes>
        <!-- This is the name of your ServiceType. 
             This name must match the string used in RegisterServiceType call in Program.cs. -->
        <StatelessServiceType ServiceTypeName="JustAnotherStatelessServiceType" />
      </ServiceTypes>
     ...
    

    在ApplicationManifest.xml 中,您将获得建议的名称:

    <ApplicationManifest ...>
      <DefaultServices>
        <Service Name="JustAnotherStatelessService">
          <StatelessService ServiceTypeName="JustAnotherStatelessServiceType" InstanceCount="[JustAnotherStatelessService_InstanceCount]">
            <SingletonPartition />
          </StatelessService>
        </Service>
      </DefaultServices>
    </ApplicationManifest>
    

    这将为您提供一个 Uri 服务,例如

    fabric:/app_name/JustAnotherStatelessService
    

    现在,继续更改应用程序清单中的名称:

    <ApplicationManifest ...>
      <DefaultServices>
        <Service Name="AwesomeService">
          <StatelessService ServiceTypeName="JustAnotherStatelessServiceType" InstanceCount="[JustAnotherStatelessService_InstanceCount]">
            <SingletonPartition />
          </StatelessService>
        </Service>
      </DefaultServices>
    </ApplicationManifest>
    

    你的服务现在回答

    fabric:/app_name/AwesomeService
    

    【讨论】:

    • 感谢您的回复,我相信我并不清楚我的要求。我想知道您是否可以让应用程序监听fabric:/foobarbazservice,但我认为这是不可能的,因为服务解析器会在应用程序类型中查找服务类型,并且不能随机选择一个!跨度>
    • 不,在这部分你是对的,你仅限于 url 的 {applicationName}/{serviceName_set_in_ApplicationManifest} 方案。考虑到织物传输在内部构建 URL 的方式,我看不到任何设置完全自定义 URL 的方法。
    【解决方案2】:

    您可以从这里使用这个 uri builder (ServiceUriBuilder.cs) 类:https://github.com/Azure-Samples/service-fabric-dotnet-web-reference-app/blob/master/ReferenceApp/Common/ServiceUriBuilder.cs

    对于无状态服务,您可以轻松获取代理:

    var serviceUri = new ServiceUriBuilder(ServiceName);
    var proxyFactory = new ServiceProxyFactory();
    var svc = proxyFactory.CreateServiceProxy<IServiceName>(serviceUri.ToUri());
    

    对于有状态服务,您必须指定分区。

    var serviceUri = new ServiceUriBuilder(StatefulServiceName);
    var proxyFactory = new ServiceProxyFactory();
    //this is just a sample of partition 1 if you are using number partitioning. 
    var partition = new ServicePartitionKey(1);
    var svc = proxyFactory.CreateServiceProxy<IStatefulServiceName>(serviceUri.ToUri(), partition);
    

    【讨论】:

      猜你喜欢
      • 2019-09-21
      • 2019-07-24
      • 2016-03-06
      • 2019-05-20
      • 2019-11-30
      • 2016-10-15
      • 2018-03-06
      • 1970-01-01
      • 2016-07-23
      相关资源
      最近更新 更多