Skip to content

Commit

Permalink
HTTP client load balancing example
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 14, 2024
1 parent db7f8aa commit bea1370
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 8 deletions.
7 changes: 7 additions & 0 deletions core-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ And a simple HTTP client which makes a request to the server.

link:src/main/java/io/vertx/example/core/http/simple/Client.java[Java simple HTTP client]

=== Load balancing

Demonstrate how to use client side load balancing

link:src/main/java/io/vertx/example/core/http/loadbalancing/Servers.java[Start 3 HTTP servers on port 8080/8081/8082]
link:src/main/java/io/vertx/example/core/http/loadbalancing/Client.java[HTTP client load balancing example]

=== HTTPS

Like the simple example, but using HTTPS instead of HTTP
Expand Down
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")));
}
}
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@ public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
}

private final int port;

public Server(int port) {
this.port = port;
}

public Server() {
this(8080);
}

@Override
public Future<?> start() throws Exception {
return vertx
.createHttpServer()
.requestHandler(req -> {
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from vert.x!</h1></body></html>");
})
.listen(8080);
.listen(port);
}
}
6 changes: 3 additions & 3 deletions grpc-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ link:src/main/java/io/vertx/example/grpc/jsonformat/ServerWithStub.java[gRPC ser
link:src/main/java/io/vertx/example/grpc/jsonformat/Client.java[gRPC client example]
link:src/main/java/io/vertx/example/grpc/jsonformat/ClientWithStub.java[gRPC client stub example]

== Load balancer
== Load balancing

Demonstrate how to use client side load balancing

link:src/main/java/io/vertx/example/grpc/loadbalancer/Servers.java[Start 3 gRPC servers on port 8080/8081/8082]
link:src/main/java/io/vertx/example/grpc/loadbalancer/Client.java[gRPC load balancing example]
link:src/main/java/io/vertx/example/grpc/loadbalancing/Servers.java[Start 3 gRPC servers on port 8080/8081/8082]
link:src/main/java/io/vertx/example/grpc/loadbalancing/Client.java[gRPC client load balancing example]

== SSL example

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vertx.example.grpc.loadbalancer;
package io.vertx.example.grpc.loadbalancing;

import io.grpc.examples.helloworld.HelloRequest;
import io.grpc.examples.helloworld.VertxGreeterGrpcClient;
Expand Down Expand Up @@ -45,7 +45,7 @@ public Future<?> start() throws Exception {

client = GrpcClient
.builder(vertx)
// In reality we could avoid using such mock resolver and the client instead could use a server list provided by a DNS server
// 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.vertx.example.grpc.loadbalancer;
package io.vertx.example.grpc.loadbalancing;

import io.vertx.core.Vertx;
import io.vertx.example.grpc.helloworld.Server;
Expand All @@ -10,6 +10,6 @@ public static void main(String[] args) {
vertx.deployVerticle(new Server(8080)).await();
vertx.deployVerticle(new Server(8081)).await();
vertx.deployVerticle(new Server(8082)).await();
System.out.println("Servers started on port 8080/8081/8082");
System.out.println("Servers listening on ports 8080/8081/8082");
}
}

0 comments on commit bea1370

Please sign in to comment.