-
Notifications
You must be signed in to change notification settings - Fork 997
Add gRPC reverse proxy server example #5722
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9f5e044
Add gRPC reverse proxy server example
eottabom 4cacfc1
Add gRPC reverse proxy server example
eottabom 774b34f
Add gRPC reverse proxy server example
eottabom 8f22629
Add gRPC reverse proxy server example
eottabom 61c3429
ISSUE-2353: Add reverse proxy example
eottabom b4abca8
ISSUE-2353: Add reverse proxy example
eottabom 00fa962
ISSUE-2353: Add reverse proxy example
eottabom 5759a6c
ISSUE-2353: Add reverse proxy example
eottabom 7016aaa
ISSUE-2353: Add reverse proxy example
eottabom cd3d8e0
ISSUE-2353: Add reverse proxy example
eottabom b1efcca
ISSUE-2353: Add reverse proxy example
eottabom ce6ab18
ISSUE-2353: Add reverse proxy example
eottabom 9424ddb
ISSUE-2353: Add reverse proxy example
eottabom 4946a11
ISSUE-2353: Add reverse proxy example
eottabom 0dd24d8
minor updates
jrhee17 958c0f6
test for multiple protocols
jrhee17 4ff7116
remove unnecessary dependencies
jrhee17 b3fd200
handle flakiness
jrhee17 39322a3
retry for http status unavailable
jrhee17 70a7c66
use testcontainers host, open the host port preemptively
jrhee17 f2569dc
ISSUE-2353: Add reverse proxy example
eottabom e1332e6
ISSUE-2353: Add reverse proxy example
eottabom 0c8ecb4
ISSUE-2353: Add reverse proxy example
eottabom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,19 @@ | ||
| plugins { | ||
| id 'application' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation project(':core') | ||
| implementation project(':grpc') | ||
| implementation libs.testcontainers.junit.jupiter | ||
| compileOnly libs.javax.annotation | ||
| runtimeOnly libs.slf4j.simple | ||
|
|
||
| testImplementation project(':junit5') | ||
| testImplementation libs.assertj | ||
| testImplementation libs.junit5.jupiter.api | ||
| } | ||
|
|
||
| application { | ||
| mainClass.set('example.armeria.grpc.envoy.Main') | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
examples/grpc-envoy/src/main/java/example/armeria/grpc/envoy/EnvoyContainer.java
This file contains hidden or 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,62 @@ | ||
| package example.armeria.grpc.envoy; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.testcontainers.containers.BindMode; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
|
|
||
| import com.github.dockerjava.api.command.InspectContainerResponse; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.Nullable; | ||
|
|
||
| // https://github.com/envoyproxy/java-control-plane/blob/eaca1a4380e53b4b6339db4e9ffe0ada5e0b7f8f/server/src/test/java/io/envoyproxy/controlplane/server/EnvoyContainer.java | ||
| class EnvoyContainer extends GenericContainer<EnvoyContainer> { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(EnvoyContainer.class); | ||
|
|
||
| private static final String CONFIG_DEST = "/etc/envoy/envoy.yaml"; | ||
| private static final String LAUNCH_ENVOY_SCRIPT = "envoy/launch_envoy.sh"; | ||
| private static final String LAUNCH_ENVOY_SCRIPT_DEST = "/usr/local/bin/launch_envoy.sh"; | ||
|
|
||
| static final int ADMIN_PORT = 9901; | ||
|
|
||
| private final String config; | ||
| @Nullable | ||
| private final String sedCommand; | ||
|
|
||
| /** | ||
| * A {@link GenericContainer} implementation for envoy containers. | ||
| * | ||
| * @param sedCommand optional sed command which may be used to postprocess the provided {@param config}. | ||
| * This parameter will be fed into the command {@code sed -e <sedCommand>}. | ||
| * An example command may be {@code "s/foo/bar/g;s/abc/def/g"}. | ||
| */ | ||
| EnvoyContainer(String config, @Nullable String sedCommand) { | ||
| super("envoyproxy/envoy:v1.30.1"); | ||
| this.config = config; | ||
| this.sedCommand = sedCommand; | ||
| } | ||
|
|
||
| @Override | ||
| protected void configure() { | ||
| super.configure(); | ||
|
|
||
| withClasspathResourceMapping(LAUNCH_ENVOY_SCRIPT, LAUNCH_ENVOY_SCRIPT_DEST, BindMode.READ_ONLY); | ||
| withClasspathResourceMapping(config, CONFIG_DEST, BindMode.READ_ONLY); | ||
|
|
||
| if (sedCommand != null) { | ||
| withCommand("/bin/bash", "/usr/local/bin/launch_envoy.sh", | ||
| sedCommand, CONFIG_DEST, "-l", "debug"); | ||
| } | ||
|
|
||
| addExposedPort(ADMIN_PORT); | ||
| } | ||
|
|
||
| @Override | ||
| protected void containerIsStarting(InspectContainerResponse containerInfo) { | ||
| followOutput(new Slf4jLogConsumer(LOGGER).withPrefix("ENVOY")); | ||
|
|
||
| super.containerIsStarting(containerInfo); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
examples/grpc-envoy/src/main/java/example/armeria/grpc/envoy/HelloService.java
This file contains hidden or 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,29 @@ | ||
| package example.armeria.grpc.envoy; | ||
|
|
||
| import example.armeria.grpc.envoy.Hello.HelloReply; | ||
| import example.armeria.grpc.envoy.Hello.HelloRequest; | ||
| import example.armeria.grpc.envoy.HelloServiceGrpc.HelloServiceImplBase; | ||
| import io.grpc.Status; | ||
| import io.grpc.stub.StreamObserver; | ||
|
|
||
| public class HelloService extends HelloServiceImplBase { | ||
|
|
||
| @Override | ||
| public void hello(HelloRequest request, StreamObserver<HelloReply> responseObserver) { | ||
| if (request.getName().isEmpty()) { | ||
| responseObserver.onError( | ||
| Status.FAILED_PRECONDITION.withDescription("Name cannot be empty").asRuntimeException()); | ||
| } else { | ||
| responseObserver.onNext(buildReply(toMessage(request.getName()))); | ||
| responseObserver.onCompleted(); | ||
| } | ||
| } | ||
|
|
||
| static String toMessage(String name) { | ||
| return "Hello, " + name + '!'; | ||
| } | ||
|
|
||
| private static HelloReply buildReply(Object message) { | ||
| return HelloReply.newBuilder().setMessage(String.valueOf(message)).build(); | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
examples/grpc-envoy/src/main/java/example/armeria/grpc/envoy/Main.java
This file contains hidden or 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,52 @@ | ||
| package example.armeria.grpc.envoy; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.testcontainers.DockerClientFactory; | ||
|
|
||
| import com.linecorp.armeria.common.util.ShutdownHooks; | ||
| import com.linecorp.armeria.server.Server; | ||
| import com.linecorp.armeria.server.grpc.GrpcService; | ||
|
|
||
| public final class Main { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(Main.class); | ||
|
|
||
| private static final int serverPort = 8080; | ||
| // the port envoy binds to within the container | ||
| private static final int envoyPort = 10000; | ||
|
|
||
| public static void main(String[] args) { | ||
| if (!DockerClientFactory.instance().isDockerAvailable()) { | ||
| throw new IllegalStateException("Docker is not available"); | ||
| } | ||
|
|
||
| final Server backendServer = startBackendServer(serverPort); | ||
| backendServer.closeOnJvmShutdown(); | ||
| backendServer.start().join(); | ||
| logger.info("Serving backend at http://127.0.0.1:{}/", backendServer.activePort()); | ||
|
|
||
| final EnvoyContainer envoyProxy = configureEnvoy(serverPort, envoyPort); | ||
| ShutdownHooks.addClosingTask(envoyProxy::stop); | ||
| envoyProxy.start(); | ||
| final Integer mappedEnvoyPort = envoyProxy.getMappedPort(envoyPort); | ||
| logger.info("Serving envoy at http://127.0.0.1:{}/", mappedEnvoyPort); | ||
| } | ||
|
|
||
| private static Server startBackendServer(int serverPort) { | ||
| return Server.builder() | ||
| .http(serverPort) | ||
| .service(GrpcService.builder() | ||
| .addService(new HelloService()) | ||
| .build()) | ||
| .build(); | ||
| } | ||
|
|
||
| static EnvoyContainer configureEnvoy(int serverPort, int envoyPort) { | ||
| final String sedPattern = String.format("s/SERVER_PORT/%s/g;s/ENVOY_PORT/%s/g", serverPort, envoyPort); | ||
| return new EnvoyContainer("envoy/envoy.yaml", sedPattern) | ||
| .withExposedPorts(envoyPort); | ||
| } | ||
|
|
||
| private Main() {} | ||
| } |
This file contains hidden or 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,19 @@ | ||
| syntax = "proto3"; | ||
|
|
||
| package example.grpc.hello; | ||
| option java_package = "example.armeria.grpc.envoy"; | ||
| option java_multiple_files = false; | ||
|
|
||
| import "google/api/annotations.proto"; | ||
|
|
||
| service HelloService { | ||
| rpc Hello (HelloRequest) returns (HelloReply); | ||
| } | ||
|
|
||
| message HelloRequest { | ||
| string name = 1; | ||
| } | ||
|
|
||
| message HelloReply { | ||
| string message = 1; | ||
| } |
Empty file.
This file contains hidden or 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,52 @@ | ||
| admin: | ||
| address: | ||
| socket_address: { address: 0.0.0.0, port_value: 9901 } | ||
| static_resources: | ||
| listeners: | ||
| - name: listener_0 | ||
| address: | ||
| socket_address: | ||
| address: 0.0.0.0 | ||
| port_value: ENVOY_PORT | ||
| filter_chains: | ||
| - filters: | ||
| - name: envoy.filters.network.http_connection_manager | ||
| typed_config: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager | ||
| stat_prefix: ingress_http | ||
| http_protocol_options: | ||
| enable_trailers: true | ||
| codec_type: AUTO | ||
| route_config: | ||
| name: local_route | ||
| virtual_hosts: | ||
| - name: local_service | ||
| domains: ["*"] | ||
| routes: | ||
| - match: | ||
| prefix: "/" | ||
| route: | ||
| cluster: grpc_service | ||
| http_filters: | ||
| - name: envoy.filters.http.router | ||
| typed_config: | ||
| "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router | ||
| clusters: | ||
| - name: grpc_service | ||
| type: STRICT_DNS | ||
| lb_policy: ROUND_ROBIN | ||
| typed_extension_protocol_options: | ||
| envoy.extensions.upstreams.http.v3.HttpProtocolOptions: | ||
| "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions | ||
| explicit_http_config: | ||
| http_protocol_options: | ||
| enable_trailers: true | ||
| load_assignment: | ||
| cluster_name: grpc_service | ||
| endpoints: | ||
| - lb_endpoints: | ||
| - endpoint: | ||
| address: | ||
| socket_address: | ||
| address: host.testcontainers.internal | ||
| port_value: SERVER_PORT |
16 changes: 16 additions & 0 deletions
16
examples/grpc-envoy/src/main/resources/envoy/launch_envoy.sh
This file contains hidden or 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,16 @@ | ||
| #!/usr/bin/env bash | ||
| set -Eeuo pipefail | ||
|
|
||
| SED_COMMAND=$1 | ||
|
|
||
| CONFIG=$(cat $2) | ||
| CONFIG_DIR=$(mktemp -d) | ||
| CONFIG_FILE="$CONFIG_DIR/envoy.yaml" | ||
|
|
||
| echo "${CONFIG}" | sed -e "${SED_COMMAND}" > "${CONFIG_FILE}" | ||
|
|
||
|
|
||
| shift 2 | ||
| /usr/local/bin/envoy --drain-time-s 1 -c "${CONFIG_FILE}" "$@" | ||
|
|
||
| rm -rf "${CONFIG_DIR}" |
54 changes: 54 additions & 0 deletions
54
examples/grpc-envoy/src/test/java/example/armeria/grpc/envoy/GrpcEnvoyProxyTest.java
This file contains hidden or 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,54 @@ | ||
| package example.armeria.grpc.envoy; | ||
|
|
||
| import static example.armeria.grpc.envoy.Main.configureEnvoy; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.EnumSource; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
|
||
| import com.linecorp.armeria.client.grpc.GrpcClients; | ||
| import com.linecorp.armeria.common.SessionProtocol; | ||
| import com.linecorp.armeria.server.ServerBuilder; | ||
| import com.linecorp.armeria.server.grpc.GrpcService; | ||
| import com.linecorp.armeria.testing.junit5.server.ServerExtension; | ||
|
|
||
| import example.armeria.grpc.envoy.Hello.HelloReply; | ||
| import example.armeria.grpc.envoy.Hello.HelloRequest; | ||
|
|
||
| @Testcontainers(disabledWithoutDocker = true) | ||
| class GrpcEnvoyProxyTest { | ||
|
|
||
| // the port envoy binds to within the container | ||
| private static final int ENVOY_PORT = 10000; | ||
|
|
||
| @RegisterExtension | ||
| static ServerExtension server = new ServerExtension() { | ||
| @Override | ||
| protected void configure(ServerBuilder sb) throws Exception { | ||
| sb.service(GrpcService.builder() | ||
| .addService(new HelloService()) | ||
| .build()); | ||
| } | ||
| }; | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource(value = SessionProtocol.class, names = {"H1C", "H2C"}) | ||
| void reverseProxy(SessionProtocol sessionProtocol) { | ||
| org.testcontainers.Testcontainers.exposeHostPorts(server.httpPort()); | ||
| try (EnvoyContainer envoy = configureEnvoy(server.httpPort(), ENVOY_PORT)) { | ||
| envoy.start(); | ||
| final String uri = sessionProtocol.uriText() + "://" + envoy.getHost() + | ||
| ':' + envoy.getMappedPort(ENVOY_PORT); | ||
| final HelloServiceGrpc.HelloServiceBlockingStub helloService = | ||
| GrpcClients.builder(uri) | ||
| .build(HelloServiceGrpc.HelloServiceBlockingStub.class); | ||
| final HelloReply reply = | ||
| helloService.hello(HelloRequest.newBuilder() | ||
| .setName("Armeria") | ||
| .build()); | ||
| assertThat(reply.getMessage()).isEqualTo("Hello, Armeria!"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.