Skip to content

SCG used as as proxy to connect gRPC-Web and back end gRPC services

License

Notifications You must be signed in to change notification settings

yuanyouxi/grpc-web-spring-cloud-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC-Web Spring Cloud Gateway

Build master branch codeql license

Spring Cloud Gateway 3.1.1 supports for gRPC and HTTP/2.

It is possible to use Spring Cloud Gateway to connect gRPC-Web and gRPC services. This project will show:

  • How SCG route gRPC-Web request to gRPC service without Envoy proxy.

  • How to integrate with Nacos and discover the gRPC service port.

Introduction to gRPC-Web

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.

gRPC and Spring Cloud Gateway

Please read spring-cloud-gateway-and-grpc, it shows how to use Spring Cloud Gateway gRPC-2-gRPC and JSON-2-gRPC

Getting started

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.

About

SCG used as as proxy to connect gRPC-Web and back end gRPC services

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages