Sign inSign up

bsacore/serviceregistryhost

By bsacore

•Updated 10 days ago

Service registry for rest or tcp socket hosts

Image
0

908

bsacore/serviceregistryhost repository overview

⁠Service Registry

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.

⁠Quick start

docker run -d --name serviceregistry -p 7777:7777 <namespace>/serviceregistry:latest
curl http://localhost:7777/api/service/all
⁠docker compose
services:
  serviceregistry:
    image: <namespace>/serviceregistry:latest
    restart: unless-stopped
    ports:
      - "7777:7777"
    volumes:
      - ./appsettings.json:/app/appsettings.json:ro   # static list of services (optional)

⁠HTTP API

Base path: /api/service

MethodPathPurpose
GETGet?name=one address for the service (least used instance); 204 if unknown
GETall?name=all instances of the service
GETallevery registered instance
POSTRegisterregister instances: JSON array of { "name", "host" } or { "name", "address", "port" }
POSTRemoveunregister 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 }]'

⁠Configuration

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"
  }
}
SettingDefault in the imageNotes
Listen URLhttp://+:7777env ServiceRegistry__Host
Static servicesnoneClients 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.

⁠Using it from .NET

<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

Logs go to stdout (docker logs). Set Logging__LogLevel__Service.Registry.Host=Debug to log one line per request.

⁠Security

The API has no authentication: anyone who can reach the port can register or remove services. Expose the port only inside your private network.

Tag summary

Content type

Image

Digest

sha256:e0be30038…

Size

89.6 MB

Last updated

10 days ago

docker pull bsacore/serviceregistryhost