@equinor/fusion-framework-module-service-discovery

中文文档
10.3.0发布于 2026/11/12ISCTypeScriptES Module近 30 天下载 1.3万
原始使用文档
文档目录13 节
  1. When to Use
  2. Prerequisites
  3. Quick Start
  4. 1. Register an HTTP client for the service discovery API
  5. 2. Enable service discovery
  6. 3. Resolve services at runtime
  7. Documentation
  8. Testing
  9. Operational telemetry
  10. Key Concepts
  11. Inheritance
  12. Caching
  13. Failure handling

@equinor/fusion-framework-module-service-discovery

Resolves service endpoint URIs and authentication scopes from a central service discovery API so that application code can request named services without hard-coding URLs.

When to Use

Use this module when your application needs to:

  • Look up backend service base URIs at runtime instead of baking them into config
  • Obtain OAuth scopes for token acquisition automatically
  • Create pre-configured HTTP clients for discovered services

Prerequisites

The module depends on @equinor/fusion-framework-module-http for HTTP transport. An HTTP client pointing at the service discovery backend must be registered before service discovery is enabled.

[!WARNING] The HTTP module must be enabled in the runtime (e.g. Portal). If you are building a sub-module such as an Application, the portal typically inherits this configuration for you.

Quick Start

1. Register an HTTP client for the service discovery API

Skip this step if the runtime already provides an HTTP client keyed "service_discovery".

import { configureHttpClient } from '@equinor/fusion-framework-module-http';
import type { ModulesConfigurator } from '@equinor/fusion-framework-module';

const configurator = new ModulesConfigurator();
configurator.addConfig(
  configureHttpClient('service_discovery', {
    baseUri: 'https://discovery.example.com',
    defaultScopes: ['https://discovery.example.com/.default'],
  }),
);

2. Enable service discovery

import { enableServiceDiscovery } from '@equinor/fusion-framework-module-service-discovery';

// Auto-detects the 'service_discovery' HTTP client
enableServiceDiscovery(configurator);

3. Resolve services at runtime

// Resolve a single service
const contextService = await modules.serviceDiscovery.resolveService('context');
console.log(contextService.uri); // 'https://api.example.com/context'

// Create a ready-to-use HTTP client for a discovered service
const client = await modules.serviceDiscovery.createClient('people');
const data = await client.fetchAsync('/persons?search=Jane');

Documentation

GuideCovers
ConfigurationCustom HTTP client keys, custom clients, and replacing the discovery client
TestingThe /mock entry point: an in-memory registry, local mock servers, and spying
Session OverridesRedirecting services to local or staging URLs during development
API ReferenceExports and the Service shape

Testing

Import from @equinor/fusion-framework-module-service-discovery/mock to resolve services from an in-memory registry instead of the service registry. The real configurator, provider and validation still run — only the boundary that would contact the registry is substituted.

import { enableServiceDiscoveryMock } from '@equinor/fusion-framework-module-service-discovery/mock';

enableServiceDiscoveryMock(configurator);

Baseline services (apps, people, context, bookmarks, notification) resolve out of the box. The configurator owns the registry, so pointing every service at a locally running mock server takes no client:

enableServiceDiscoveryMock(configurator, (builder) => {
  builder.setBaseUri('http://localhost:6669');
  builder.addService({ key: 'my-api' });
});

The entry point has no test-runner dependency, and ships no mocking API of its own — spying on a call is your test runner's job.

See Testing for the full builder API, local-mock-server setup and runner guidance, or @equinor/fusion-framework/mock to mock every framework boundary at once.

Operational telemetry

When the telemetry module is available, the provider connects to it after initialization and records one fusion.service-discovery.resolve.duration metric per network, fallback or cooldown resolution, with a sanitized .failure exception when resolution throws. Notifications explain where the list came from: fusion.service-discovery.fallback (last known good list served instead of a failure) and fusion.service-discovery.cooldown (failed fast during backoff, without a network attempt). fusion.service-discovery.unknown-service marks a key that is not registered. Keys, URIs, scopes and errors are never recorded.

Cached resolutions are the bulk of lookups, so they record only a Debug-level fusion.service-discovery.cache-hit event and no duration metric. Adapters that export Information and above therefore send nothing for them.

The HTTP attempt behind a network resolution is reported by HTTP telemetry, not repeated here. A child host that reuses its parent's client leaves reporting to the parent, so each resolution is recorded once. Custom clients opt in by exposing diagnostic$, an observable of ServiceDiscoveryDiagnostic.

Key Concepts

Inheritance

When used inside a sub-module (e.g. an Application), the Service Discovery module inherits the parent module's discovery client by default. This means:

  • The child shares the parent's cache, avoiding duplicate API calls
  • The child sees the same session overrides
  • Breaking changes in the parent's client could affect the child

Caching

The built-in ServiceDiscoveryClient caches results for 5 minutes via @equinor/fusion-query. The allow_cache parameter on resolveService / resolveServices controls whether to return the first cached snapshot (true) or wait for the latest response (false, the default).

Failure handling

Failed requests are never cached by @equinor/fusion-query, so without protection every retry, page reload, or re-render would call the service discovery API again. The built-in client hardens this:

  • Failure backoff — after a failed request, outgoing requests pause for 1s, then 2s, 4s … up to 30s. Concurrent callers sharing one failed request count as a single failure. The pause resets on the next successful response.
  • Stale fallback — when a refresh fails, or a call arrives during the pause, the last successful service list is returned if it is younger than 24 hours, so applications keep working through short outages.
  • Fail fast — without a usable fallback, the request that failed rethrows its original error (for example HttpJsonResponseError with status 401), and calls during the pause throw ServiceDiscoveryUnavailableError without touching the network. Its cause holds the original error and retryAt the time requests resume.
import { ServiceDiscoveryUnavailableError } from '@equinor/fusion-framework-module-service-discovery';

try {
  await serviceDiscovery.resolveService('people');
} catch (error) {
  if (error instanceof ServiceDiscoveryUnavailableError) {
    // service discovery is down; error.cause is the original failure
  }
}

版本与发布记录

@equinor/fusion-framework-module-service-discovery 共发布 228 个版本,首次发布于 2022/01/17;过去 12 个月发布了 20 个版本。

版本发布日期距今
10.3.0latest2026/11/121 天前
10.3.0-next.2next2026/11/112 天前
10.3.0-next.12026/10/2915 天前
10.3.0-next.02026/10/2816 天前
10.2.1-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85atrusted-publishing-202610152026/10/1629 天前
10.2.02026/10/1529 天前

其他发布通道:apploader → 8.0.15-apploader-be21a6b60eb13eee8bf869e75002d8634e9c50a9,cli-search-index → 9.0.5-cli-search-index.0,msal-v5 → 9.0.6-msal-v5.1,trusted-publishing-20261015 → 10.2.1-trusted-publishing-20261015-9dfe7602cd2a49a8a9df00d62c26e7304a82a85a,next → 10.3.0-next.2。

关于 @equinor/fusion-framework-module-service-discovery 的常见问题

如何安装 @equinor/fusion-framework-module-service-discovery?

在项目目录运行 npm install @equinor/fusion-framework-module-service-discovery,或使用 pnpm add / yarn add / bun add。

@equinor/fusion-framework-module-service-discovery 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@equinor/fusion-framework-module-service-discovery 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@equinor/fusion-framework-module-service-discovery 可以商用吗?

@equinor/fusion-framework-module-service-discovery 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@equinor/fusion-framework-module-service-discovery 的下载量有多少?

根据 npm 官方下载统计,@equinor/fusion-framework-module-service-discovery 在 2026/10/14 – 2026/11/12 这 30 天内被下载 12,622 次,日均约 421 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@equinor/fusion-framework-module-service-discovery 还在维护吗?

最新版本 10.3.0 发布于 2026/11/12(1 天前);至今共发布 228 个版本,其中过去 12 个月发布了 20 个;首次发布于 2022/01/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。