sg-service v0.0.7
sg-service
A Javascript service manager work with node-zookeeper-client and grpc for Node.js.
This module is not ready for product.
Installation
You can install it using npm:
$ npm install sg-service
Example
1. Create a service manager and add two services
let SgService = require("sg-service");
let serviceMgr = new SgService({
appName: "app",
zookeeperConStr: "192.168.73.141:2181,192.168.73.142:2181,192.168.73.143:2181"
});
serviceMgr.addServer("math", {
async multiPlus(a, b, c) {
return a+b+c;
}
});
serviceMgr.addServer("array", {
async toArray(a, b, c) {
return [a,b,c];
}
});
serviceMgr.start();
process.on('SIGINT', () => {
serviceMgr.shutdown();
process.exit();
});
2. Create a client and use the service
let SgService = require("sg-service");
let serviceMgr = new SgService({
appName: "app",
zookeeperConStr: "192.168.73.141:2181,192.168.73.142:2181,192.168.73.143:2181",
});
serviceMgr.start();
serviceMgr.registerService(["math", "array"]);
let mathService = serviceMgr.getService("math");
let arrayService = serviceMgr.getService("array");
setTimeout(() => { // there is not service ready event yet, use setTimeout instead
console.log("mathtest", mathService.call(null, "multiPlus", 1, 2, 3)); // mathtest 6
console.log("arraytest", arrayService.call(null, "join", 1, 2, 3)); // mathtest [1,2,3]
}, 2000);
More examples is not ready.
Documentation
SgService(options)
Class function to create a service manager.
Arguments
options
Object
- An object to set the manager options. Currently available options are:- appName
string
- Root node name of zookeeper. - zookeeperConStr
string
- Comma separatedhost:port
pairs, each represents a ZooKeeper server. - myRpcIP
string
- IPv4 addr for grpc connection, default is the IP address of network.
- appName
Example
let serviceMgr = new SgService({
appName: "app",
zookeeperConStr: "192.168.73.141:2181,192.168.73.142:2181,192.168.73.143:2181",
});
SgService
void start()
Start the service, connect to zookeeper.
void shutdown()
Stop the service, disconnect from zookeeper, close grpc connections.
void addServer(serviceName, handler)
Add a service server on this node process.
Argument
- serviceName
string
- Service name to serve - handler
Object.<string,function>
- Service function object
Example
serviceMgr.addServer("math", {
async multiPlus(a, b, c) {
return a+b+c;
}
});
ServiceServer getServer(serviceName)
Get a service server.
Argument
- serviceName
string
- Service name
void registerService(serviceNames)
Register Services to use, zookeeper will watch the service node and synchronize the detail services nodes to local cache.
Arguments
- serviceNames
Array.<string>
- Array of service names to regist.
Example
serviceMgr.registerService(["math", "array"]);
Service getService(serviceName)
get Service by name.
Arguments
- serviceName
string
- Service name.
Example
let mathService = serviceMgr.getService("math");
Example
let mathServer = serviceMgr.getServer("math");
ServiceServer
A service server with some functions
setData(data)
Set data to the service server in this service manager, it can be get by service node in the service.
Arguments
- data
*
- Some data
Example
mathServer.setData({burn:100});
Service
A service client.
call(node, method, ...args)
Call remote function provided by service servers that added to service manager.
Returns the result of the service function.
Arguments
- node
ServiceNode
- null to random one, see ServiceNode - method
string
- Function name to call - ...args
*
- Any arguments, not support Function type.
Example
let result = mathService.call(null, "multiPlus", 1, 2, 3);
console.log("result is", result);
Array. getNodes()
Get the nodes in the services.
Returns the serviceNodes list, see ServiceNode
Example
let mathServiceNodes = mathService.getNodes();
string getName()
Get the name of service.
Example
let name = mathService.getName();
console.log("service name is", name);
ServiceNode
A node in a service, connected to a grpc server of the service server.
call(method, ...args)
Call remote function provided by service servers that added to service manager.
Returns the result of the service function.
Arguments
- method
string
- Function name to call - ...args
*
- Any arguments, not support Function type.
Example
let serviceNode = mathServiceNodes[0];
let result = serviceNode.call("multiPlus", 1, 2, 3);
console.log("result is", result);
getData()
Returns the data set by service server.
Example
let serviceNode = mathServiceNodes[0];
console.log("service node data is", serviceNode.getData());