@creditkarma/consul-client

中文文档
1.0.0发布于 2022/06/24Apache-2.0TypeScriptCommonJS近 30 天下载 2106

A client for Hashicorp Consul written in TypeScript

原始使用文档
文档目录9 节
  1. Install
  2. K/V Store
  3. IKey
  4. Observer
  5. Request Options
  6. Catalog
  7. API Overview
  8. Contributing
  9. License

Consul Client

A client for Hashicorp Consul written in TypeScript.

This library currently has clients to support the Consul KV API and the Consul Catalog API.

Install

$ npm install --save @creditkarma/consul-client

K/V Store

The K/V store provides a simple JS API for getting values in and out of Consul. This API is primarily exposed through the KvStore class.

import { KvStore, IKey, Observer } from '@creditkarma/consul-client'

/**
 * Instantiate KvStore with location of Consul, default is localhost:8500.
 *
 * The argument is actually an array of locations in order to support fail over.
 */
const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ])

IKey

The KvStore reads, writes and deletes values with Consul based on IKey objects. These are objects with one required property and one optional property. The required property is path. The path is the key name to look up. The optional property is dc. The dc is the datacenter to read from. Per the Consul docs this will default to the datacenter of the agent being queried, specified by the host address.

// Set value for key
kvStore.set({ path: 'key', dc: 'dc1' }, 'test').then((success: boolean) => {
  if (success) {
    // write was successful
  }
})

// Get current value of key
kvStore.get({ path: 'key', dc: 'dc1' }).then((val: string) => {
  // val === 'test'
})

// Watch a key for value changes
kvStore.watch({ path: 'key', dc: 'dc1' }).onValue((val: string) => {
  // runs anytime the value changes, initially fires with current value
})

// Stop watching a value
kvStore.unwatch({ path: 'key', dc: 'dc1' })

// Delete key from consul
kvStore.delete({ path: 'key', dc: 'dc1' }).then((success: boolean) => {
  if (success) {
    // delete was successful
  }
})

Observer

An Observer is the object returned from a call to watch.

It has five public methods:

const observer: Observer<string> = kvStore.watch({ path: 'key', dc: 'dc1' })

observer.onValue((val: string) => {
    // runs anytime the value changes, initially fires with current value
})

observer.onError((err: Error) => {
    // runs anytime there is an error updating the value.
    // depending on the error the library may continue watching the key.
})

observer.current() // Returns the current value

observer.previous() // Returns the previous value

observer.destroy() // Nulls out all internal state

// Get the current value, returns null if no value
const currentVal: string | null = observer.current()

// Get the previous value, returns null in no previous value
const previousVal: string | null = observer.previous()

Request Options

We use Request as our underlying HTTP client. As such you can pass options through to Request to customize the HTTP request for your environment. This can be done both when instantiating a new KvStore or when making a request.

const kvStore: KvStore = new KvStore([ 'http://localhost:8500' ], { headers: { ... } })

kvStore.get({ path: 'key' }, { headers: { ... } })

The options given to the KvStore constructor are used on every request. Options given to a method are only used for that request. Options passed to a request method are deep merged with the instance options before performing the request.

Available Options

Catalog

The Catalog API allows you to discover other assets registered with your Consul instance. This is useful for service discovery.

import { Catalog } from '@creditkarma/consul-client'

/**
 * Instantiate Catalog with location of Consul, default is localhost:8500
 *
 * Like KvStore the Catalog supports fail over by providing a list of addresses.
 */
const catalog: Catalog = new Catalog([ 'http://localhost:8500' ])

API Overview

// List all nodes registered with Consul
catalog.listNodes().then((res: Array<INodeDescription>) => {
    // Do something
})

// List all services registered with Consul
catalog.listServices().then((res: IServiceMap) => {
    // Do something
})

// List all nodes registered with a given service
catalog.listNodesForService('service-name').then((res: IServiceMap) => {
    // Do something
})

// Get the registered address for a given service
catalog.resolveAddress('service-name').then((res: string) => {
    // Do something
})

// Watch for runtime changes to a service address
catalog.watchAddress('service-name').onValue((res: string) => {
    // Do something
})

Contributing

For more information about contributing new features and bug fixes, see our Contribution Guidelines. External contributors must sign Contributor License Agreement (CLA)

License

This project is licensed under Apache License Version 2.0

版本与发布记录

@creditkarma/consul-client 共发布 43 个版本,首次发布于 2017/12/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2022/06/244 年前
0.8.12-5next2022/06/144 年前
0.8.12-32022/06/094 年前
0.8.12-22022/06/084 年前
0.8.12-12022/06/034 年前
0.8.12-02022/06/024 年前

其他发布通道:next → 0.8.12-5,fix → 0.4.5。

关于 @creditkarma/consul-client 的常见问题

@creditkarma/consul-client 是什么?

A client for Hashicorp Consul written in TypeScript

如何安装 @creditkarma/consul-client?

在项目目录运行 npm install @creditkarma/consul-client,或使用 pnpm add / yarn add / bun add。

@creditkarma/consul-client 支持 TypeScript 吗?

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

@creditkarma/consul-client 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@creditkarma/consul-client 可以商用吗?

@creditkarma/consul-client 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@creditkarma/consul-client 的下载量有多少?

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

@creditkarma/consul-client 还在维护吗?

最新版本 1.0.0 发布于 2022/06/24(4 年前);至今共发布 43 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/12/06。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。