Dynamically creating services in an service fabric application

断了今生、忘了曾经 提交于 2019-12-11 01:01:46

问题


This is a bit descriptive so please bear with me. :)

In the application that I'm trying to build, there are distinct functionalities of product. Users can choose to opt-in for functionality A, B, D but not C. The way I'm building this, is that each of the distinct functionality is a Service (stateless, I'm thinking of storing the data in Azure SQL DBs and exposing REST APIs from each service). Bundled all services together is an ApplicationType. For each customer tenant (consider this as an shared account of a group of users) that is created, I'm thinking of creating a new concrete instance of registered ApplicationType using a TenantManagementService and calling client.ApplicationManager.CreateApplicationAsync() on a FabricClient instance so that I can have a dedicated application instance running on my nodes for that tenant. However, as I mentioned, a tenant can choose to opt-in only for specific functionality which is mapped to a subset of services. If a tenant chooses only service A of my Application, rest of the service instances corresponding to features B, C, D shouldn't be idly running on the nodes.

I thought of creating actors for each service, but the services I'm creating are stateless and I'd like to have multiple instances of them actively running on multiple nodes to load balance rather than having idle replicas of stateful services.

Similar to what I'm doing with application types, i.e., spawning application types as a new tenant registers, can I spawn/delete services as and when a tenant wants to opt-in/out of product features?

Here's what I've tried: I tried setting InstanceCount 0 for the services at when packaging my application. In my ApplicationParameters XML files:

<Parameters>
    <Parameter Name="FeatureAService_InstanceCount" Value="0" />
    <Parameter Name="FeatureBService_InstanceCount" Value="0" />
</Parameters>

However, Service Fabric Explorer cribs when instantiating the application out of such application type. The error is this:

But on the other hand, when a service is deployed on the fabric, it gives me an option to delete it specifically, so this scenario should be valid.

Any suggestions are welcome!

EDIT: My requirement is similar to the approach mentioned by anderso in here - https://stackoverflow.com/a/35248349/1842699, However, the problem that I'm specifically trying to solve is to upload create an application instance with one or more packaged services having zero instance count!


回答1:


@uplnCloud

I hope I understand everything right.

Your situation is the following:

  • Each customer should have separate Application (created from the same ApplicationType).
  • Each customer should have only subset of Services (defined in ApplicationType).

If I get it right then this is supported out of the box.

First of all you should remove <DefaultServices /> section from the ApplicationManifest.xml. This will instruct Service Fabric to don't create services automatically with the application.

Now the algorithm is the following:

  1. Create application using FabricClient.ApplicationManager.CreateApplicationAsync()
  2. For each required feature create a new corresponding Service using FabricClient.ServiceManager.CreateServiceAsync() (you need to specify the Application name of newly created Application)

Also note that CreateServiceAsync() accepts ServiceDescriptor that you can configure all service related parameters - starting from partitioning schema and ending up with instance count.




回答2:


Unfortunately you can't have 0 instance services, Service Fabric has the idea that a named service always exists(running). In this case, when you define a service (give a name to a serviceType instance), it will have at least 1 instance running, otherwise, you shouldn't even have the definition of this service on your application if it does not need to be running.

But what you can have is the ServiceType definition, that means, you have the binaries but you will create it when required.

I assume you are being limited by the default services, where you declare the application and services structure upfront(before deployment of any application instance), instead, you should use dynamic service creation via FabricClient like you described, or via Powershell using New-ServiceFabricApplication and New-ServiceFabricService .

This link you guide you how to do it using FabricClient




回答3:


I'll just add this as a new answer instead of commenting on another answer.

As other have mentioed, remove DefaultServices from your ApplicationManifest. That way, every new instance of the ApplicationType you create will come online without services, and you'll have to create those manually depending on what functionallity your customer has selected.

Also, going with the "services per customer" approach, make sure you got enough nodes to handle the load when you get customers online. You'll end up with a lot of processes (since Application Instances) runs their own processes of the services, and if you have few nodes with a lot of these, reboots to your cluster nodes can take a bit to stabilise since it can potentionally have many services it needs to relocate. Altough running Stateless Services will aleviate a good part of this.



来源:https://stackoverflow.com/questions/51024779/dynamically-creating-services-in-an-service-fabric-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!