Hydra

Let's get back to Express. As you've seen before, it's a rock-solid layer on top of the http module. Although it adds an important base layer in the somewhat raw module, it still lacks many features you need to make a good microservice.

As there are lots of plugins out there to extend Express, it can be hard to pick a useful list for us to use.

After picking the right list, you'll still need to make other decisions:

  • How can I distribute my service with multiple instances?
  • How can the service be discovered?
  • How can I monitor whether my service is running properly?

Enter Hydra, a framework that facilitates building distributed microservices. Hydra leverages the power of Express and helps you create microservices or communicate with microservices.

It will, out of the box, enable you to:

  • Do service registration and service discovery, allowing your microservices to discover and be discoverable
  • Communicate with microservices and load balance communication between multiple instances, taking care of failed instances and automatically rerouting requests to other running instances
  • Monitor instances, checking whether the microservice is available and operating normally

Unlike the other modules we've reviewed so far, Hydra has a dependency that is not installable directly using the NPM. Hydra uses Redis to accomplish its goal. Look for information on the Redis website at https://redis.io/ to install it on your operating system before continuing. If you have macOS and use Homebrew, type the following to install redis:

Now, let's make sure redis has successfully started:

After that, we need to install Hydra command-line tools:

sudo npm install -g yo generator-fwsp-hydra hydra-cli

We now need to configure the connection to Redis. We do this by creating a configuration. Type in the command and follow the instructions. If you installed it locally (or used the preceding instructions), you should answer something similar to the following screenshot:

Now, let's create a very simple microservice, just to see what the workflow is like. Hydra has a scaffolding tool using yeoman. To create a service, type the following command and follow the instructions:

On the name of the service, just type hello. Just hit Enter to the rest of the questions to use the defaults. In the end, enter the folder that was created and install the dependencies:

The service is now ready to start. You might have already seen the instructions when scaffolding the service. Let's start the service:

As we can see from the preceding screenshot, the service has started and has been attached to a local IP (192.168.1.108) and port (45394). Open up the folder in your code editor:

You'll see a file in the base folder called hello-service.js, which has the service routes inside it. You'll find the /v1/hello route, which points to another file in routes/hello-v1-routes.js:

Inside that file, you'll see the response to that route. Now, let's jump to a web browser and see if it's up and running:

What we saw in the file is inside the result property of the JSON response. We just deployed our first Hydra microservice without writing a single line of code!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset