forked from spring-cloud/spring-cloud-gateway
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding initial gRPC support (spring-cloud#2388)
Passing TE trailers header through Adding grpc-status as response trailer to ensure the right end of stream Fixes gh-40
- Loading branch information
1 parent
da10105
commit af09c72
Showing
16 changed files
with
630 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://maven.apache.org/POM/4.0.0" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>grpc</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>Spring Cloud Gateway gRPC Integration Test</name> | ||
<description>Spring Cloud Gateway gRPC Integration Test</description> | ||
|
||
<properties> | ||
</properties> | ||
|
||
<parent> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-gateway-integration-tests</artifactId> | ||
<version>3.1.0-SNAPSHOT</version> | ||
<relativePath>..</relativePath> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-webflux</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.cloud</groupId> | ||
<artifactId>spring-cloud-starter-gateway</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-netty-shaded</artifactId> | ||
<version>1.41.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-protobuf</artifactId> | ||
<version>1.41.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.grpc</groupId> | ||
<artifactId>grpc-stub</artifactId> | ||
<version>1.41.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-tcnative-boringssl-static</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.projectreactor</groupId> | ||
<artifactId>reactor-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<extensions> | ||
<extension> | ||
<groupId>kr.motd.maven</groupId> | ||
<artifactId>os-maven-plugin</artifactId> | ||
<version>1.6.2</version> | ||
</extension> | ||
</extensions> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
<configuration> | ||
<skip>true</skip> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.xolstice.maven.plugins</groupId> | ||
<artifactId>protobuf-maven-plugin</artifactId> | ||
<version>0.6.1</version> | ||
<configuration> | ||
<protocArtifact>com.google.protobuf:protoc:3.17.3:exe:${os.detected.classifier}</protocArtifact> | ||
<pluginId>grpc-java</pluginId> | ||
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.41.0:exe:${os.detected.classifier}</pluginArtifact> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>compile</goal> | ||
<goal>compile-custom</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
|
119 changes: 119 additions & 0 deletions
119
...ests/grpc/src/main/java/org/springframework/cloud/gateway/tests/grpc/GRPCApplication.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,119 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or 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 | ||
* | ||
* https://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 org.springframework.cloud.gateway.tests.grpc; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.grpc.Grpc; | ||
import io.grpc.Server; | ||
import io.grpc.ServerCredentials; | ||
import io.grpc.TlsServerCredentials; | ||
import io.grpc.stub.StreamObserver; | ||
|
||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.SpringBootConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.cloud.gateway.route.RouteLocator; | ||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.SocketUtils; | ||
|
||
/** | ||
* @author Alberto C. Ríos | ||
*/ | ||
@SpringBootConfiguration | ||
@EnableAutoConfiguration | ||
public class GRPCApplication { | ||
|
||
private static final int GRPC_SERVER_PORT = SocketUtils.findAvailableTcpPort(); | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(GRPCApplication.class, args); | ||
} | ||
|
||
@Bean | ||
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { | ||
return builder.routes().route("grpc", r -> r.predicate(p -> true).uri("https://localhost:" + GRPC_SERVER_PORT)) | ||
.build(); | ||
} | ||
|
||
@Component | ||
static class GRPCServer implements ApplicationRunner { | ||
|
||
private Server server; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
final GRPCServer server = new GRPCServer(); | ||
server.start(); | ||
} | ||
|
||
private void start() throws Exception { | ||
/* The port on which the server should run */ | ||
ServerCredentials creds = createServerCredentials(); | ||
server = Grpc.newServerBuilderForPort(GRPC_SERVER_PORT, creds).addService(new HelloService()).build() | ||
.start(); | ||
|
||
System.out.println("Starting server in port " + GRPC_SERVER_PORT); | ||
|
||
Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
try { | ||
GRPCServer.this.stop(); | ||
} | ||
catch (InterruptedException e) { | ||
e.printStackTrace(System.err); | ||
} | ||
})); | ||
} | ||
|
||
private ServerCredentials createServerCredentials() throws IOException { | ||
File privateKey = new ClassPathResource("private.key").getFile(); | ||
File certChain = new ClassPathResource("certificate.pem").getFile(); | ||
return TlsServerCredentials.create(certChain, privateKey); | ||
} | ||
|
||
private void stop() throws InterruptedException { | ||
if (server != null) { | ||
server.shutdown().awaitTermination(30, TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
static class HelloService extends HelloServiceGrpc.HelloServiceImplBase { | ||
|
||
@Override | ||
public void hello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) { | ||
|
||
String greeting = "Hello, " + request.getFirstName() + " " + request.getLastName(); | ||
System.out.println(greeting); | ||
|
||
HelloResponse response = HelloResponse.newBuilder().setGreeting(greeting).build(); | ||
|
||
responseObserver.onNext(response); | ||
responseObserver.onCompleted(); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
spring-cloud-gateway-integration-tests/grpc/src/main/proto/hello.proto
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,16 @@ | ||
syntax = "proto3"; | ||
option java_multiple_files = true; | ||
option java_package = "org.springframework.cloud.gateway.tests.grpc"; | ||
|
||
message HelloRequest { | ||
string firstName = 1; | ||
string lastName = 2; | ||
} | ||
|
||
message HelloResponse { | ||
string greeting = 1; | ||
} | ||
|
||
service HelloService { | ||
rpc hello(HelloRequest) returns (HelloResponse); | ||
} |
Empty file.
21 changes: 21 additions & 0 deletions
21
spring-cloud-gateway-integration-tests/grpc/src/main/resources/certificate.pem
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,21 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIDdzCCAl+gAwIBAgIEIon96DANBgkqhkiG9w0BAQsFADBsMRAwDgYDVQQGEwdV | ||
bmtub3duMRAwDgYDVQQIEwdVbmtub3duMRAwDgYDVQQHEwdVbmtub3duMRAwDgYD | ||
VQQKEwdVbmtub3duMRAwDgYDVQQLEwdVbmtub3duMRAwDgYDVQQDEwdVbmtub3du | ||
MB4XDTIxMDkyNzE2NDQwNVoXDTMxMDkyNTE2NDQwNVowbDEQMA4GA1UEBhMHVW5r | ||
bm93bjEQMA4GA1UECBMHVW5rbm93bjEQMA4GA1UEBxMHVW5rbm93bjEQMA4GA1UE | ||
ChMHVW5rbm93bjEQMA4GA1UECxMHVW5rbm93bjEQMA4GA1UEAxMHVW5rbm93bjCC | ||
ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAIwevGHY30YdoUdCSB/5q7/F | ||
c0KHetdjb71G2u6vFdeNvSwMpCV8Z1JznOJ/1zuY+0Z105QCPM7fi1ACi+tqxDR+ | ||
L7yjUHhUEMTiGgCHcYJIZZCYfWS3BQXVxgORXhDv7RduCUaCLnkaFY++iPMTUy0C | ||
VxIkplIEhAmqcikIgWaa5ZjBkegKgahlPQLKlfD4Rz/kq2P+LLYFsHNNdKfWv6XQ | ||
u4LMw7ZEAJtfdpaMTzmtQipbTt6Dh87vIa0CIVnCPdlQ3o/5WeaxEA1pnfOLas07 | ||
1VdHih2nC5vHhQcTPQDfa+uwzQvzHrchjuvMUUZaCYJzuT0G6nbGBba54EBT7yUC | ||
AwEAAaMhMB8wHQYDVR0OBBYEFKOHmfytP5ab2C4iFHlSklu6tCcuMA0GCSqGSIb3 | ||
DQEBCwUAA4IBAQAdgWwdOtRbI796Z22weTBc0/tM8kLc6G0raNb08WyZMPZVki04 | ||
jPh73pPQCgYeI/pq5JqH46KgvehmygTzpWDAFIllW0kgABVw3Nu6duV+blt1JG8T | ||
lWP7t5A+qDXgPDm3I5diii7O1YlLB3I37XiBdEV/+2WmF1VGQ7uBWAv+uotQeuW2 | ||
JvHOr4ICOiW45TzRYtAbzWukSYKg/A7lwBs7HE9KVomUxNrkD+7+ugRuy/31pyen | ||
pHsEJQpx5juFRE222B6GXmX0w9xLIOapytl4EoPUx3K8Ecc+yI2q00UUC43x0v28 | ||
c05YqwRZ5vp+jUnRxkxaz85YdArfR7QFYWtO | ||
-----END CERTIFICATE----- |
Binary file added
BIN
+2.52 KB
spring-cloud-gateway-integration-tests/grpc/src/main/resources/keystore.p12
Binary file not shown.
32 changes: 32 additions & 0 deletions
32
spring-cloud-gateway-integration-tests/grpc/src/main/resources/private.key
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,32 @@ | ||
Bag Attributes | ||
friendlyName: mykey | ||
localKeyID: 54 69 6D 65 20 31 36 33 32 38 32 34 38 37 31 33 33 35 | ||
Key Attributes: <No Attributes> | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwgg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| ||
8V9TFd5VQ89VeB5OekZwPks8vftwzW4L82ZRW5hvyQB0jPR+lwqjrNqds+xxH7i+ | ||
c+RFx15biwKBgQCgkGGK0zao7YUGl+zAWNDHgQo9KM5deZr0SUEg/kwhNlbHEEC/ | ||
plxxeauS1XdcdMdFb3bER/N+O31y2Uu7IL0qOJ9ZcRXdFep3sNxWFoHcctZw51AB | ||
accnIEjD21R62bTkditJoL4n5i9a2TS2T/QkfAR6QkvtCz5IDGBCzgoFRQKBgC51 | ||
VBfy3gklPgMt/PHW+1FSuep9FnvLwQ8F9iKdnjKdu8AoPNQGTw5Ok6bwDOSFnQaR | ||
n1Kb/anN7sRaICfw9kNFJVFHbznpjNwK4JO5+tif3EvSNND3+/QAXIIVck3G+GXH | ||
8nt/EJQcExRZSgv3jYf+cXeflPTBvb0RUyOAn3B/AoGAM9aUi2dg3XjlDkZahQlY | ||
P5QNIr9BY25Ordga3GLwfR6rE+jiWTeTmreXBSJ7nvcaEQUNyFMX9n3v/QI84etk | ||
lMJkZ4o2TgzRnCsFJoBV36Ihsa6B5uXVWAvMRLKvwKpptKXfO0TnhUg7oKtN4t3a | ||
/FzAOS2Eu1PFP27z74gAMKY= | ||
-----END PRIVATE KEY----- |
83 changes: 83 additions & 0 deletions
83
...grpc/src/test/java/org/springframework/cloud/gateway/tests/grpc/GRPCApplicationTests.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,83 @@ | ||
/* | ||
* Copyright 2013-2021 the original author or 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 | ||
* | ||
* https://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 org.springframework.cloud.gateway.tests.grpc; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
import javax.net.ssl.SSLException; | ||
import javax.net.ssl.TrustManager; | ||
import javax.net.ssl.X509TrustManager; | ||
|
||
import io.grpc.ManagedChannel; | ||
import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts; | ||
import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.system.OutputCaptureExtension; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
import static io.grpc.netty.shaded.io.grpc.netty.NegotiationType.TLS; | ||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
|
||
/** | ||
* @author Alberto C. Ríos | ||
*/ | ||
@ExtendWith(OutputCaptureExtension.class) | ||
@SpringBootTest(classes = GRPCApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) | ||
@DirtiesContext | ||
public class GRPCApplicationTests { | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@Test | ||
public void gRPCUnaryCalShouldReturnResponse() throws SSLException { | ||
ManagedChannel channel = createSecuredChannel(port); | ||
|
||
final HelloResponse response = HelloServiceGrpc.newBlockingStub(channel) | ||
.hello(HelloRequest.newBuilder().setFirstName("Sir").setLastName("FromClient").build()); | ||
|
||
Assertions.assertThat(response.getGreeting()).isEqualTo("Hello, Sir FromClient"); | ||
} | ||
|
||
private ManagedChannel createSecuredChannel(int port) throws SSLException { | ||
TrustManager[] trustAllCerts = createTrustAllTrustManager(); | ||
|
||
return NettyChannelBuilder.forAddress("localhost", port).useTransportSecurity() | ||
.sslContext(GrpcSslContexts.forClient().trustManager(trustAllCerts[0]).build()).negotiationType(TLS) | ||
.build(); | ||
} | ||
|
||
private TrustManager[] createTrustAllTrustManager() { | ||
return new TrustManager[] { new X509TrustManager() { | ||
public X509Certificate[] getAcceptedIssuers() { | ||
return new X509Certificate[0]; | ||
} | ||
|
||
public void checkClientTrusted(X509Certificate[] certs, String authType) { | ||
} | ||
|
||
public void checkServerTrusted(X509Certificate[] certs, String authType) { | ||
} | ||
} }; | ||
} | ||
|
||
} |
Oops, something went wrong.