# @sealsystems/seal-consul

> seal-consul provides service discovery based on Consul.

Latest version **3.5.17** (published 2018-09-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sealsystems/seal-consul
pnpm add @sealsystems/seal-consul
yarn add @sealsystems/seal-consul
bun add @sealsystems/seal-consul
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.5.17 |
| Published | 2018-09-09 |
| First published | 2017-04-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 23.7 KB |
| Known vulnerabilities | 0 (+11 in 3 direct dependencies) |
| Install scripts | no |
| Author | SEAL Systems AG |
| Maintainers | comgit, michaelscherer-seal, seal-mt, stefanscherer |

## Links

- npm: https://www.npmjs.com/package/@sealsystems/seal-consul
- Repository: https://github.com/sealsystems/node-consul
- Homepage: https://github.com/sealsystems/node-consul#readme
- Issues: https://github.com/sealsystems/node-consul/issues
- npm.io page: https://npm.io/package/@sealsystems/seal-consul

## Dependencies (11)

- [async](https://npm.io/package/async.md) 2.6.1
- [retry](https://npm.io/package/retry.md) 0.12.0
- [consul](https://npm.io/package/consul.md) 0.34.0
- [getenv](https://npm.io/package/getenv.md) 0.7.0
- [lodash](https://npm.io/package/lodash.md) 4.17.10
- [dnscache](https://npm.io/package/dnscache.md) 1.0.1
- [seal-log](https://npm.io/package/seal-log.md) 1.2.0
- [seal-droddel](https://npm.io/package/seal-droddel.md) 1.0.1
- [seal-tlscert](https://npm.io/package/seal-tlscert.md) 1.2.3
- [app-root-path](https://npm.io/package/app-root-path.md) 2.1.0
- [parse-duration](https://npm.io/package/parse-duration.md) 0.1.1

## Recent versions

- 3.5.17 (latest) — 2018-09-09
- 3.5.16 — 2018-09-09
- 3.5.15 — 2018-09-09
- 3.5.14 — 2018-09-08
- 3.5.7 — 2017-07-13
- 3.5.5 — 2017-07-05
- 3.5.4 — 2017-04-11
- 3.5.3 — 2017-04-11
- 3.5.2 — 2017-04-11
- 3.5.1 — 2017-04-11
- 3.5.0 — 2017-04-10

## README

# seal-consul

[![CircleCI](https://circleci.com/gh/sealsystems/seal-consul.svg?style=svg)](https://circleci.com/gh/sealsystems/seal-consul)
[![AppVeyor](https://ci.appveyor.com/api/projects/status/3y40yyflrpw10hao?svg=true)](https://ci.appveyor.com/project/Plossys/seal-consul)

seal-consul provides service discovery based on Consul.

## Installation

    $ npm install seal-consul

## Quick start

First you need to add a reference to seal-consul within your application.

```javascript
const consul = require('seal-consul');
```

Then call `connect` to register your service with Consul.

```javascript
consul.connect({
  id: 'my-service-id',
  name: 'my-service-name',
  serviceUrl: 'http://localhost:3000', // URL of my service
  consulUrl: 'http://localhost:8500' // URL of a Consul server
}, (err) => {
  if (err) {
    throw err;
  }
  // Your service is now registered
});
```

You may omit the hostname of your service in `serviceUrl` (e.g. by setting it to `http://:3000`). In this case, your service is assumed to run on the same host as the Consul agent.

For the service, a new health check with a TTL of 10 seconds will be created. A heartbeat request will be sent every 5 seconds to Consul in order to prevent the TTL to expire.

By default, the status of a service is `warn`. Consul also recognizes the states `pass`and `fail`. Call the appropriate function, to change the state of your service. To set it to e.g. `pass`, use:

```javascript
consul.pass((err) => {
  // Check the result of your status change here...
});
```

To get all nodes providing a specific service, call `getNodes`. It uses the same interface as [node-consul's `consul.catalog.service.nodes` function](https://github.com/silas/node-consul#catalog-service-nodes).

## Watching a service

Use the `watch` function to receive notifications when the group of nodes that provide a service has been changed:

```javascript
consul.watch({
  serviceName: 'my-service-name', // Name of the service to watch
  consulUrl: 'http://localhost:8500' // URL of a Consul server
}, (err, nodes) => {
  if (err) {
    throw err;
  }

  // The 'nodes' array contains data about all nodes that provide the watched service
});
```

The callback is triggered as soon as a new node provides the service or a node is no longer available. Only nodes with passing health checks are regarded as available. At the start of the watch, the callback is also immediately triggered with an array of all currently active nodes.

A node object contains the following properties:
- `host`: The address of the node
- `node`: Consul's node name
- `port`: The port used by the service


## Custom Consul domain

By default the domain `consul` will be used to resolve a service. E.g. the service `checkout` will be expanded to `checkout.service.consul`. If another domain is given in Consul's configuration, you must set the environment variable `CONSUL_DOMAIN` accordingly.

If you configure Consul to use e.g. `sealsystems.com` as the domain, you must also define this domain via the environment variable:

```bash
CONSUL_DOMAIN=sealsystems.com
```

This will change the expanded service name given above to: `checkout.service.sealsystems.com`

## Initializing without connecting first

It is assumed that you call `consul.connect` first. This will establish the connection to the local Consul agent. The other functions (e.g. `consul.getHostname`) will throw an error if this connection has not been initialized.

If you do not want to register a service check via `consul.connect`, just call `consul.initialize` instead. This will only connect to the Consul agent. Now, you can  use most of the other functions.

Please note: `consul.heartbeat`, `consul.lookup`, `consul.resolveService` require `consul.connect` to be called. They will not work properly if you only call `consul.initialize`.

## Running the build

To build this module use [roboter](https://www.npmjs.com/package/roboter).

```bash
$ bot
```

---
_Source: https://npm.io/package/@sealsystems/seal-consul · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
