@setu-ts/service-discovery-plugin — locate and select service instances
Add it: setu add service-discovery adds it and prints the registration for setu.config.ts.
It provides CAPABILITIES.SERVICE_DISCOVERY (IServiceDiscovery).
Configure a static service pool
import { ServiceDiscoveryPlugin } from '@setu-ts/service-discovery-plugin';
export const discovery = ServiceDiscoveryPlugin({
provider: 'static',
services: {
billing: [{ host: '10.0.0.1', port: 8080 }, { host: '10.0.0.2', port: 8080 }],
},
});
Resolve an upstream URL from a route
import { CAPABILITIES } from '@setu-ts/common';
import type { IRouterApi, IServiceDiscovery } from '@setu-ts/common';
export function registerBillingRoutes(router: IRouterApi): void {
router.get('/billing-location', {
handler: async (ctx) => {
const discovery = ctx.services.get<IServiceDiscovery>(CAPABILITIES.SERVICE_DISCOVERY);
const url = await discovery.resolveUrl('billing', '/invoices');
return url === null
? ctx.response.status(503).json({ error: 'No billing instance' })
: ctx.response.json({ url });
},
});
}
Behaviour the types do not show
resolveUrlandpickreturnnullfor an empty pool.resolvelists all known instances, including ejected ones;pickfilters ejected instances, falling back to the full pool if needed.- Selection defaults to round-robin. A per-call
strategyoverrides the configured default. - Resolved lists are cached for 30000 ms by default;
cacheTtlMs: 0disables cache reuse. Concurrent cold reads of one service share a backend read. - Ejection depends on application
report(instance, outcome)calls. By default five failures within 30000 ms eject for 30000 ms, subject to a 50% pool cap; success clears the state. - A watch delivers the full list and refreshes the cache on change. Stop it using the returned unsubscribe function.
Other options
provideralso selects'consul','kubernetes','dns'or'custom'withdiscovery.strategyalso accepts'random'and'weighted-random';ejection: falsedisables ejection.selfRegistrationis Consul-only; other providers reject it withSelfRegistrationNotSupportedError. DNS requires the runtime DNS surface.