-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
103 additions
and
8 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
63 changes: 63 additions & 0 deletions
63
core-examples/src/main/java/io/vertx/example/core/http/loadbalancing/Client.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,63 @@ | ||
package io.vertx.example.core.http.loadbalancing; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.VerticleBase; | ||
import io.vertx.core.http.HttpClient; | ||
import io.vertx.core.http.RequestOptions; | ||
import io.vertx.core.net.Address; | ||
import io.vertx.core.net.AddressResolver; | ||
import io.vertx.core.net.SocketAddress; | ||
import io.vertx.core.net.endpoint.LoadBalancer; | ||
import io.vertx.launcher.application.VertxApplication; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">Julien Viet</a> | ||
*/ | ||
public class Client extends VerticleBase { | ||
|
||
// This resolver simply response localhost:808x on a service.com:80 access | ||
// we use such resolver here instead of DNS load balancing because there is no way to provide a local example | ||
// with DNS load balancing that requires multiple network interfaces | ||
private static final AddressResolver resolver = AddressResolver.mappingResolver(Collections | ||
.<Address, List<SocketAddress>>singletonMap(SocketAddress.inetSocketAddress(80, "service.com"), | ||
Arrays.asList( | ||
SocketAddress.inetSocketAddress(8080, "localhost"), | ||
SocketAddress.inetSocketAddress(8081, "localhost"), | ||
SocketAddress.inetSocketAddress(8082, "localhost")))::get); | ||
|
||
public static void main(String[] args) { | ||
VertxApplication.main(new String[]{Client.class.getName()}); | ||
} | ||
|
||
private HttpClient client; | ||
|
||
@Override | ||
public Future<?> start() throws Exception { | ||
|
||
// Load balancer of your choice | ||
LoadBalancer loadBalancer = LoadBalancer.RANDOM; | ||
|
||
client = vertx.httpClientBuilder() | ||
// In reality, we could avoid using such mock resolver and the client instead could use a server list provided by a DNS server | ||
.withAddressResolver(resolver) | ||
.withLoadBalancer(loadBalancer) | ||
.build(); | ||
|
||
return client | ||
.request(new RequestOptions().setServer(SocketAddress.inetSocketAddress(80, "service.com"))) | ||
.compose(request -> { | ||
System.out.println("Interacting with server " + request.connection().remoteAddress()); | ||
return request | ||
.send() | ||
.compose(resp -> { | ||
System.out.println("Got response " + resp.statusCode()); | ||
return resp.body(); | ||
}); | ||
}) | ||
.onSuccess(body -> System.out.println("Got data " + body.toString("ISO-8859-1"))); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
core-examples/src/main/java/io/vertx/example/core/http/loadbalancing/Servers.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,15 @@ | ||
package io.vertx.example.core.http.loadbalancing; | ||
|
||
import io.vertx.core.Vertx; | ||
import io.vertx.example.core.http.simple.Server; | ||
|
||
public class Servers { | ||
|
||
public static void main(String[] args) { | ||
Vertx vertx = Vertx.vertx(); | ||
vertx.deployVerticle(new Server(8080)).await(); | ||
vertx.deployVerticle(new Server(8081)).await(); | ||
vertx.deployVerticle(new Server(8082)).await(); | ||
System.out.println("Servers listening on ports 8080/8081/8082"); | ||
} | ||
} |
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
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