Hydra Integration Module
Integrating third-party Node.js web frameworks with Hydra, the simplest way.
Hydra is a NodeJS package, which facilitates building distributed applications such as Microservices. (https://www.hydramicroservice.com/)
The magic thing with Hydra is that with a minimum setup, your micro-service get 'superpowers':
- Distributed Computing
- Application Clusters + Load Balancer
- High Availability
- Centralized Proxy Router
- Realtime Messaging
- Service Discovery, Monitoring and Management
- ... and much more (https://www.hydramicroservice.com/)
By using this module, you will be able to easily integrate your prefered web framework with hydra. Just keep reading...
Integrated frameworks
At the moment, the following frameworks are supported:
- Express: Fast, unopinionated, minimalist web framework for Node.js... (http://expressjs.com/)
- Hapi: A rich framework for building applications and services, hapi enables developers to focus on writing reusable application logic instead of spending time building infrastructure... (https://hapijs.com/)
- Koa: Koa is a new web framework designed by the team behind Express, which aims to be a smaller, more expressive, and more robust foundation for web applications and APIs... (http://koajs.com/)
- Sails.js: Sails makes it easy to build custom, enterprise-grade Node.js apps... (http://sailsjs.com/)
- Restify: Restify is a node.js module built specifically to enable you to build correct REST web services... (http://restify.com/)
- Restana: Super fast and minimalist web framework for building REST micro-services... (https://github.com/jkyberneees/ana)
- Native Hydra Service: Hydra services are ideal for making distributed API calls through HTTP or real-time events management... (https://www.hydramicroservice.com/)
Getting Started
Next we will explain you how to create and express based micro-service on top of hydra:
IMPORTANT: Hydra depends on Redis database server for storage, cache and messaging. In case you don't have it yet: https://www.hydramicroservice.com/docs/quick-start/step1.html
- Install dependencies:
npm i hydra hydra-integration express --save
Express is a dependency here because in the next example we use express, it could be koa or hapi, or any of the supported frameworks.
- Create and edit app.js file:
const HydraServiceFactory = HydraServiceFactory;const express = ;const router = express; const factory = hydra: 'serviceName': 'express-service-test' 'serviceDescription': 'Basic express service on top of Hydra' 'serviceIP': '127.0.0.1' 'servicePort': 3000 'serviceType': 'express' 'serviceVersion': '1.0.0' 'redis': 'host': '127.0.0.1' 'port': 6379 'db': 15 ; factory;
Alternative way of getting the service instance by passing config params to the builder (if supported):
factory;
To use hydra-integration as a plugin see: The HydraIntegrationPlugin class
- Run your service:
- Single process:
node app.js
- Optionally you can run multiple processes with PM2:
pm2 start app.js -i 4
- Test your service using a Web browser at: http://localhost:3000/v1/welcome
- Test the service using the hydra-cli (https://www.hydramicroservice.com/docs/tools/hydra-cli/getting-started.html):
- List service routes
hydra-cli routes express-service-test
- Invoke the [GET]/v1/welcome service endpoint:
hydra-cli rest express-service-test:[GET]/v1/welcome
Demos
Demos available into demos folder on the git repository: https://github.com/jkyberneees/hydra-integration
Next Topics
- The HydraServiceFactory class
- The HydraIntegrationPlugin class
- Express Framework Integration
- Hapi Framework Integration
- Koa Framework Integration
- Sails(0.x) Framework Integration
- Restify Framework Integration
- Restana Framework Integration
- Native Hydra Integration
- Creating your own integration
- Reference documentation