Create NCache Discovery Service
Now that the cluster and management services are created, NCache discovery service is created that serves the purpose of acquiring the IP addresses of the NCache servers and furnish them to the client applications. It is a stateless ASP.NET Core web API and it consults the Service Fabric naming service to get the endpoints registered by the NCache cluster service and providing them to the clients as HTTP response.
The service manifest file contains the endpoints by the communication listener to obtain the port to listen on.
<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest Name="NCacheDiscoveryPkg"
Version="1.0.0"
xmlns="http://schemas.microsoft.com/2011/01/fabric"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ServiceTypes>
<StatelessServiceType ServiceTypeName="NCacheDiscoveryType" />
</ServiceTypes>
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>NCacheDiscovery.exe</Program>
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
<EnvironmentVariables>
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value=""/>
</EnvironmentVariables>
</CodePackage>
<ConfigPackage Name="Config" Version="1.0.0" />
<Resources>
<Endpoints>
<Endpoint Protocol="http" Name="ServiceEndpoint" UriScheme="http" Port="55100" Type="Input" />
</Endpoints>
</Resources>
</ServiceManifest>
Here is a code snippet from the NCache Service Discovery application where the service calls the Service Fabric Naming Service to get access to the end points registered by the NCache Service.
// Beginning Code
string serviceUri = $"fabric:/ServiceFabricApplication/NCacheService";
ServicePartitionResolver resolver = ServicePartitionResolver.GetDefault();
ResolvedServicePartition resolvedServicePartition = await resolver.ResolveAsync(new Uri(serviceUri), new ServicePartitionKey(), new System.Threading.CancellationToken());
var endpoints = resolvedServicePartition.Endpoints;
var endpointDictionary =
new Dictionary<string, List<string>>();
JObject addresses;
string bridge_management_address;
string cache_management_address;
string cache_client_address;
string bridge_client_address;
foreach (var endpoint1 in endpoints)
{
addresses = JObject.Parse(endpoint1.Address);
bridge_management_address = (string)addresses["Endpoints"]["bridge-management"];
cache_management_address = (string)addresses["Endpoints"]["cache-management"];
cache_client_address = (string)addresses["Endpoints"]["cache-client"];
bridge_client_address = (string)addresses["Endpoints"]["bridge-client"];
if (!endpointDictionary.ContainsKey("bridge-management"))
{
endpointDictionary["bridge-management"] = new List<string>();
}
if (!endpointDictionary.ContainsKey("cache-management"))
{
endpointDictionary["cache-management"] = new List<string>();
}
if (!endpointDictionary.ContainsKey("cache-client"))
{
endpointDictionary["cache-client"] = new List<string>();
}
if (!endpointDictionary.ContainsKey("bridge-client"))
{
endpointDictionary["bridge-client"] = new List<string>();
}
endpointDictionary["bridge-management"].Add(bridge_management_address);
endpointDictionary["cache-management"].Add(cache_management_address);
endpointDictionary["cache-client"].Add(cache_client_address);
endpointDictionary["bridge-client"].Add(bridge_client_address);
}
// Rest of code
See Also
Deploying NCache in Azure Service Fabric
Create Service Fabric Cluster
Create NCache Cluster service
Create NCache Management Service