pnxtech / hydra

A light-weight library for building distributed applications such as microservices
https://www.hydramicroservice.com
MIT License
645 stars 54 forks source link

Improve hydra initialization process #128

Closed cjus closed 7 years ago

cjus commented 7 years ago

This PR seeks to improve the initialization process by offering an additional way of initializing hydra beyond the use of a configuration file and object.

The init function currently accepts a config parameter which can be an object or string. If it's a string then the string is expected to point to a file path for a configuration file. If the config parameter is an object then hydra will attempt to use the object members for initialization.

This PR adds the possibility of passing a config parameter of null or undefined. Doing so will initiate the following flow:

"hello-service:1.2.3"

or:

"serviceName=hello-service|serviceDescription=A service that says hello|serviceIP=127.0.0.1|servicePort=5000|serviceType=greeter"

The first format is what we already use when working with Docker containers. The second format represents a string containing pipe delimited | key/value pairs. The key/value pairs are separated by the = symbol.

The string is parsed and an internal config object is created.

This allows a service to be started at the command line using:

$ HYDRA_SERVICE="serviceName=hello-service|serviceDescription=A service that says hello|serviceIP=127.0.0.1|servicePort=5000|serviceType=greeter|serviceVersion=0.1.3" HYDRA_REDIS_URL=redis://localhost:6379/15" node test.js

This allow allows for starting a Docker container and passing the environment variables:

$ sudo docker service create \
 --name hydra-router \
 --network servicenet \
 --restart-condition any \
 --restart-max-attempts 5 \
 --update-delay 10s \
 --constraint=node.role==manager \
 --env HYDRA_REDIS_URL="redis://10.0.0.154:6379/15" \
 --env HYDRA_SERVICE="serviceName=hydra-router|serviceDescription=A service-aware router|serviceIP=|servicePort=80|serviceType=router|serviceVersion1.3.11" HYDRA_REDIS_URL="redis://localhost:6379/15"" \
 --publish 80:80 \
 --replicas=3 \
 flywheelsports/hydra-router:1.3.11

A side effect of this PR is that creating test services locally does not require a config file. Only serviceName and servicePort need to be defined.

cjus commented 7 years ago

@emadum The service user will definitely need to ensure that they don't use | and = inside the values they provide. I think that's reasonable. I'll update this PR to support parsing JSON as well - that's a nice addition!