msdr-zk
v1.1.4
Published
"discover service or register service for micro-services environment using zookeeper"
Downloads
10
Maintainers
Readme
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.connect({
connectionURL: "localhost:2181",
basePath: "/services/endpoints/test"
});
let msdzkConnection;
msdzkP.then(conn => {
msdzkConnection = conn;
msdzkConnection.on("zk-disconnected", () => {
// listen for zk-disconnected event, and do the clean up.
console.log("zk-disconnected stopping the process");
process.exit(1);
});
// watch for event's
msdzkConnection.on("NODE_CHILDREN_CHANGED", e => {
console.log("Node Children changed on the path");
console.log(e);
});
});
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 service
msdzk.registerService(registerServiceParam).then(_p => {
console.log("service registered at " + _p);
});
//allows service to register and then check
setTimeout(() => {
msdzk.getServiceEndpoints("service-name").then(arr => {
console.log(arr);
msdzk.getRandomServiceEndPoint(arr, "service-name").then(endpoint => {
console.log(endpoint);
});
});
}, 2000);
More Examples in examples folder.
Api.
1. connect(connectionOptions) ⇒ Promise
connect Initiate the connection to the provided server list (ensemble). Examples:
connect({
connectionURL: "localhost:2181",
basePath: "/services/endpoints/test"
});
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.
2. registerService(serviceOptions) ⇒ Promise
registerService register the service to the zookeeper node using the passed options. Examples:
registerService({
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"]
}
}
});
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 | | http]. | | 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. |
3. getServiceEndpoints(serviceName) ⇒ Promise
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 |
4. getRandomServiceEndPoint(endPointsList, serviceName) ⇒ Promise
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. |
5. getService(endPoint) ⇒ Promise
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 |
6. getAllChildren() ⇒ Promise
getAllChildren returns all service registered at the basePath.
Kind: global function
Returns: Promise - - that resloves to list of all services.
7. getServiceConfigData(serviceConfigPath) ⇒ Promise
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 |
8. setServiceConfigData(serviceConfigPath) ⇒ Promise
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