Example 1
Example 1
import { createApplication } from '@setu-ts/kernel'; import { RuntimePlugin } from '@setu-ts/runtime'; import { ServiceDiscoveryPlugin } from '@setu-ts/service-discovery-plugin'; import { CAPABILITIES, type IServiceDiscovery } from '@setu-ts/common'; const app = createApplication({ plugins: [ RuntimePlugin(), ServiceDiscoveryPlugin({ provider: 'consul', address: 'http://127.0.0.1:8500', }), ], }); await app.start({ port: 3000 }); const discovery = app.services.get<IServiceDiscovery>( CAPABILITIES.SERVICE_DISCOVERY, ); const url = await discovery.resolveUrl('billing', '/invoices');
Reads and registers against a Consul agent.
-
deregisterSelf(registration: SelfRegistration): Promise<void>
Removes this instance from the agent.
-
headers(): Record<string, string>
Headers every request carries; the ACL header is omitted when unset.
-
healthUrl(): stringserviceName: string,index?: number
Builds the health-read URL.
-
isHealthy(): Promise<boolean>
M70c: probes the agent's leader endpoint —
GET /v1/status/leader— through the existingIDiscoveryHttpseam. 2xx means the agent is reachable; any other status or a transport failure means it is not. -
kind: string
Backend id.
-
mapEntries(): readonly ServiceInstance[]body: unknown,serviceName: string
Maps a health response body onto instances.
-
registerSelf(registration: SelfRegistration): Promise<void>
Advertises this instance to the agent.
-
resolve(serviceName: string): Promise<readonly ServiceInstance[]>
Reads the current instance list for a service.
-
selfId(registration: SelfRegistration): string
The instance id used for both register and deregister.
-
watch(): Promise<Unsubscribe>serviceName: string,listener: (instances: readonly ServiceInstance[]) => void
Subscribes to instance-list changes.
Resolves services through DNS.
-
domainFor(serviceName: string): string
Substitutes the service name into the configured template.
-
kind: string
Backend id.
-
resolve(serviceName: string): Promise<readonly ServiceInstance[]>
Reads the current instance list for a service.
-
watch(): Promise<Unsubscribe>serviceName: string,listener: (instances: readonly ServiceInstance[]) => void
Polls at
watchIntervalMs, firing only when the instance list changed.
Reads EndpointSlices for a service.
-
authHeader(): Promise<string>
Resolves the bearer token, reading the projected file when no explicit token was configured.
-
isHealthy(): Promise<boolean>
M70c: probes the API server with a
limit=1EndpointSlice LIST through the existing seam. This is the probe that makes X10-3 visible: anUnknownIssuerTLS rejection surfaces here asdown. 2xx means the API is reachable. -
kind: string
Backend id.
-
listUrl(): stringserviceName: string,extra?: Readonly<Record<string, string>>
Builds the EndpointSlice list URL for a service.
-
mapSlices(): readonly ServiceInstance[]body: unknown,serviceName: string
Maps an EndpointSlice list onto instances.
-
resolve(serviceName: string): Promise<readonly ServiceInstance[]>
Reads the current instance list for a service.
-
watch(): Promise<Unsubscribe>serviceName: string,listener: (instances: readonly ServiceInstance[]) => void
Subscribes to instance-list changes.
selfRegistration was configured against a provider that cannot register.
-
name: string
Discriminating name for
instanceof-free checks.
Serves a configured instance list.
-
isHealthy(): Promise<boolean>
M70c: a static map is in memory and cannot be unreachable, so it is always reachable (M47).
-
kind: string
Backend id.
-
resolve(serviceName: string): Promise<readonly ServiceInstance[]>
Reads the current instance list for a service.
-
watch(): Promise<Unsubscribe>serviceName: string,listener: (instances: readonly ServiceInstance[]) => void
Fires once with the configured list and never again.
Creates the default HTTP seam.
Creates the service discovery plugin.
The 'dns' arm in address mode — A/AAAA records carry no port.
-
mode: "a"
Query
A/AAAArecords. -
port: number
Port every resolved address is reached on.
The 'consul' arm.
-
address: string
Base URL of the Consul agent, e.g.
http://127.0.0.1:8500. -
datacenter: string
Datacenter, sent as
?dc=. -
http: IDiscoveryHttp
Overrides the default
fetch-backed HTTP seam. -
provider: "consul"
Discriminant.
-
secure: boolean
Whether resolved instances speak TLS — Consul carries no scheme.
-
token: string
ACL token, sent as
X-Consul-Token. -
waitSeconds: number
Blocking-query
wait, in seconds. Default30, clamped to Consul's documented maximum of 600.
Constructor options.
-
address: string
Base URL of the Consul agent.
-
datacenter: string
Datacenter, sent as
?dc=. -
secure: boolean
Whether resolved instances speak TLS.
-
token: string
ACL token, sent as
X-Consul-Token. -
waitSeconds: number
Blocking-query wait, in seconds.
The 'custom' arm — the application's own backend.
-
discovery: DiscoveryProvider
The provider, used as supplied.
-
provider: "custom"
Discriminant.
A buffered HTTP response, as IDiscoveryHttp.request returns it.
-
headers: Headers
Response headers.
-
ok: boolean
Whether the status is 2xx.
-
status: number
HTTP status code.
-
text: string
Response body, read to completion as text.
A streaming HTTP response, as IDiscoveryHttp.stream returns it.
-
body: ReadableStream<Uint8Array> | null
The response body, or
nullwhen the response carries none. -
headers: Headers
Response headers.
-
ok: boolean
Whether the status is 2xx.
-
status: number
HTTP status code.
A discovery backend.
-
deregisterSelf(registration: SelfRegistration): Promise<void>
Removes this application instance from the backend.
-
isHealthy(): Promise<boolean>
M70c: reports whether the discovery backend is reachable right now, for the plugin's health indicator.
-
kind: string
Backend id, surfaced by the health indicator.
-
registerSelf(registration: SelfRegistration): Promise<void>
Registers this application instance with the backend.
-
resolve(serviceName: string): Promise<readonly ServiceInstance[]>
Reads the current instance list for a service.
-
watch(): Promise<Unsubscribe>serviceName: string,listener: (instances: readonly ServiceInstance[]) => void
Subscribes to instance-list changes.
Outlier-ejection tuning.
-
durationMs: number
How long an ejection lasts, in milliseconds.
-
failureThreshold: number
Failures inside the window that eject an instance.
-
maxEjectionPercent: number
Ceiling on the percentage of a service's instances ejected at once.
-
windowMs: number
Rolling window, in milliseconds, that failures are counted over.
The HTTP surface the Consul and Kubernetes providers need.
-
request(): Promise<DiscoveryHttpResponse>url: string,init?: RequestInit
Performs a request and reads the whole body.
-
stream(): Promise<DiscoveryHttpStream>url: string,init?: RequestInit
Performs a request and leaves the body unread.
The 'kubernetes' arm — EndpointSlices read from the API server.
-
apiServer: string
API server base URL. Defaults to the in-cluster
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT. -
http: IDiscoveryHttp
Overrides the default
fetch-backed HTTP seam. -
namespace: string
Namespace the services live in.
-
portName: string
Selects the
ports[]entry by name when a service exposes several. -
provider: "kubernetes"
Discriminant.
-
secure: boolean
Whether resolved instances speak TLS.
-
token: string
Bearer token, used verbatim.
Constructor options.
-
apiServer: string
API server base URL, already resolved from options or the environment.
-
namespace: string
Namespace the services live in.
-
portName: string
Selects the
ports[]entry by name. -
secure: boolean
Whether resolved instances speak TLS.
-
token: string
Bearer token used verbatim; absent means read the projected token file.
What this application advertises about itself.
-
address: string
Address other applications should reach this instance on.
-
check: SelfRegistrationCheck
The mandatory health check (defaults applied by
resolveOptions). -
drainDelayMs: number
Milliseconds to keep serving after deregistering, before draining begins.
-
id: string
Instance id, unique within the service. Defaults to
<name>-<uuid>. -
metadata: Readonly<Record<string, string>>
Key/value metadata to attach to the registration.
-
port: number
Port other applications should reach this instance on.
-
serviceName: string
Logical service name other applications resolve.
-
tags: readonly string[]
Labels to attach to the registration.
The health check the backend runs against this instance after registration.
-
deregisterAfterSeconds: number
Seconds a critical service survives before the backend removes it.
-
httpPath: string
HTTP path the backend polls, appended to this instance's origin.
-
intervalSeconds: number
Seconds between checks.
The 'dns' arm in SRV mode — records carry their own ports.
-
mode: "srv"
Query
SRVrecords and honor RFC 2782 priority tiers.
The 'static' arm — a literal instance list, with no backend at all.
-
provider: "static"
Discriminant.
-
services: Readonly<Record<string, readonly StaticServiceDefinition[]>>
Service name to its instances. An unknown name resolves to
[]. -
watchIntervalMs: number
Milliseconds between
watch()polls.
One entry of a 'static' service list.
-
host: string
Hostname or IP literal.
-
id: string
Instance id. Synthesized as
<host>:<port>when omitted. -
metadata: Readonly<Record<string, string>>
Key/value metadata.
-
port: number
TCP port.
-
secure: boolean
Whether the instance speaks TLS.
-
tags: readonly string[]
Labels.
-
weight: number
Relative selection weight for
'weighted-random'.
| ConsulDiscoveryOptions
| KubernetesDiscoveryOptions
| DnsDiscoveryOptions
| CustomDiscoveryOptions
Options accepted by ServiceDiscoveryPlugin.
Usage
import * as Service_discovery_for_Setu_TS___turns_a_logical_service_name_into_a_reachable_address__balances_across_the_instances_behind_it__and_takes_them_out_of_rotation_when_callers_report_failures___Registers_an__IServiceDiscovery__under__CAPABILITIES_SERVICE_DISCOVERY___backed_by_a_pluggable_provider__a_literal___static___list____consul______kubernetes___EndpointSlices____dns____SRV_and_address_records___or_the_application_s_own___custom___provider__Zero_npm_dependencies___the_HTTP_providers_run_on_web_standard__fetch__and_the_DNS_provider_on_the_optional__IRuntimeServices_dns___ from "service-discovery-plugin/src/index.ts";