Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add an example of using DefaultLoadBalancer #2855

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
** xref:{page-version}@servicetalk-examples::http/index.adoc#HTTP2[HTTP/2]
** xref:{page-version}@servicetalk-examples::http/index.adoc#Mutual-TLS[Mutual TLS]
** xref:{page-version}@servicetalk-examples::http/index.adoc#Observer[Observer]
** xref:{page-version}@servicetalk-examples::http/index.adoc#DefaultLoadBalancer[DefaultLoadBalancer]
** xref:{page-version}@servicetalk-examples::http/index.adoc#OpenTracing[OpenTracing]
** xref:{page-version}@servicetalk-examples::http/index.adoc#Redirects[Redirects]
** xref:{page-version}@servicetalk-examples::http/index.adoc#Retries[Retries]
Expand Down
14 changes: 14 additions & 0 deletions servicetalk-examples/docs/modules/ROOT/pages/http/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,20 @@ on via a client filter on the client builder.
link:{source-root}/servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpLifecycleObserver.java[HttpLifecycleObserver]
on via a client filter on the client builder.

[#DefaultLoadBalancer]
== DefaultLoadBalancer
This example demonstrates how to use the experimental link:{source-root}/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancer.java[DefaultLoadBalancer]:

- by setting the load balancer to DefaultLoadBalancer for a HTTP client
- configuring xDS failure detection for that client using default settings

Using the following classes:

- link:{source-root}/servicetalk-examples/http/defaultloadbalancer/src/main/java/io/servicetalk/examples/http/defaultloadbalancer/DefaultLoadBalancerClient.java[DefaultLoadBalancerClient] - A client that uses the DefaultLoadBalancer.
- link:{source-root}/servicetalk-examples/http/defaultloadbalancer/src/main/java/io/servicetalk/examples/http/defaultloadbalancer/HelloWorldServer.java[HelloWorldServer] - A simple server implementation.

NOTE: DefaultLoadBalancer is currently considered experimental and therefore the API is subject to change.

[#OpenTracing]
== OpenTracing

Expand Down
4 changes: 4 additions & 0 deletions servicetalk-examples/http/defaultloadbalancer/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
== ServiceTalk DefaultLoadBalancer Example

A simple hello-world style application that uses a client configured with the DefaultLoadBalancer and xDS style outlier
detection.
25 changes: 25 additions & 0 deletions servicetalk-examples/http/defaultloadbalancer/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © 2019 Apple Inc. and the ServiceTalk project authors
*
* 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.
*/
apply plugin: "java"
apply from: "../../gradle/idea.gradle"

dependencies {
implementation project(":servicetalk-annotations")
implementation project(":servicetalk-http-netty")
implementation project(":servicetalk-loadbalancer-experimental")

runtimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors
*
* 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.
*/
package io.servicetalk.examples.http.defaultloadbalancer;

import io.servicetalk.client.api.LoadBalancerFactory;
import io.servicetalk.http.api.BlockingHttpClient;
import io.servicetalk.http.api.FilterableStreamingHttpLoadBalancedConnection;
import io.servicetalk.http.api.HttpResponse;
import io.servicetalk.http.api.SingleAddressHttpClientBuilder;
import io.servicetalk.http.netty.DefaultHttpLoadBalancerFactory;
import io.servicetalk.http.netty.HttpClients;
import io.servicetalk.loadbalancer.LoadBalancers;
import io.servicetalk.loadbalancer.OutlierDetectorConfig;
import io.servicetalk.loadbalancer.XdsHealthCheckerFactory;
import io.servicetalk.transport.api.HostAndPort;

import java.net.InetSocketAddress;

import static io.servicetalk.http.api.HttpSerializers.textSerializerUtf8;

public final class DefaultLoadBalancerClient {

public static void main(String[] args) throws Exception {
SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> builder =
HttpClients.forSingleAddress("localhost", 8080)
.loadBalancerFactory(DefaultHttpLoadBalancerFactory.Builder.from(
loadBalancer("localhost-defaultloadbalancer")).build());
try (BlockingHttpClient client = builder.buildBlocking()) {
HttpResponse response = client.request(client.get("/sayHello"));
System.out.println(response.toString((name, value) -> value));
System.out.println(response.payloadBody(textSerializerUtf8()));
}
}

private static LoadBalancerFactory<InetSocketAddress, FilterableStreamingHttpLoadBalancedConnection> loadBalancer(
String id) {
return LoadBalancers.<InetSocketAddress, FilterableStreamingHttpLoadBalancedConnection>
builder(id)
.healthCheckerFactory(new XdsHealthCheckerFactory<>(
new OutlierDetectorConfig.Builder().build()
)).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors
*
* 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.
*/
package io.servicetalk.examples.http.defaultloadbalancer;

import io.servicetalk.http.netty.HttpServers;

import static io.servicetalk.http.api.HttpSerializers.textSerializerUtf8;

public final class HelloWorldServer {

public static void main(String[] args) throws Exception {
HttpServers.forPort(8080)
.listenBlockingAndAwait((ctx, request, responseFactory) ->
responseFactory.ok().payloadBody("Hello World!", textSerializerUtf8()))
.awaitShutdown();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright © 2024 Apple Inc. and the ServiceTalk project authors
*
* 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.
*/
@ElementsAreNonnullByDefault
package io.servicetalk.examples.http.defaultloadbalancer;

import io.servicetalk.annotations.ElementsAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright © 2018 Apple Inc. and the ServiceTalk project authors
~
~ 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.
-->
<Configuration status="info">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %30t [%-5level] %-30logger{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<!-- Prints server start and shutdown -->
<Logger name="io.servicetalk.http.netty.NettyHttpServer" level="DEBUG"/>

<!-- Prints default subscriber errors-->
<Logger name="io.servicetalk.concurrent.api" level="DEBUG"/>

<!-- Use `-Dservicetalk.logger.level=DEBUG` to change the root logger level via command line -->
<Root level="${sys:servicetalk.logger.level:-INFO}">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ include "servicetalk-annotations",
"servicetalk-examples:http:compression",
"servicetalk-examples:http:debugging",
"servicetalk-examples:http:timeout",
"servicetalk-examples:http:defaultloadbalancer",
"servicetalk-gradle-plugin-internal",
"servicetalk-grpc-api",
"servicetalk-grpc-health",
Expand Down
Loading