Merge branch '3.1.x'

This commit is contained in:
spencergibb
2023-04-20 16:24:59 -04:00
4 changed files with 20 additions and 11 deletions

View File

@@ -984,7 +984,7 @@ src/main/resources/proto/hello.proto
* `protoFile`: Proto definition file.
* `service`: Fully qualified name of the service that handles the request.
* `service`: Short name of the service that handles the request.
* `method`: Method name in the service that handles the request.
@@ -1022,12 +1022,12 @@ spring:
args:
protoDescriptor: file:proto/hello.pb
protoFile: file:proto/hello.proto
service: com.example.grpcserver.hello.HelloService
service: HelloService
method: hello
----
When a request is made through the gateway to `/json/hello`, the request is transformed by using the definition provided in `hello.proto`, sent to `com.example.grpcserver.hello.HelloService/hello`, and the response back is transformed to JSON.
When a request is made through the gateway to `/json/hello`, the request is transformed by using the definition provided in `hello.proto`, sent to `HelloService/hello`, and the response back is transformed to JSON.
By default, it creates a `NettyChannel` by using the default `TrustManagerFactory`. However, you can customize this `TrustManager` by creating a bean of type `GrpcSslConfigurer`:

View File

@@ -1,6 +1,7 @@
syntax = "proto3";
option java_multiple_files = true;
option java_package = "org.springframework.cloud.gateway.tests.grpc";
package org.springframework.cloud.gateway.tests.grpc;
message HelloRequest {
optional string firstName = 1;

View File

@@ -192,9 +192,12 @@ public class JsonToGrpcGatewayFilterFactory
descriptor = DescriptorProtos.FileDescriptorProto.parseFrom(descriptorFile.getInputStream())
.getDescriptorForType();
Descriptors.Descriptor outputType = getOutputTypeDescriptor(config, descriptorFile.getInputStream());
Descriptors.MethodDescriptor methodDescriptor = getMethodDescriptor(config,
descriptorFile.getInputStream());
Descriptors.ServiceDescriptor serviceDescriptor = methodDescriptor.getService();
Descriptors.Descriptor outputType = methodDescriptor.getOutputType();
clientCall = createClientCallForType(config, outputType);
clientCall = createClientCallForType(config, serviceDescriptor, outputType);
ProtobufSchema schema = ProtobufSchemaLoader.std.load(protoFile.getInputStream());
ProtobufSchema responseType = schema.withRootType(outputType.getName());
@@ -220,18 +223,19 @@ public class JsonToGrpcGatewayFilterFactory
}
private ClientCall<DynamicMessage, DynamicMessage> createClientCallForType(Config config,
Descriptors.Descriptor outputType) {
Descriptors.ServiceDescriptor serviceDescriptor, Descriptors.Descriptor outputType) {
MethodDescriptor.Marshaller<DynamicMessage> marshaller = ProtoUtils
.marshaller(DynamicMessage.newBuilder(outputType).build());
MethodDescriptor<DynamicMessage, DynamicMessage> methodDescriptor = MethodDescriptor
.<DynamicMessage, DynamicMessage>newBuilder().setType(MethodDescriptor.MethodType.UNKNOWN)
.setFullMethodName(MethodDescriptor.generateFullMethodName(config.getService(), config.getMethod()))
.setFullMethodName(MethodDescriptor.generateFullMethodName(serviceDescriptor.getFullName(),
config.getMethod()))
.setRequestMarshaller(marshaller).setResponseMarshaller(marshaller).build();
Channel channel = createChannel();
return channel.newCall(methodDescriptor, CallOptions.DEFAULT);
}
private Descriptors.Descriptor getOutputTypeDescriptor(Config config, InputStream descriptorFile)
private Descriptors.MethodDescriptor getMethodDescriptor(Config config, InputStream descriptorFile)
throws IOException, Descriptors.DescriptorValidationException {
DescriptorProtos.FileDescriptorSet fileDescriptorSet = DescriptorProtos.FileDescriptorSet
.parseFrom(descriptorFile);
@@ -239,11 +243,15 @@ public class JsonToGrpcGatewayFilterFactory
Descriptors.FileDescriptor fileDescriptor = Descriptors.FileDescriptor.buildFrom(fileProto,
new Descriptors.FileDescriptor[0]);
List<Descriptors.MethodDescriptor> methods = fileDescriptor.findServiceByName(config.getService())
.getMethods();
Descriptors.ServiceDescriptor serviceDescriptor = fileDescriptor.findServiceByName(config.getService());
if (serviceDescriptor == null) {
throw new NoSuchElementException("No Service found");
}
List<Descriptors.MethodDescriptor> methods = serviceDescriptor.getMethods();
return methods.stream().filter(method -> method.getName().equals(config.getMethod())).findFirst()
.orElseThrow(() -> new NoSuchElementException("No Method found")).getOutputType();
.orElseThrow(() -> new NoSuchElementException("No Method found"));
}
private ManagedChannel createChannel() {