Service Fabric - 集群中的 Web api,其唯一工作是提供来自可靠集合的数据
Service Fabric - A web api in cluster who' only job is to serve data from reliable collection
我是 Service Fabric 的新手,目前我正在努力寻找如何从 WEB API(即,也作为一个单独的应用程序存在于 Service fabric 集群中)。这个问题非常基本,我确信我遗漏了一些非常明显的东西。如果这听起来很蹩脚,请向社区道歉。
我有一个很大的 XML,我想通过 WEB API 端点公开其中的一部分作为各种查询的结果。搜索了类似的问题,但找不到合适的答案。
很高兴看到有经验的 SF 开发人员如何完成这样的任务。
编辑 我发布了我想出的解决方案
您可以将 WebAPI 与 OWIN 结合使用来设置通信侦听器并公开来自可靠集合的数据。请参阅 Build a web front end for your app for info on how to set that up. Take a look at the WordCount sample in the Getting started sample apps,它将一堆随机词提供给有状态服务并记录已处理的词数。希望对您有所帮助。
在阅读和观察其他问题和 Azure 示例后,我实施了一个解决方案。在这里发布我遇到的问题,希望能帮助其他刚接触 Azure Service Fabric 的开发人员(免责声明:我仍然是 Service Fabric 的新手,因此非常感谢评论和建议):
首先,非常简单 - 我最终在 Azure 服务结构应用程序中得到了有状态服务和 WEB Api 无状态服务:
DataStoreService
- 读取大型 XML 并将其存储到 Reliable 字典中的有状态服务(发生在 RunAsync 方法中)。
- Web Api 提供了一个
/api/query
端点,过滤掉存储在 rteliable 字典中的 XElements 集合,并将其序列化回请求者
3 个陷阱
1)如何从无状态服务中获取可靠的字典数据,即如何从无状态服务中获取有状态服务的实例:
ServiceUriBuilder builder = new ServiceUriBuilder("DataStoreService");
IDataStoreService DataStoreServiceClient = ServiceProxy.Create<IDataStoreService>(builder.ToUri(), new ServicePartitionKey("Your.Partition.Name"));
上面的代码已经给了你实例。即 - 您需要使用服务代理。为此,您需要:
定义有状态服务将实现的接口,并在调用 ServiceProxy 的 Create 方法时使用它(IDataStoreService
)
将正确的分区键传递给 Create 方法。 This article 很好地介绍了 Azure 服务总线分区
2) 注册副本监听器 - 为了避免错误说
The primary or stateless instance for the partition 'a67f7afa-3370-4e6f-ae7c-15188004bfa1' has invalid address, this means that right address from the replica/instance is not registered in the system
,您需要按照 中所述注册副本侦听器:
public DataStoreService(StatefulServiceContext context)
: base(context)
{
configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
}
3) Service fabric name spacing and referencing services - ServiceUriBuilder
class 我取自 service-fabric-dotnet-web-reference-app。基本上你需要一些东西来生成一个形式的 Uri:
new Uri("fabric:/" + this.ApplicationInstance + "/" + this.ServiceInstance);
,
其中 ServiceInstance
是您要获取其实例的服务的名称(在本例中为 DataStoreService
)
我是 Service Fabric 的新手,目前我正在努力寻找如何从 WEB API(即,也作为一个单独的应用程序存在于 Service fabric 集群中)。这个问题非常基本,我确信我遗漏了一些非常明显的东西。如果这听起来很蹩脚,请向社区道歉。
我有一个很大的 XML,我想通过 WEB API 端点公开其中的一部分作为各种查询的结果。搜索了类似的问题,但找不到合适的答案。
很高兴看到有经验的 SF 开发人员如何完成这样的任务。
编辑 我发布了我想出的解决方案
您可以将 WebAPI 与 OWIN 结合使用来设置通信侦听器并公开来自可靠集合的数据。请参阅 Build a web front end for your app for info on how to set that up. Take a look at the WordCount sample in the Getting started sample apps,它将一堆随机词提供给有状态服务并记录已处理的词数。希望对您有所帮助。
在阅读和观察其他问题和 Azure 示例后,我实施了一个解决方案。在这里发布我遇到的问题,希望能帮助其他刚接触 Azure Service Fabric 的开发人员(免责声明:我仍然是 Service Fabric 的新手,因此非常感谢评论和建议):
首先,非常简单 - 我最终在 Azure 服务结构应用程序中得到了有状态服务和 WEB Api 无状态服务:
DataStoreService
- 读取大型 XML 并将其存储到 Reliable 字典中的有状态服务(发生在 RunAsync 方法中)。- Web Api 提供了一个
/api/query
端点,过滤掉存储在 rteliable 字典中的 XElements 集合,并将其序列化回请求者
3 个陷阱
1)如何从无状态服务中获取可靠的字典数据,即如何从无状态服务中获取有状态服务的实例:
ServiceUriBuilder builder = new ServiceUriBuilder("DataStoreService");
IDataStoreService DataStoreServiceClient = ServiceProxy.Create<IDataStoreService>(builder.ToUri(), new ServicePartitionKey("Your.Partition.Name"));
上面的代码已经给了你实例。即 - 您需要使用服务代理。为此,您需要:
定义有状态服务将实现的接口,并在调用 ServiceProxy 的 Create 方法时使用它(
IDataStoreService
)将正确的分区键传递给 Create 方法。 This article 很好地介绍了 Azure 服务总线分区
2) 注册副本监听器 - 为了避免错误说
The primary or stateless instance for the partition 'a67f7afa-3370-4e6f-ae7c-15188004bfa1' has invalid address, this means that right address from the replica/instance is not registered in the system
,您需要按照
public DataStoreService(StatefulServiceContext context)
: base(context)
{
configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
}
3) Service fabric name spacing and referencing services - ServiceUriBuilder
class 我取自 service-fabric-dotnet-web-reference-app。基本上你需要一些东西来生成一个形式的 Uri:
new Uri("fabric:/" + this.ApplicationInstance + "/" + this.ServiceInstance);
,
其中 ServiceInstance
是您要获取其实例的服务的名称(在本例中为 DataStoreService
)