@apifie/node-microservice v1.0.3
node-microservice NPM Module
Please refer to our Project Wiki for a very detailed documentation and examples.
node-microservice is an aggregation of industry standard best practices and design patterns for creating micro-services using Node.js.
It is a node module (npm) that gives a standard blueprint for creating new micro-services in Node.js. This blueprint attempts to bring in industry standard design patterns relevant and required for a micro-services based architecture. To implement these patterns, we typically need to integrate with a lot of tools and services from Infrastructure and Platform. This blueprint takes a opinionated view on selection of these tools and offers these integrations out-of-the-box. A list of these tools and services pre-integrated in node-microservice are as below
- Redis 4.0+ : It is used as a caching server.
- MySQL 5.6+ : It is used as a persistent data store Database schema per service pattern.
- Rabbit MQ 3.6.12+ : It is used to support Asynchronous inter-service communication pattern.
- Winston 2.4+ and winston-logstash 0.4+ : It is used to support upstream aggregation of application logs to Platform level monitoring systems like ELK, Splunk etc.
- Zipkin : Used to support traceability of requests across different services, including async flows
- Axios 0.18+ : To manage HTTP requests and responses
- Swagger specs : To automatically swagger specifications for all HTTP routes published by your service. This is further helpful in upstream integration with Application gateways
What it does (features-list)
Read it on our detailed Wiki. In addition to above out-of-the-box integrations, this blueprint also provides in-built solutions for some of the most common and frustrating issues that developers have faced while working on micro-services architecture and its underlying complexities. Some of the key solutions offered out-of-box are listed below.
Database (ORM)
- Manage Database schema migrations
- Inject SSL certificates for DB connection (if required)
- Pre-Integrated with Sequelize
- Automated reconnection to DB server in case of intermittent connection failures
Message Queue (Async communication)
- Automated creation / binding to message exchanges and routing keys to service queues
- Scoping Message queues to specific service-name and version (to support multiple versions of services)
- Auto retry of failed messages on MQ (with or without a delay)
- Managing MQ failures by posting all incidents on a dedicated queue (for manual recover options)
- Automated reconnection to MQ server in case of intermittent connection failures
Caching
- Scoping of all cache keys to service-name and version
- Support for asynchronous read and write from cache
- Support for expiring keys after a set interval
- Automated reconnection to Redis server in case of intermittent connection failures
Traceability and logging
- Automatic addition of a unique transactionId to all incoming (HTTP and MQ) requests
- Chaining of this transactionId to all subsequent internal, inter-service calls (Both Sync and Async, including MQ interfaces)
- Injection of this transactionId in all application log messages
- Injection of a calculated
responseTime
header in all API (HTTP and MQ) responses - Injection of serviceName and serviceVersion information in response headers
- Pushing all application logs to a logstash agent for upstream integration with tools like ELK, Splunk etc.
- Automated reconnection to logstash in case of intermittent connection failures
HTTP request
- Enforce standard REST routes
- Inject standard middleware components like ErrorHandlers, BodyParsers, application Routes
- Inject specific middleware components like express and zipkin
- Ensure compliance to specific headers for supporting traceability (See above)
- Automated generation of Swagger specification for all HTTP REST routes exposed by the service
- Automated generation of Postman scripts for all HTTP REST routes exposed by the service
Testing and Quality Assurance
- In-built support for Chai, Mocha and Proxyquire
- A fully functional test harness, and helper function to kick start unit and integration test cases
- Generation of standard code coverage report
- Enforce compliance to standard coding practices (using ESLint)
- Generation of standard sonarqube reports
Health check / Monitoring
- Out-of-box HTTPS routes to support automated health monitoring in a clustered / cloud setup and upstream integration with an Alert system
- Ability to check health of all downstream components (Database, Cache, MQ etc) without getting in the service box (server / vm)
We maintain a very detailed documentation with a lot of examples and use cases. Please refer to to node-microservice wiki page
Installation
Prerequisites: Node.js (>=8.3). node-microservice is available on npm. To install it, type:
$ npm install @apifie/node-microservice
Usage
Read it on our detailed Wiki
We need to create a micro-service that uses this module as its dependency. The simplest way to start your new micro-service is to fork / clone one our sample service and start adding your business logic
At a very basic level, there are three steps.
1. Configure you service and define which components to use and what are different server end-points (like MySQL, redis etc.)
Configuration guideline
External component setup
2. Import @apifie/node-microservice module in your service. Preferably, do this at the very start of your service (index.js
)
const {
logger,
errors,
serviceBootstrapper,
router,
swagger,
getTracerClient,
getCacheClient,
getSqlDBClient,
getMqClient,
getMiddleware,
getApiClient,
getUtils,
eventBus
} = require('@apifie/node-microservice')
- Call Service bootstrap helper function (async operation)
async function bootstrapService (app) {
logger.info('Requesting service bootstrap for your app')
resgisterBootstrapperEvents()
try {
apifiedApp = await serviceBootstrapper(serviceSetup, mqListeners, app)
return apifiedApp
} catch (err) {
logger.error('Failed in Bootstrapping your service %s ', err)
throw err
}
}
Once above steps are executed, consuming different capabilities is all about calling available methods on APIs exposed by this module. An example for cache would be as below
await getCacheClient().setRedisKey('k1', 'v1')
const val = await getCacheClient().getRedisKey('k1')
Testing the module
npm test
npm run test:integration
For a detailed documentation on test harness, bootstrapping and support for different types of testing (Unit, Integration, Flow etc.), you can refer to to node-microservice wiki page
Component API Specification
Here is a quick reference to our detailed documentation to help and support you in using node-microservice
- What is
node-microservice
- Features and capabilities
Configuration guideline
- External component setup
- Getting started guide
- API and service Specification
- Testing your service
- How to guides
- How to use logger
- How to use tracer
- How to add a new model to database
- How to manage database migrations
- How to add service specific middleware request / response interceptors
- How to add service specific api routes
- How to call external (HTTP / HTTPS) apis
- How to generate Swagger Specs
- How to generate Postman collection
- Understanding technical design
- Best practices and guidelines
- How to contribute
- How to report an Issue