Simplified nodejs microservices architecture. Create a registry service (Chain) and register services (Links) to build a collection of dynamic, disributed services.
- Register links with chain
- Clients query chain for list of available links
- Clients query links directly
+--------------+--------------+---------------------+
| | | |
| | | |
+----+-----+ +----+-----+ +----+-----+ |
| | | | | | |
| Link | | Link | | Link | |
| | | | | | |
+----+-----+ +--+-------+ +------+---+ |
^ ^ ^ |
| | | |
| | | ^
| | | +---------+-------+
| | | | |
| | | | |
| | | | |
| | | | Chain |
| | | | |
| | | | |
| | | +---------+-------+
| | | ^
+--+------------+ +-----------+---+ |
| | | | |
| | | | |
| Client 1 | | Client 2 +---------------+
| | | | |
| | | | |
+-------+-------+ +---------------+ |
| |
| |
+----------------------------------------------+
Use chainlink.createChain()
to create the chain.
let chain = chainlink.createChain(/* options */);
Use chain.start()
to start the chain. This method will initiate an http server and expose three routes POST /register
, POST /unregister
, GET /links
.
chain.start()
Use chain.stop()
to stop the chain. This will stop the http server and unregister all links.
chain.stop()
To create a link
use the constructor and pass in a port.
let link = chainlink.createLink(3000);
Use link.register
to register the link and pass in details of the chain you want to register with.
link.register({
port: /* port of running chain */
hostname: /* hostname of running */
path: /* registration path */
});
Use link.addRoute(route, action)
to add a route to the link. When a request is made to the route, the action will be performed.
link.addRoute('/home', (req, res) => {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.write('Hello, World!');
res.end()
});
Use link.stop()
to stop the running link and unregister it from the chain.
link.stop();