gRPC Proxy
This document is intended to help the gRPC
service access the Apache ShenYu
gateway. The Apache ShenYu
gateway uses the gRPC
plugin to handle gRPC
service.
Before the connection, start shenyu-admin
correctly, start gRPC
plugin, and add related dependencies on the gateway and gRPC
application client. Refer to the previous Quick start with gRPC .
For details about client access configuration, see Application Client Access Config .
For details about data synchronization configurations, see Data Synchronization Config .
#
Add gRPC plugin in gatewayAdd the following dependencies in the gateway's pom.xml
file:
<!-- apache shenyu grpc plugin start--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-plugin-grpc</artifactId> <version>${project.version}</version> </dependency> <!-- apache shenyu grpc plugin end-->
- Restart the gateway service.
#
gRPC service access gatewayYou can refer to:shenyu-examples-grpc .
- In the microservice built by
gRPC
, add the following dependencies:
<dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-client-grpc</artifactId> <version>${shenyu.version}</version> <exclusions> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> </exclusions> </dependency>
Execute command to generate java code in shenyu-examples-grpc
project.
mvn protobuf:compile mvn protobuf:compile-custom
- Add the following configuration to application.yaml:
shenyu: register: registerType: http #zookeeper #etcd #nacos #consul serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848 props: username: admin password: 123456 client: grpc: props: contextPath: /grpc appName: grpc ipAndPort: 127.0.0.1:38080 port: 38080
- Add
@ShenyuGrpcClient
Annotation on thegRPC
service interface implementation class. Start your service provider, after successful registration, in the background management system go to PluginList -> rpc proxy -> gRPC, you will see automatic registration of selectors and rules information.
Example:
@Override @ShenyuGrpcClient(path = "/echo", desc = "echo") public void echo(EchoRequest request, StreamObserver<EchoResponse> responseObserver) { System.out.println("Received: " + request.getMessage()); EchoResponse.Builder response = EchoResponse.newBuilder() .setMessage("ReceivedHELLO") .addTraces(Trace.newBuilder().setHost(getHostname()).build()); responseObserver.onNext(response.build()); responseObserver.onCompleted(); }
#
User RequestYou can request your gRPC service by Http. The Apache ShenYu
gateway needs to have a route prefix that you access to configure contextPath
.
If your proto
file is defined as follows:
message EchoRequest { string message = 1;}
So the request parameters look like this:
{ "data": [ { "message": "hello grpc" } ]}
The parameters are currently passed in json
format, and the name of key
defaults to data
, which you can reset in GrpcConstants.JSON_DESCRIPTOR_PROTO_FIELD_NAME
; The value
is passed in according to the proto
file you define.
the Apache ShenYu can support streaming calls to gRPC
service, passing multiple arguments in the form of an array.
If your proto
file is defined as follows:
message RequestData { string text = 1;}
The corresponding method call request parameters are as follows:
UNARY
{ "data": [ { "text": "hello grpc" } ]}
CLIENT_STREAMING
{ "data": [ { "text": "hello grpc" }, { "text": "hello grpc" }, { "text": "hello grpc" } ]}
SERVER_STREAMING
{ "data": [ { "text": "hello grpc" } ]}
BIDI_STREAMING
{ "data": [ { "text": "hello grpc" }, { "text": "hello grpc" }, { "text": "hello grpc" } ]}