Node.js api gateway app with internal loadbalancing for isntances in each service and basic authorization (user based & token based).
pull the project and get the api gw started by running:
npm run dev
Now navigate to one of the workspaces (behaving as our Frontend) and start them as well (at least one)
node server.js
right after running the service, the routes/registery.json will be updated with your service.
In order to enable the service, run: (where bWFqZDpwYXNzd29yZA== is base64 encoding of the majd:password as username:password pair)
curl -X POST -H 'Content-Type:application/json' -H 'authorization:bWFqZDpwYXNzd29yZA==' -d '{"url":"http://localhost:3003/","enabled":true}' http://localhost:3000/enable-instance/staging
enabling / disabling an instance in a service is done for times such as maintanance, server down, nightly shutdown etc, in those cases, the api gw should not use the instances
In order to disable the service, run: (where bWFqZDpwYXNzd29yZA== is base64 encoding of the majd:password as username:password pair)
curl -X POST -H 'Content-Type:application/json' -H 'authorization:bWFqZDpwYXNzd29yZA==' -d '{"url":"http://localhost:3003/","enabled":false}' http://localhost:3000/enable-instance/staging
Notice the change in routes/registery.json file.
Now if you have many instances under the service. You can enjoy the loadbalancing!
Lets say you have 2 instances enabled under a service in your routes/registery.json file.
curl -X POST -H 'Content-Type:application/json' -H 'authorization:bWFqZDpwYXNzd29yZA==' http://localhost:3000/staging/fakeapi
curl -X POST -H 'Content-Type:application/json' -H 'authorization:bWFqZDpwYXNzd29yZA==' http://localhost:3000/staging/fakeapi
You will have these results (in the GW terminal, not the service) - showing the effect of the loadbalancing:
username: majd | password: password
Authorized!
url: http://localhost:3003/ <----------- Notice the port
username: majd | password: password
Authorized!
url: http://localhost:3004/ <----------- Notice the port
meaning we're now scaled horizontally and have a loadbalancer - we can have the instances on different hosts as well
If you disable one instance from the instances above and re run the curl command, you'll only have 1 instance serving.