Cloud Native Communication Patterns With GRPC
Cloud Native Communication Patterns With GRPC
with gRPC
Kasun Indrasiri
Author “gRPC Up and Running” and
“Microservices for Enterprise”
About Me
● Author “gRPC Up & Running”, “Microservices for Enterprise”
● Product Manager/Senior Director at WSO2.
● Committer and PMC member at Apache Software Foundation.
● Founder “Bay area Microservices, APIs and Integration” meetup
group.
What is gRPC?
● Modern Inter-process communication technology.
● Invoking remote functions as easy as making a local function invocation.
● Contract-first.
● Binary messaging on the wire on top of HTTP2
● Polyglot.
Fundamentals of gRPC - Service Definition
syntax = "proto3";
● Defines the business capabilities of package ecommerce;
define services. }
message Product {
● Protocol Buffers : string id = 1;
○ A language-agnostic, platform-neutral, string name = 2;
ProductInfo.proto
Fundamentals of gRPC - gRPC Service
// AddProduct implements ecommerce.AddProduct
● gRPC service implements the func (s *server) AddProduct(ctx context.Context,
ProductInfoClient.java
Why gRPC?
● Efficient.
● Strict specification and well-defined service contracts.
● Strongly Typed.
● Polyglot.
● Duplex Streaming.
● Native integration with cloud native ecosystem.
gRPC vs OpenAPI/REST vs GraphQL
REST/OAS
Code generation Optional, via third party libs Required and natively Optional
supported.
Streaming No first class support First class support No first class support
service OrderManagement {
rpc getOrder(google.protobuf.StringValue) returns (Order);
}
message Order { ... }
} c := pb.NewOrderManagementClient(conn)
retrievedOrder , err := c.GetOrder(ctx,
&wrapper.StringValue{Value: "106"})
Server Streaming RPC
● Server sends back a sequence of
responses(stream) after getting the
client’s request message. order_mgt.proto
(google.protobuf.StringValue);
}
return stream.SendAndClose(&wrapper.StringValue{
Value: "Orders processed " + ordersStr})
} ...
Bidirectional-Streaming RPC
● Client is sending a request to the
server as a stream of messages.
● Server also responds with a stream
order_mgt.proto
of messages. rpc processOrders(stream google.protobuf.StringValue)
clientDeadline := time.Now().Add(time.Duration(2 *
time.Second))
ctx, cancel := context.WithDeadline(context.Background(),
clientDeadline)
// Invoke RPC
Metadata
● Information directly related to the service’s
business logic and consumer is part of the
remote method invocation arguments.
● Use Metadata to share information about the
RPC calls that are not related to the business
context of the RPC (e.g. Security Headers)
● Structured as K-V pairs.
● Exchanged as gRPC headers.
Multiplexing
● Running multiple gRPC services on the
same gRPC server.
grpcServer := grpc.NewServer()
syntax = "proto3";
package ecommerce.v1;
:method POST
:path /ecommerce.v1.OrderManagement>/addOrder
Extending Service Definition Service Options
import "google/protobuf/descriptor.proto" ;
runtime/implementation. }
service OrderManagement {
option(oauth2Provider) = "https: //localhost:9444/oauth2/introspect";
}
REST/Open API ←→ gRPC Bridge
● gRPC Gateway : REST/HTTP 1.1 -> gRPC Bridge.
● Also known as HTTP/JSON Transcoding for gRPC
● gRPC gateway plug-in enables the protocol buffer compiler to read the gRPC
service definition and generate a reverse proxy server, which translates a
RESTful JSON API into gRPC.
import "google/api/annotations.proto" ;
service OrderManagement {
rpc addOrder (Order) returns (google .protobuf .StringValue )
{
option (google.api.http) = {
post: "/v1/order"
body: "*"
};
}
}
Resources
● gRPC Up and Running Book.
○ Gives you a comprehensive understanding of gRPC.
○ gRPC communication patterns and advanced concepts.
○ Running gRPC in production.
○ Dozens of samples written in Go and Java.
● Use cases and source code in Java and Go -
https://grpc-up-and-running.github.io/
● grpc.io
Thank You