您需要对服务进行分区。
有几个选项,但是两个与此处(以及您链接的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"
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使用。