msdr-zk-nodejs
A Simple ZooKeeper based Microservices Service Discovery and Registery library for node.js services, that automatically re-register the watch again on the path when data is requested for the first time.
Important Point
This library doesn't create the basePath at the ZooKeeper tree for services, assuming each service needs to be provisioned as the requirement comes and to avoid the services getting registered at the incorrect basePath at the Zookeeper end in the system.
Installation
npm install --save msdr-zk
Usage.
const msdzkP = msdzk; let msdzkConnection;msdzkP; const registerServiceParam = name: "service-name" port: "4000" protocol: "http" api: "/api/v1" ip: "localhost" release: "1.1.0" metadata: check: status: "/status" health: "health" interval: "30s" user: "node" tags: "api" ; // register servicemsdzk; //allows service to register and then check;
More Examples in examples folder.
Api.
Promise
1. connect(connectionOptions) ⇒ connect Initiate the connection to the provided server list (ensemble). Examples:
;
Kind: global function
Returns: Promise
- that resolves to an event emitter instance Event.
Param | Type | Description |
---|---|---|
connectionOptions | object |
|
connectionOptions.connectionURL | string |
server list (ensemble) as string, Comma separated host:port pairs. |
connectionOptions.basePath | string |
the base path in the ZooKeeper tree for services of the system or platform. |
All the services will get connected to the basePath and
store their endpoints as ephemeral nodes
, at the service-name node because ephemeral nodes are only active as long as the session that created it is active. So if the services goes down the ephemeral node that it created also comes to an end.
It's advisable to create the basePath as
persistent znodes
as Persistent znodes are useful for storing data that needs to be highly available and accessible and have a lifetime in the ZooKeeper's namespace until they're explicitly deleted.
Promise
2. registerService(serviceOptions) ⇒ registerService register the service to the zookeeper node using the passed options. Examples:
;
Kind: global function
Returns: Promise
- - That resolves to complete service path if the service gets registered or reject with error.
Param | Type | Description |
---|---|---|
serviceOptions | object |
|
serviceOptions.name | string |
name of the service to register as ephemeral znode at basePath+name. |
serviceOptions.port | string |
port number configuration of the microservice to save. |
serviceOptions.protocol | string |
communication protocol [https |
serviceOptions.api | string |
base service api. |
serviceOptions.ip | string |
ip address of the running service. |
serviceOptions.metadata | object |
other metadata of the services line - healthcheck, tags, etc. |
Promise
3. getServiceEndpoints(serviceName) ⇒ getServiceEndpoints returns all endpoint from the list of registered endpoints, given a service-name.
Kind: global function
Returns: Promise
- - that resloves to list of all endpoints
Param | Type | Description |
---|---|---|
serviceName | string |
service name |
Promise
4. getRandomServiceEndPoint(endPointsList, serviceName) ⇒ getRandomServiceEndPoint returns a random endpoint from the list of registered endpoints.
Kind: global function
Returns: Promise
- - That resolves to a randomnly selected endpoint.
Param | Type | Description |
---|---|---|
endPointsList | array |
list of endpoints. |
serviceName | string |
service name. |
Promise
5. getService(endPoint) ⇒ getService return a object containing endpoint and metadata from the registered endpoint.
Kind: global function
Returns: Promise
- - That resolves to a selected endpoint and metadata.
Param | Type | Description |
---|---|---|
endPoint | string |
Full Path of the endPoint |
Promise
6. getAllChildren() ⇒ getAllChildren returns all service registered at the basePath.
Kind: global function
Returns: Promise
- - that resloves to list of all services.
Promise
7. getServiceConfigData(serviceConfigPath) ⇒ getServiceConfigData returns the configuration data for the particular service.
Kind: global function
Returns: Promise
- - that resolves with the config data
Param | Type | Default | Description |
---|---|---|---|
serviceConfigPath | string |
null |
config store path of service |
Promise
8. setServiceConfigData(serviceConfigPath) ⇒ setServiceConfigData sets the configuration data on the particular path.
Kind: global function
Returns: Promise
- - that resolves with stat of the node.
Param | Type | Default | Description |
---|---|---|---|
serviceConfigPath | string |
null |
config store path of service |
Event
The watcher function triggers the follwoing event on the instance of event emitter resolved with connect
function.
Properties
NODE_DELETED
- watched node is deleted.NODE_DATA_CHANGED
- Data of watched node is changed.NODE_CHILDREN_CHANGED
- Children of watched node is changed.
License
MIT <3