Service Fabric 指定结构 url
Service Fabric specify fabric url
是否可以定义服务使用哪个 url 而不是标准 fabric:/AppName/ServiceName
?
我找不到这是否可以在应用程序级别配置。
您可以从这里使用此 uri 生成器 (ServiceUriBuilder.cs) class: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);
是,您可以将 ApplicationManifest.xml
中的服务名称更改为不同于服务 class 名称的名称。
简而言之: 只需将 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
是否可以定义服务使用哪个 url 而不是标准 fabric:/AppName/ServiceName
?
我找不到这是否可以在应用程序级别配置。
您可以从这里使用此 uri 生成器 (ServiceUriBuilder.cs) class: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);
是,您可以将 ApplicationManifest.xml
中的服务名称更改为不同于服务 class 名称的名称。
简而言之: 只需将 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