Service registry for rest or tcp socket hosts
908
A small HTTP service that tells your applications where their dependencies live. Clients ask for a service
by name (usually the interface name, e.g. IFolderStorageRestService) and get back its address:
a base URL for REST services, or an IP address and port for TCP socket services.
If several instances share a name, the registry hands out the least used one, so the load spreads round-robin. Services can be listed statically in configuration or register and unregister themselves at runtime.
The .NET client is Service.Registry on NuGet. It resolves the address and creates a typed REST or TCP client in one call.
docker run -d --name serviceregistry -p 7777:7777 <namespace>/serviceregistry:latest
curl http://localhost:7777/api/service/all
services:
serviceregistry:
image: <namespace>/serviceregistry:latest
restart: unless-stopped
ports:
- "7777:7777"
volumes:
- ./appsettings.json:/app/appsettings.json:ro # static list of services (optional)
Base path: /api/service
| Method | Path | Purpose |
|---|---|---|
GET | Get?name= | one address for the service (least used instance); 204 if unknown |
GET | all?name= | all instances of the service |
GET | all | every registered instance |
POST | Register | register instances: JSON array of { "name", "host" } or { "name", "address", "port" } |
POST | Remove | unregister instances (same body) |
Register a REST host:
curl -X POST http://localhost:7777/api/service/Register \
-H "Content-Type: application/json" \
-d '[{ "name": "IFolderStorageRestService", "host": "http://filestorage:11580/" }]'
Register a TCP host:
curl -X POST http://localhost:7777/api/service/Register \
-H "Content-Type: application/json" \
-d '[{ "name": "ITcpFolderStorageService", "address": "10.0.0.5", "port": 11581 }]'
Services known at startup go in the Clients section of /app/appsettings.json. Mount your own file:
{
"Clients": [
{ "id": "IFolderStorageRestService", "host": "http://filestorage:11580/" },
{ "id": "ITcpFolderStorageService", "address": "filestorage", "port": "11581" }
],
"ServiceRegistry": {
"Host": "http://+:7777",
"address": "http://localhost:7777"
}
}
| Setting | Default in the image | Notes |
|---|---|---|
| Listen URL | http://+:7777 | env ServiceRegistry__Host |
| Static services | none | Clients in appsettings.json (only from the file, not from env vars) |
Entries with the same id are instances of one service and are balanced between.
Runtime registrations live in memory only. After a restart the registry starts again from Clients,
and services re-register themselves when they start.
<PackageReference Include="Service.Registry" Version="*" />
// appsettings.json of the client application
{ "serviceRegistry": { "address": "http://serviceregistry:7777" } }
var registry = new ServiceRegistryFactory();
// typed REST client: the address is resolved by the interface name
var storage = registry.CreateRest<IFolderStorageRestService>();
// typed TCP client
var tcp = registry.CreateTcp<ITcpFolderStorageService>();
// self-registration on startup
registry.AddClients(new List<ServiceClient> {
new ServiceClient { Name = "IMyService", Host = "http://my-service:8080/" }
});
Logs go to stdout (docker logs). Set Logging__LogLevel__Service.Registry.Host=Debug to log one line per request.
The API has no authentication: anyone who can reach the port can register or remove services. Expose the port only inside your private network.
Install/ in the source repositoryContent type
Image
Digest
sha256:e0be30038…
Size
89.6 MB
Last updated
10 days ago
docker pull bsacore/serviceregistryhost