Node discovery service with built-in support for automatic single master and capability advertising.
Automatic and decentralized discovery and monitoring of nodejs instances with built in support for a variable number of master processes, service advertising and channel messaging.
Probably has bugs.
So, you have a whole bunch of node processes running but you have no way within each process to determine where the other processes are or what they can do. This module aims to make discovery of new processes as simple as possible. Additionally, what if you want one process to be in charge of a cluster of processes? This module also has automatic master process selection.
This module uses broadcast and multicast features from node's dgram module. Additionally, this module depends on changes to the dgram module in 0.10. Therefore, this module is compatible with Node
var Discovery = require'node-discovery';var d = ;don"promotion"/*** Launch things this master process should do.** For example:* - Monitior your redis servers and handle failover by issuing slaveof* commands then notify other node instances to use the new master* - Make sure there are a certain number of nodes in the cluster and* launch new ones if there are not enough* - whatever*/console.log"I was promoted to a master.";;don"demotion"/*** End all master specific functions or whatever you might like.*/console.log"I was demoted from being a master.";;don"added"console.log"A new node has been added.";;don"removed"console.log"A node has been removed.";;don"master"/*** A new master process has been selected** Things we might want to do:* - Review what the new master is advertising use its services* - Kill all connections to the old master*/console.log"A new master is in control";;
npm install node-discovery
git clone git://github.com/skeggse/node-discovery.git
Any of the following will run the tests:
node-discover/$ mochanode-discover/$ npm testnode-discover/$ make test
helloInterval: 1000 // How often to broadcast a hello packet in millisecondscheckInterval: 2000 // How often to to check for missing nodes in millisecondsnodeTimeout: 2000 // Consider a node dead if not seen in this many millisecondsmasterTimeout: 2000 // Consider a master node dead if not seen in this many millisecondsaddress: '0.0.0.0' // Address to bind toport: 12345 // Port on which to bind and communicate with other node-discovery processesbroadcast: '255.255.255.255' // Broadcast address if using broadcastmulticast: null // Multicast address if using multicast (don't use multicast, use broadcast)mulitcastTTL: 1 // Multicast TTL for when using multicastkey: null // Encryption key if your broadcast packets should be encrypted (null means no encryption)mastersRequired: 1 // The count of master processes that should always be availableweight: Mathrandom // A number used to determine the preference for a specific process to become master. Higher numbers win.;
Promote the instance to master.
This causes the old master to demote.
var Discovery = require'node-discovery';var d = ;dpromote;
Demote the instance from being a master. Optionally pass true to demote to specify that this node should not automatically become master again.
This causes another node to become master
var Discovery = require'node-discovery';var d = ;// different usagesddemote; // this node is still eligible to become a master node.ddemotetrue; // this node is no longer eligible to become a master node.
Join a channel on which to receive messages/objects
var Discovery = require'node-discovery';var d = ;// pass the channel and the callback function for handling received data from that channelvar success = djoin"config-updates"if dataredisMaster// connect to the new redis master;if !success// could not join that channel; probably because it is reserved
Leave a channel
var Discovery = require'node-discovery';var d = ;// pass the channel which we want to leavevar success = dleave"config-updates";if !success// could leave channel; who cares?
Send a message/object on a specific channel
var Discovery = require'node-discovery';var d = ;var success = dsend"config-updates" redisMaster : "10.0.1.4";if !succes// could not send on that channel; probably because it is reserved
Advertise an object or message with each hello packet; this is completely arbitrary. make this object/message whatever you applies to your application that you want your nodes to know about the other nodes.
var Discovery = require'node-discovery';var d = ;// any of these invocationsdadvertiselocalServices :type: 'http' port: '9911' description: 'my awesome http server'type: 'smtp' port: '25' description: 'smtp server';dadvertise"i love nodejs";dadvertisesomething: "something";
Start broadcasting hello packets and checking for missing nodes (start is called automatically in the constructor)
var Discovery = require'node-discovery';var d = ;dstart;
Stop broadcasting hello packets and checking for missing nodes
var Discovery = require'node-discovery';var d = ;dstop;
Each event is passed the
Node Object for which the event is occuring.
Triggered when the node has been promoted to a master.
Triggered when the node is no longer a master.
Triggered when a new node is discovered.
Triggered when a new node is not heard from within
Triggered when a new master has been selected.
isMaster: trueisMasterEligible: trueinfo: nulllastSeen: 1317323922551address: '10.0.0.1'port: 12345id: '31d39c91d4dfd7cdaa56738de8240bc4'hostName: 'myMachine'
Copyright © 2011 Dan VerWeire firstname.lastname@example.org
Copyright © 2013 Eli Skeggs email@example.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.