With gRPC-Web we can use proto to generate JS lib for web project like VUE to call gRPC services, but need a proxy like Envoy or golang proxy to transfer gRPC-Web protocol to gRPC.
Thanks to golang proxy, get the idea how to do the protocol transform , and build the Gateway request/response filters with Java.
Please read spring-cloud-gateway-and-grpc, it shows how to use Spring Cloud Gateway gRPC-2-gRPC and JSON-2-gRPC
To prepare the basic, please read spring-cloud-gateway-and-grpc gRPC-2-gRPC part, here we enable HTTP2 only without SSL
server:
http2:
enabled: true
So web project will call Spring Cloud Gateway using HTTP1.1 without SSL, and Spring Cloud Gateway talk with backend gRPC service using HTTP2 with TLS1.2
Here we trust all backend gRPC services.
spring:
cloud:
gateway:
httpclient:
ssl:
use-insecure-trust-manager: true
The Spring Cloud Gateway Global filter RemoveHopByHopHeadersFilter
will remove te
and trailer
which need in gRPC especially for stream.
spring:
cloud:
gateway:
filter:
remove-hop-by-hop:
headers:
- connection
- keep-alive
- transfer-encoding
- proxy-authenticate
- proxy-authorization
- x-application-context
- upgrade
Update HEADERS_REMOVED_ON_REQUEST
list in yaml to skip te
and trailer
As we build the protocol transform filter as Global filters, introduce a metadata grpcWeb
in route , the gRPC-Web related filters will only work when the metadata grpcWeb
is true.
routes:
- id: grpc
metadata:
grpcWeb: true
uri: lb:https://greeter
predicates:
- Path=/Greeter/**
filters:
- GrpcLoadBalance=true
The GrpcLoadBalance is used to rebuild the target URI change the port to gRPC service port, as the common examples of gRPC springboot integrate with Nacos usually add spring boot web/webflux to support both REST and gRPC API.
We can use gRPC in springboot without spring boot web/webflux , but when use Nacos or other Register/Discovery services, there are additional code needed to trigger the registration.
Finally, come to protocol transform, need to decode request body decode, here use ModifyRequestBodyGatewayFilterFactory
to do base64 decode , then modify the request headers.
For response part, base64 encode chunk by chunk to support the service side stream, then modify the response headers.