您需要以某种方式对您的服务进行分区。
有几个选项,但这里有两个很好(以及您链接的 SO 问题):
拥有一个 SF 应用程序,每个租户都可以在其中获取您的服务实例。然后,您需要在前面有一个共享服务,以将请求路由到正确的服务。它应该看起来像这样。
MyAwesomeApp
SharedStatelessApi <- External API points here
MyTenantService_Tenant1 <- ServiceType: MyTenantService
MyTenantService_Tenant2 <- ServiceType: MyTenantService
...
另一种解决方案是每个租户拥有一个(或多个)服务结构应用程序,并且看起来类似于:
MySharedApp
SharedStatelessApi <- External API points here
Tenant1 <- ApplicationType: MyTenantApp
MyTenantService <- ServiceType: MyTenantService
Tenant2 <- ApplicationType: MyTenantApp
MyTenantService <- ServiceType: MyTenantService
和第一个例子的概念一样,但是分区是在更高的水平上完成的。
就个人而言,我更喜欢第二种情况。感觉更对了。
在这两种情况下,您都必须在新客户注册时手动创建服务/应用程序,或者在代码中创建。如果你想用代码来做,你应该看看 FabricClient。如果您需要这方面的示例,请告诉我。
此外,如您所见,您应该有一个共享的公共端点,并在该端点中根据某些内容(标头、身份验证令牌、uri、与您的应用程序内联的任何内容)将请求路由到正确的服务。
使用 FabricClient 创建服务的示例:
首先你需要一个 FabricClient。对于不安全的集群(您的本地开发集群),以下内容就足够了:
var fabricClient = new FabricClient("localhost:19000");
当您部署到安全集群(例如在 Azure 中)时,您需要对 FabricClient 进行身份验证,如下所示:
var creds = new X509Credentials
{
FindType = X509FindType.FindByThumbprint,
FindValue = clientCertThumbprint,
RemoteCertThumbprints = {clientCertThumbprint},
StoreLocation = StoreLocation.LocalMachine,
StoreName = "My"
};
var clusterEndpoint = "CLUSTERNAME.LOCATION.cloudapp.azure.com:19000"
// or whatever your cluster endpoint is
var fabricClient = new FabricClient(creds, clusterEndpoint);
然后,当您拥有 FabricClient 时,您可以像这样创建无状态服务:
var statelessDescriptor = new StatelessServiceDescription
{
ApplicationName = new Uri("fabric:/MYAPP"),
InstanceCount = 1, // How many instances.
PartitionSchemeDescription = new SingletonPartitionSchemeDescription(),
ServiceName = new Uri("fabric:/MYAPP/TenantA"),
ServiceTypeName = "YourServiceTypeName",
InitializationData = DATA_TO_PASS_TO_SERVICE_BYTE[] // Only if needed.
};
await _client.ServiceManager.CreateServiceAsync(statelessDescriptor)
如果您在“InitializationData”属性中传递了任何数据,它将在服务中作为 ServiceInitializationParameters.InitializationData 提供