Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented round-robin load balancing in the reverse proxy.
- Loading branch information
1 parent
140f70f
commit 8915d3a
Showing
7 changed files
with
217 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
rapidoid-http-server/src/main/java/org/rapidoid/reverseproxy/ProxyMapping.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.rapidoid.reverseproxy; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-http-server | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.http.Req; | ||
|
||
import java.util.List; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.2.0") | ||
public class ProxyMapping extends RapidoidThing { | ||
|
||
private final String prefix; | ||
|
||
private final List<String> targets; | ||
|
||
private volatile LoadBalancer loadBalancer = new RoundRobinLoadBalancer(this); | ||
|
||
public ProxyMapping(String prefix, List<String> targets) { | ||
this.prefix = prefix; | ||
this.targets = targets; | ||
} | ||
|
||
public String prefix() { | ||
return prefix; | ||
} | ||
|
||
public List<String> targets() { | ||
return targets; | ||
} | ||
|
||
public boolean matches(Req req) { | ||
return req.path().startsWith(prefix); | ||
} | ||
|
||
public String getTargetUrl(Req req) { | ||
return loadBalancer.getTargetUrl(req); | ||
} | ||
|
||
public LoadBalancer loadBalancer() { | ||
return loadBalancer; | ||
} | ||
|
||
public ProxyMapping loadBalancer(LoadBalancer loadBalancer) { | ||
this.loadBalancer = loadBalancer; | ||
return this; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
rapidoid-http-server/src/main/java/org/rapidoid/reverseproxy/Reverse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.rapidoid.reverseproxy; | ||
|
||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.setup.On; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-http-server | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.2.0") | ||
public class Reverse extends RapidoidThing { | ||
|
||
private static volatile ReverseProxy DEFAULT_PROXY; | ||
|
||
public static synchronized ReverseProxy proxy() { | ||
if (DEFAULT_PROXY == null) { | ||
DEFAULT_PROXY = newProxy(); | ||
On.req(DEFAULT_PROXY); | ||
} | ||
|
||
return DEFAULT_PROXY; | ||
} | ||
|
||
public static ReverseProxy newProxy() { | ||
return new ReverseProxy(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
rapidoid-http-server/src/main/java/org/rapidoid/reverseproxy/RoundRobinLoadBalancer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.rapidoid.reverseproxy; | ||
|
||
/* | ||
* #%L | ||
* rapidoid-http-server | ||
* %% | ||
* Copyright (C) 2014 - 2016 Nikolche Mihajlovski and contributors | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.rapidoid.RapidoidThing; | ||
import org.rapidoid.annotation.Authors; | ||
import org.rapidoid.annotation.Since; | ||
import org.rapidoid.commons.Str; | ||
import org.rapidoid.http.Req; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
@Authors("Nikolche Mihajlovski") | ||
@Since("5.2.0") | ||
public class RoundRobinLoadBalancer extends RapidoidThing implements LoadBalancer { | ||
|
||
private final ProxyMapping mapping; | ||
|
||
private final AtomicLong counter = new AtomicLong(); | ||
|
||
public RoundRobinLoadBalancer(ProxyMapping mapping) { | ||
this.mapping = mapping; | ||
} | ||
|
||
@Override | ||
public String getTargetUrl(Req req) { | ||
long n = counter.incrementAndGet(); | ||
|
||
List<String> targets = mapping.targets(); | ||
int index = (int) (n % targets.size()); | ||
String target = targets.get(index); | ||
|
||
String trimmed = Str.triml(req.uri(), mapping.prefix()); | ||
return target + trimmed; | ||
} | ||
|
||
} |