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

Add gRPC reverse proxy server example #5722

Merged
merged 23 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9f5e044
Add gRPC reverse proxy server example
eottabom Jun 4, 2024
4cacfc1
Add gRPC reverse proxy server example
eottabom Jun 4, 2024
774b34f
Add gRPC reverse proxy server example
eottabom Jun 4, 2024
8f22629
Add gRPC reverse proxy server example
eottabom Jun 5, 2024
61c3429
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
b4abca8
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
00fa962
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
5759a6c
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
7016aaa
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
cd3d8e0
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
b1efcca
ISSUE-2353: Add reverse proxy example
eottabom Jun 10, 2024
ce6ab18
ISSUE-2353: Add reverse proxy example
eottabom Jun 11, 2024
9424ddb
ISSUE-2353: Add reverse proxy example
eottabom Jun 11, 2024
4946a11
ISSUE-2353: Add reverse proxy example
eottabom Jun 11, 2024
0dd24d8
minor updates
jrhee17 Jun 14, 2024
958c0f6
test for multiple protocols
jrhee17 Jun 14, 2024
4ff7116
remove unnecessary dependencies
jrhee17 Jun 14, 2024
b3fd200
handle flakiness
jrhee17 Jun 14, 2024
39322a3
retry for http status unavailable
jrhee17 Jun 14, 2024
70a7c66
use testcontainers host, open the host port preemptively
jrhee17 Jun 14, 2024
f2569dc
ISSUE-2353: Add reverse proxy example
eottabom Jun 19, 2024
e1332e6
ISSUE-2353: Add reverse proxy example
eottabom Jun 19, 2024
0c8ecb4
ISSUE-2353: Add reverse proxy example
eottabom Jun 19, 2024
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
1 change: 1 addition & 0 deletions examples/grpc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
testImplementation libs.awaitility
testImplementation libs.junit5.jupiter.api
testImplementation libs.protobuf.java.util
testImplementation libs.testcontainers.junit.jupiter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a separate project for the example you're writing. Don't make an example do more than one thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, am I right to understand?
Are you telling me to create and use a grpc-reverse-proxy project in the sample directory?

}

application {
Expand Down
45 changes: 45 additions & 0 deletions examples/grpc/envoy/envoy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
static_resources:
listeners:
- name: listener_0
address:
socket_address:
address: 0.0.0.0
port_value: 9090
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
codec_type: AUTO
route_config:
name: local_route
virtual_hosts:
- name: local_service
domains: ["*"]
routes:
- match:
prefix: "/"
route:
cluster: grpc_service
timeout: 0s
idle_timeout: 0s
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
clusters:
- name: grpc_service
connect_timeout: 1s
type: STRICT_DNS
lb_policy: round_robin
http2_protocol_options: {}
load_assignment:
cluster_name: grpc_service
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
address: host.docker.internal
port_value: 18080
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package example.armeria.grpc;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;

import com.linecorp.armeria.client.WebClient;
import com.linecorp.armeria.client.grpc.GrpcClients;
import com.linecorp.armeria.server.Server;
import com.linecorp.armeria.server.grpc.GrpcService;

import example.armeria.grpc.Hello.HelloReply;
import example.armeria.grpc.Hello.HelloRequest;

class GrpcReverseProxyServerTest {

private static Server server;

@Container
private static GenericContainer<?> envoy = new GenericContainer<>("envoyproxy/envoy:v1.22.0")
.withExposedPorts(9090)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a random unused port to avoid port collision as much as possible. Use PortUtil.unusedTcpPort.

.withFileSystemBind("./envoy/envoy.yaml", "/etc/envoy/envoy.yaml", BindMode.READ_WRITE);

@BeforeAll
static void startServer() {
server = Server.builder()
.http(18080)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use an ephemeral port to avoid port collision

.service(GrpcService.builder()
.addService(new HelloServiceImpl())
.build())
.build();
server.start().join();
envoy.start();
}

@AfterAll
static void stopServer() {
server.stop().join();
envoy.stop();
}

@Test
void reverseProxy() {

// given
final String envoyAddress = "http://" + envoy.getHost() + ":" + envoy.getMappedPort(9090);

final WebClient client = WebClient.of(envoyAddress);
final HelloServiceGrpc.HelloServiceBlockingStub helloService = GrpcClients.builder(client.uri())
.build(HelloServiceGrpc.HelloServiceBlockingStub.class);

// when
final HelloReply reply = helloService.hello(HelloRequest.newBuilder().setName("Armeria").build());

// then
assertThat(reply.getMessage()).isEqualTo("Hello, Armeria!");
}
}
Loading