Introduction to Kong gRPC Plugins

Before going into the specifics of configuring Kong’s gRPC plugins, let’s discuss the advantages of the gRPC protocol. Unlike JSON, gRPC is a binary protocol, using protobuf definitions to instruct how the data is marshalled and unmarshalled. Because binary data is used instead of text, it’s a more efficient way to transmit data over a network. However, this also makes gRPC harder to work with, because inspecting what went wrong is more challenging. Additionally, few clients natively handle gRPC.

To help alleviate the challenges of working with gRPC, Kong has two plugins:

The gRPC-Gateway plugin allows you to send JSON requests to a gRPC service. A specially configured .proto file handles the conversion of the JSON request into one that the gRPC service can handle. This allows you to expose RESTful-style interfaces that talk to a gRPC service.

The gRPC-Web plugin allows you to interact with a gRPC service from a browser. Instead of presenting a RESTful-type call, you POST data to the same gRPC service endpoint that the protobuf defines.

For flexibility and compatibility with RESTful expectations, the gRPC-Gateway plugin offers more configurability, whereas the gRPC-Web plugin adheres more directly to the protobuf specification.

Let’s walk through setting up each plugin so you can see how they work.

gRPC-Gateway plugin configuration

Set up a Service:

  1. curl -X POST kong-cp-host:8001/services \
  2. --data 'name=grpcbin-service' \
  3. --data 'url=grpc://grpcb.in:9000'

Set up the Route to the Service:

  1. curl -X POST kong-cp-host:8001/services/grpcbin-service/routes \
  2. --data 'name=grpcbin-get-route' \
  3. --data 'paths=/' \
  4. --data 'methods=GET' \
  5. --data 'headers.x-grpc=true'

Set up the gRPC-Web plugin:

  1. curl -X POST kong-cp-host:8001/routes/grpcbin-get-route/plugins \
  2. --data 'name=grpc-gateway' \
  3. --data 'config.proto=/usr/local/kong/hello-gateway.proto'

Protobuf definition (hello-gateway.proto):

  1. syntax = "proto3";
  2. package hello;
  3. service HelloService {
  4. rpc SayHello(HelloRequest) returns (HelloResponse) {
  5. option (google.api.http) = {
  6. get: "/v1/messages/{name}"
  7. additional_bindings {
  8. get: "/v1/messages/legacy/{name=**}"
  9. }
  10. post: "/v1/messages/"
  11. body: "*"
  12. }
  13. }
  14. }
  15. // The request message containing the user's name.
  16. message HelloRequest {
  17. string name = 1;
  18. }
  19. // The response message containing the greetings
  20. message HelloResponse {
  21. string message = 1;
  22. }

Upload the protobuf definition to your Kong Node:

  1. docker cp hello-gateway.proto kong-dp-host:/usr/local/kong/

Test your setup:

  1. curl -X GET kong-dp-host:8000/v1/messages/kong2.1 \
  2. --header 'x-grpc: true'

gRPC-Web plugin configuration

Set up a Service:

  1. curl -X POST kong-cp-host:8001/services \
  2. --data 'name=grpcbin-service' \
  3. --data 'url=grpc://grpcb.in:9000'

Set up the Route to the Service:

  1. curl -X POST kong-cp-host:8001/services/grpcbin-service/routes \
  2. --data 'name=grpcbin-post-route' \
  3. --data 'paths=/' \
  4. --data 'methods=POST' \
  5. --data 'headers.x-grpc=true'

Set up the gRPC-Web plugin:

  1. curl -X POST kong-cp-host:8001/routes/grpcbin-post-route/plugins \
  2. --data 'name=grpc-web' \
  3. --data 'config.proto=/usr/local/kong/hello.proto'

Protobuf definition (hello.proto):

  1. syntax = "proto2";
  2. package hello;
  3. service HelloService {
  4. rpc SayHello(HelloRequest) returns (HelloResponse);
  5. rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
  6. rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
  7. rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
  8. }
  9. message HelloRequest {
  10. optional string greeting = 1;
  11. }
  12. message HelloResponse {
  13. required string reply = 1;
  14. }

Upload the protobuf definition to your Kong Node:

  1. docker cp hello.proto kong-dp-host:/usr/local/kong/

Test your setup:

  1. curl -X POST kong-dp-host:8000/hello.HelloService/SayHello \
  2. --header 'x-grpc: true' \
  3. --header 'Content-Type: application/json' \
  4. --data '{"greeting":"kong2.1"}'