Getting started: Installing and initializing Hydra

To use Hydra from another project:

$ npm install hydra

Importing Hydra

const hydra = require('hydra');

Initialization

On import, the Hydra module is loaded but must first be initialized before it can be used.

hydra.init(initObject);

The initialization object consist of the following fields:

{
  serviceName: 'hydramcp',
  serviceDescription: 'Hydra Master Control Program',
  serviceIP: '',
  servicePort: 0,
  serviceType: 'mcp',
  redis: {
    host: '127.0.0.1',
    port: 6379,
    db: 0
  }
}

All of the fields shown are required. However, if your application isn't going to function as a service then the following values can be blank and will be ignored. It's considered a best practice to blank the values if you don't intend for them to be used:

serviceName: '',
serviceDescription: '',
serviceDNS: '',
serviceIP: '',
servicePort: 0,
serviceType: '',

Important:

  • When Hydra is being used in a service, if serviceIP is equal to an empty string (''), then the machine's local IP will be used, otherwise a four segment IP address is expected (52.9.201.160). If servicePort is equal to zero then Hydra will choose a random port. SetservicePortin cases where you need a microservice to use a specific port address.
  • The hydra.serviceDNS entry allows you to specify a service DNS rather than an IP address. This allows you to place your service behind an external load balancer such as Nginx or the internal DNS of a Docker Swarm. When a value is present the serviceDNS entry will ignore the serviceIP field - even if it contains a value.
  • Thehydra.redis.dbvalue must be set to the same value for all network services in a cluster. Not doing this will impact service discoverability and monitoring. The reason a redis database value isn't hardcoded within Hydra is because the number of databases present on a Redis instance isn't guaranteed to be the same across providers. So ultimately the service implementor (you?) needs the flexibility of setting this value and thus bare the responsibility.

In an actual production system the Hydra JSON might be embedded in a larger configuration file such as a properties.js file:

exports.value = {
  appServiceName: 'hydramcp',
  cluster: false,
  environment: 'development',
  maxSockets: 500,
  logPath: '',
  hydra: {
    serviceName: 'hydramcp',
    serviceDescription: 'Hydra Master Control Program',
    serviceVersion: '1.0.0',
    serviceIP: '',
    serviceDNS: '',
    servicePort: 0,
    serviceType: 'mcp',
    serviceWorker: false,
    redis: {
      host: '127.0.0.1',
      port: 6379,
      db: 0
    }
  }
};

When using this approach simply pass the hydra branch during initialization:

hydra.init(config.hydra);

If you are using hydra in the same file it's being initialized in, then you can wait directly on the promise returned byhydra.init()before preceding with other hydra methods.

// index.js
hydra.init({...})
  .then(...);

However, if you are importing your hydra instance from a separate file, you will need to call thehydra.ready()method.

hydra.ready()returns the exact same promise thathydra.init()does, though it does so without re-initializing the hydra instance.

// my-hydra.js
import hydra from 'hydra';

hydra.init({...});
export default hydra;
// service.js
import hydra from './my-hydra.js';

hydra.ready().then(...);

You can usehydra.ready()any time after you have calledhydra.init()to wait for initialization to complete.

results matching ""

    No results matching ""