Skip to content

NATS - Microservice auto registering, discovery and messaging #17

@alphonse92

Description

@alphonse92

We need a way to register each microservice instead of a static registration. Also, we need to check the health of each microservice without using HTTP because it could get into a conflict with some microservice routes. Also, we need to create a way to add microservices to the ecosystem dynamically, sure, with a security basis.

In order to it, we need to add to the microservice ecosystem NATS. Also, we need to specify a protocol, interface o some way to:

  1. Microservice register on startup.
  2. The gateway must check the availability of a microservice using NATS, for this, the gateway must have a table (in memory) with the currently registered microservices. If a request target to a microservice that is not registered in the table, then the Gateway must check the availability sending a message with NATS. If the microservice respond, the gateway adds the microservice to the table.

Security

First of all, the gateway is the only microservices that is enabled to manage the microservice routing. In order to keep a secure flow, we need to update the docker-compose yaml file to provides a unique JWT token, or a common secret. The Gateway must have a Secret always (it is a mandatory), this secret will be used to generate new tokens to new Microservices.

Requirements to register to the ecosystem

  1. Each microservice must have or a JWT token or a secret, otherwise the microservice will not be registered.
  2. The gateway Must Have a secret. If it does not exist, the gateway CANT START.

Way to register a microservice

  1. At startup: Each microservice send a message with the 'secret' or the JWT token, and its name to the gateway. If MS sends a Secret, the gateway validates the secret and send a token. If the MS sends a token, the gateway must validate it, and if the token or the secret is valid, then Gateway must add it to the Microservices table

  2. a request to unknow microservices arrives: It could be happend if that microservice was not registered in the microservices table. Then, the Gateway must send a message to identify (PING), if it was not resolved (because the microservice doest not exist, or it is not properly configured), send a 404. Otherwise, if the microservice exists and it is properly configured, the microservice will send the token or the secret, and it will be registered as above.

Messages namming

"gateway:ping"

  • Listen by: children microservices
  • Sended by: gateway

is a gateway message to discovery new microservices.

"gateway:login"

  • Listen By: gateway
  • Sended by: microservices that want to register to the ecosystem
  • Data: {name:'myMs', token?:'tokenOrSecret',secret:?'tokenOrSecret' } , token or secret must be sent.

add myMs to the microservice table

"gateway:logout"

  • Listen By: gateway
  • Sended by: microservices that want to be removed from the ecosystem
  • Data: {name:'myMs', token?:'tokenOrSecret',secret:?'tokenOrSecret' }, token or secret must be sent.

remove myMs from the microservice table

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions