Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

how to achieve app level load balancing? #107

Closed
MrSpark2591 opened this issue Mar 22, 2017 · 8 comments
Closed

how to achieve app level load balancing? #107

MrSpark2591 opened this issue Mar 22, 2017 · 8 comments
Labels

Comments

@MrSpark2591
Copy link

MrSpark2591 commented Mar 22, 2017

Thanks for such wonderful proxy module. I am using this module for our gateway implementation and it works really well. but I am stuck in a situation.
Suppose there is one application named XYZ and it contains APIs lets say api1, api2, api3.
so I want to implement something like this.
proxy .all('\XYZ\*') .balance(serverList)
so this will be used for load balancing.
and can also register individual APIs as:
proxy .get('\XYZ\api1') .useForward(middleware)
for custom logic on individual API.

how can I achieve this?

@h2non h2non added the question label Mar 22, 2017
@tomas-fp
Copy link

  • You can't combine multiple routes with different behaviours at rocky API level (you can achieve that, however, writing your own middleware function).
  • You must declare the specific route first.
  • Then declare the generic one with the wildcard.

You code might look like:

proxy.get('/XYZ/api1/*') .balance(serverList).useForward(middleware)
// GET /XYZ/api1/* HTTP requests won't never reach the route below:
proxy.all('/XYZ/*') .balance(serverList)

@MrSpark2591
Copy link
Author

but here if we define balance at individual API, Case will be:
take a scenario I have app. XYZ which has two API api1, api2.
whenever I receive request for any of these I have to load balance on application level not API level.
example: (suppose XYZ has two instances running xyz1 , xyz2)
in case of proxy.all('/XYZ/*') .balance(serverList)
request sequence:
api1 -> hits xyz1
api2 -> hits xyz2
api1 -> hits xyz1
api2 -> hits xyz2
in the case of individual load balance:
api1 -> hits xyz1
api2 -> hits xyz1
api1 -> hits xyz2
api2 -> hits xyz2

i want 1st case not second

@tomas-fp
Copy link

The just register multiple routes that you want to balance independently:

proxy.all('/XYZ/api1/*') .balance(serverList)
proxy.all('/XYZ/api2/*') .balance(serverList2)

@MrSpark2591
Copy link
Author

this is what i am doing right now which leads

//here server list is xyz1.com, xyz2.com
proxy.all('/XYZ/api1/*') .balance(serverList)
proxy.all('/XYZ/api2/*') .balance(serverList)

which leads :
api1 -> xyz1
api2 -> again xyz1 but i want it to go on xyz2 because it should load balace on application level
api1 -> xyz2 // even if this is 3rd hit to same application because of api level balancing it will go to xyz2
and so on.

@tomas-fp
Copy link

The current, pretty simple, balancer implementation relies on object mutability. This is not good. In order to fix it from package consumer perspective, you can do:

proxy.all('/XYZ/api1/*') .balance(Array.from(serverList))
proxy.all('/XYZ/api2/*') .balance(Array.from(serverList))

@MrSpark2591
Copy link
Author

thank you, I tried that but not working as well. can we change target URL in middleware ?

@tomas-fp
Copy link

Yes, you can change whatever you want via middleware. See:
https://github.com/h2non/rocky#request

@MrSpark2591
Copy link
Author

MrSpark2591 commented Mar 22, 2017

woha , i am your fan now. Thank you so much 💃
implemented manual round robin on server array using middleware. 💯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants