SlideShare une entreprise Scribd logo
1  sur  38
Building your First gRPC Service
Why gRPC?
Why gRPC?
From gRPC’s website:
“gRPC is a modern open source high performance RPC framework that can run in
any environment. It can efficiently connect services in and across data centers with
pluggable support for load balancing, tracing, health checking and authentication. It
is also applicable in last mile of distributed computing to connect devices, mobile
applications and browsers to back end services.”
Benefits of gRPC
● Low latency
○ Lack of parsing call parameters from paths and query strings allows for faster services
● Full-duplex streaming
○ Both requests and responses can be optionally streamed - gRPC uses HTTP/2 by default
● Supports multiple data formats
○ Protobuf, JSON, XML, FlatBuffers, and Thrift (varying levels of support)
● Static Types & Versioned Service Interface
○ Alleviates headaches where fields may be an object or an array/usually is an int, but under
some circumstance is a string
○ Fields can be depreciated without negative downstream consequences
(when handled properly)
Supported Languages
● C/C++
● C#
● Dart
● Go
● Java
● Node.js
● PHP
● Python
● Ruby
● Web (with Envoy)
gRPC vs REST
gRPC
● Supports streaming APIs over HTTP/2
● Uses Messages
● Strong Typing
● Not as straightforward to call from the the
browser as REST
● Supports many types of encoding
(protocol buffers by default)
● Fields can be deprecated without causing
breaking changes
REST
● Request/Response model over HTTP/1.1
● Utilizes resources and verbs
● Serialization
● Easy to call from the browser
● Supports limited number of encoding
(JSON by default)
● Deprecating fields is a breaking change
that requires versioning
Protocol Buffers
Building a gRPC Service: Part One
What is a protocol buffer?
From Google:
Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing
structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be
structured once, then you can use special generated source code to easily write and read your structured
data to and from a variety of data streams and using a variety of languages.
In plain speak:
Protocol buffers are essentially a way to serialize a binary wire format message, where fields are
identified by order.
Protocol Buffers
gRPC uses Protocol Buffers as its default data format. To get started with gRPC,
you will need to create a .proto file that defines:
1. What services you will implement
2. The message definition for requests
3. The message definition for responses
Service Definition
Let’s create a service for running a pet store
(Lovingly borrowed from Swagger’s example
docs).
We’ll create endpoints for:
● Retrieving all pets
● Retrieving a pet by ID
● Creating a new pet
● Updating an existing pet
● Deleting a pet
We’ll define a pet as an entity with the following:
● id: unique identifier for the pet
● name: the pet’s name
● status: one of available, pending, or sold
The .proto file
Before we can write any code for our services,
we’ll need to define them in a .proto file.
The file will need to first define the syntax and
package name.
After that we can define what messages we’ll
send/receive and what services are available.
syntax = "proto3";
package petstore;
Creating the Pet Message
We’ve already determined a Pet is an entity with
the following:
● id: unique identifier for the pet
● name: the pet’s name
● status: one of available, pending, or sold
We’ll create a message that has three string
fields to represent this data.
By default, all fields are optionally. This will
allow us to use the same message for the
request and response for retrieving a pet.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Defining Messages
Messages can be composed of a mixture of
scalar and custom types.
Scalar types include:
double, float, int32, int64, uint32, uint64, sint32,
sint64, fixed32, fixed64, sfixed32, sfixed64, bool,
string, bytes
Numeric values default to zero, booleans to
false, and strings to an empty string.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Assigning Field Numbers
You’ll notice that each field in the message definition has a
unique number.
These fields numbers are used to identify your fields for the
binary parser and should not be changed once your
message is in use.
Fields 1-15 take one byte to encode, and should be reserved
for frequently occurring message elements.
Fields 16-2047 take two bytes to encode.
Deprecated fields should still be defined in your message
definition with their original field number.
message Pet {
string id = 1;
string name = 2;
string status = 3;
}
Enums
We wanted to limit status to be one of available,
pending, or sold. We can do this by defining an
enum.
In order to future proof our service, we’ll define
the default status as unknown in case we
decide to deprecate the field in the future.
enum Status {
UNKNOWN = 0;
AVAILABLE = 1;
PENDING = 2;
SOLD = 3;
}
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
Repeated Fields and Empty Messages
What if we want to return multiple pets? We can
do this by using the repeated key.
How about instances where we want to send
empty messages? We will still need to define a
message for those instances, but we will not
give it any fields.
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
message Pets {
repeated Pet pets = 1;
}
message Empty {}
Defining Services
Now that we’ve defined our messages, we’ll
need to define what services we’ll make
available.
We’ll create services for:
● Retrieving all pets
● Retrieving a pet by ID
● Creating a new pet
● Updating an existing pet
● Deleting a pet
service PetStore {
rpc GetAll(Empty) returns (Pets) {}
rpc GetPet(Pet) returns (Pet) {}
rpc CreatePet(Pet) returns (Pet) {}
rpc UpdatePet(Pet) returns (Pet) {}
rpc DeletePet(Pet) returns (Empty) {}
}
Putting it all Together
syntax = "proto3";
package petstore;
service PetStore {
rpc GetAll(Empty) returns (Pets) {}
rpc GetPet(Pet) returns (Pet) {}
rpc CreatePet(Pet) returns (Pet) {}
rpc UpdatePet(Pet) returns (Pet) {}
rpc DeletePet(Pet) returns (Empty) {}
}
message Empty {}
enum Status {
UNKNOWN = 0;
AVAILABLE = 1;
PENDING = 2;
SOLD = 3;
}
message Pet {
string id = 1;
string name = 2;
Status status = 3;
}
message Pets {
repeated Pet pets = 1;
}
Loading the Protobuf in Node
Building a gRPC Service: Part Two
Using Protobufs in Node
In Node, there are two options for using Protocol Buffers. Static generation or
dynamic loading.
With static generation, you’ll use the proto tool to load your protobuf definition and
create static JavaScript files that you’ll use for creating your server and client. This
is how protobufs are handled in most other supported languages.
With Node you also have the option to dynamically load your proto definition,
making it function similar to any other dependency you might have.
For our example, we’ll use dynamic loading.
Loading the Protobuf
You’ll need two modules to get started
● grpc
● @grpc/proto-loader
When loading the protocol definition, you’ll have a couple of options.
● keepCase: keep field casing instead of converting to camelcase
● longs: Should long numbers be treated as strings or numbers
● enums: Should enums use the string or numeric value
● defaults: Should default values be set
Loading the .proto file
const { resolve } = require('path')
const { loadPackageDefinition } = require('grpc')
const { loadSync } = require('@grpc/proto-loader')
const PETSTORE_PROTO_PATH = resolve(__dirname, 'petstore.proto')
const petstorePackageDefinition = loadSync(
PETSTORE_PROTO_PATH,
{
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
)
const { petstore } = loadPackageDefinition(petstorePackageDefinition)
module.exports = { petstore }
Creating the gRPC Server
Building a gRPC Service: Part Three
Creating the gRPC Server
To run a gRPC server, you will need to create one in Node.
const grpc = require('grpc')
const server = new grpc.Server()
Additionally, you must tell it what security to use and what port to run on. For our
example, we’ll use the simplest type of security: insecure.
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
Adding the Service Definition
We now have a running server, but it doesn’t actually do anything. Let’s assume
we have a local package that already contains all of the service logic. We’ll also
need to load the proto definition from before.
const services = require('./services')
const proto = require('../proto')
For our server to register as a valid petstore server for our protobuf definition, it
must implement all of the services we defined.
Full Server File
const grpc = require('grpc')
const proto = require('../proto')
const services = require('./services')
const server = new grpc.Server()
server.addService(proto.petstore.PetStore.service, {
getAll: services.getPets,
getPet: services.getPet,
createPet: services.createPet,
updatePet: services.updatePet,
deletePet: services.deletePet
})
server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure())
server.start()
Writing Services
A service will receive two arguments, a call and a callback. The call contains the
request along with some other metadata. The callback has a signature of (error,
message).
const getPet = (call, callback) => {
const pet = db.getPet(call.request)
callback(null, pet)
}
Error Handling
We have a service that looks up a pet by ID, but what if we don’t have a record for that pet? Should we
send back a pet message with defaulted fields? While that is an option, gRPC has standardized status
codes, much like REST. We can build standardized status messages using the @grpc/grpc-js package.
const { status } = require('grpc')
const grpc = require('@grpc/grpc-js')
const getPet = (call, callback) => {
const pet = db.getPet(call.request)
if (!pet) {
const err = new grpc.StatusBuilder().withCode(status.NOT_FOUND).withDetails('Pet Not Found').build()
callback(err)
return
}
callback(null, pet)
}
Unimplemented Methods
As stated earlier, for our server to be of type petstore, it must implement all five
services, but what if we didn’t want to implement one of them? gRPC provides a
status code for unimplemented services.
const { status } = require('grpc')
const grpc = require('@grpc/grpc-js')
const deletePet = (call, callback) => {
const err = new grpc.StatusBuilder().withCode(status.UNIMPLEMENTED).withDetails('Service Not Implemented').build()
callback(err)
}
Available Status Messages
● Status (Closest HTTP Equivalent)
● OK (200)
● CANCELLED (499)
● UNKNOWN (500)
● INVALID_ARGUMENT (400)
● DEADLINE_EXCEEDED (504)
● NOT_FOUND (404)
● ALREADY_EXISTS (409)
● PERMISSION_DENIED (403)
● UNAUTHENTICATED (401)
● RESOURCE_EXHAUSTED (429)
● FAILED_PRECONDITION (400)
● ABORTED (409)
● OUT_OF_RANGE (400)
● UNIMPLEMENTED (501)
● INTERNAL (500)
● UNAVAILABLE (503)
● DATA_LOSS (500)
Creating the gRPC Client
Building a gRPC Service: Part Four
Creating the gRPC Client
Creating a gRPC client is fairly straightforward. You will just need to load a
protocol definition and know where the gRPC server is running.
const { credentials } = require('grpc')
const proto = require('../proto')
const client = new proto.petstore.PetStore('localhost:50051', credentials.createInsecure())
From there, you can make calls to your server and handle any errors.
client.getPet({ id:'23' }, (err, pet) => {
if (err) {
console.log(err.details) // Prints the error message, in this case “Pet Not Found”
}
console.log(pet)
})
Advanced Topics
Health Checks
● gRPC provides a standard protobuf definition for health checks.
● This defines two methods, Check and Watch.
○ Check is standard call. Watch is a streaming call.
● Returns one of three statuses: UNKNOWN, SERVING, NOT_SERVING
● The client can decide how to handle these statuses
○ For instance, if the health check returns NOT_SERVING you can fail the request, or queue it
and try again with an exponential backoff
Implementing Multiple Server Types
A single server can implement multiple server types. For instance, you can
implement both petstore and a health check server.
const server = new grpc.Server()
server.addService(proto.petstore.PetStore.service, {
// service functions
})
server.addService(proto.health.Health.service, {
// service functions
})
Importing Proto Definitions
You can reuse definitions from one proto file by importing them in another.
import "myproject/other_protos.proto";
For well known types, you can use the “Any” type which takes a definition URL for
deserializing arbitrary JSON and the value
{
"@type": "type.googleapis.com/google.protobuf.Duration",
"value": "1.212s"
}
Security
gRPC supports 3 security schemes:
1. Unsecure
2. SSL/TLS
3. Token-based authentication with Google (should only be used with Google
services)
Questions?

Contenu connexe

Tendances

Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersShiju Varghese
 
Kafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsKafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsSlim Baltagi
 
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t... Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...AboutYouGmbH
 
What is gRPC introduction gRPC Explained
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explainedjeetendra mandal
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Codemotion
 
SeaweedFS introduction
SeaweedFS introductionSeaweedFS introduction
SeaweedFS introductionchrislusf
 
Kafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupKafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupMingmin Chen
 
Kubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy AgentKubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy AgentCloudOps2005
 
gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!Alex Borysov
 
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceZeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceDatabricks
 
The Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersThe Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersSATOSHI TAGOMORI
 
A crash course in CRUSH
A crash course in CRUSHA crash course in CRUSH
A crash course in CRUSHSage Weil
 
Inter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPCInter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPCShiju Varghese
 
Deep dive into Kubernetes Networking
Deep dive into Kubernetes NetworkingDeep dive into Kubernetes Networking
Deep dive into Kubernetes NetworkingSreenivas Makam
 
Kafka Streams: What it is, and how to use it?
Kafka Streams: What it is, and how to use it?Kafka Streams: What it is, and how to use it?
Kafka Streams: What it is, and how to use it?confluent
 
Prometheus Overview
Prometheus OverviewPrometheus Overview
Prometheus OverviewBrian Brazil
 
Writing HTTP Middleware In Go
Writing HTTP Middleware In GoWriting HTTP Middleware In Go
Writing HTTP Middleware In GoShiju Varghese
 

Tendances (20)

Power-up services with gRPC
Power-up services with gRPCPower-up services with gRPC
Power-up services with gRPC
 
Building High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol BuffersBuilding High Performance APIs In Go Using gRPC And Protocol Buffers
Building High Performance APIs In Go Using gRPC And Protocol Buffers
 
Building microservices with grpc
Building microservices with grpcBuilding microservices with grpc
Building microservices with grpc
 
Kafka Streams for Java enthusiasts
Kafka Streams for Java enthusiastsKafka Streams for Java enthusiasts
Kafka Streams for Java enthusiasts
 
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t... Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.t...
 
What is gRPC introduction gRPC Explained
What is gRPC introduction gRPC ExplainedWhat is gRPC introduction gRPC Explained
What is gRPC introduction gRPC Explained
 
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
Introduction to gRPC: A general RPC framework that puts mobile and HTTP/2 fir...
 
SeaweedFS introduction
SeaweedFS introductionSeaweedFS introduction
SeaweedFS introduction
 
gRPC
gRPCgRPC
gRPC
 
Kafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetupKafka Practices @ Uber - Seattle Apache Kafka meetup
Kafka Practices @ Uber - Seattle Apache Kafka meetup
 
Kubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy AgentKubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy Agent
 
gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!gRPC vs REST: let the battle begin!
gRPC vs REST: let the battle begin!
 
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a ServiceZeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
Zeus: Uber’s Highly Scalable and Distributed Shuffle as a Service
 
The Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and ContainersThe Patterns of Distributed Logging and Containers
The Patterns of Distributed Logging and Containers
 
A crash course in CRUSH
A crash course in CRUSHA crash course in CRUSH
A crash course in CRUSH
 
Inter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPCInter-Process Communication in Microservices using gRPC
Inter-Process Communication in Microservices using gRPC
 
Deep dive into Kubernetes Networking
Deep dive into Kubernetes NetworkingDeep dive into Kubernetes Networking
Deep dive into Kubernetes Networking
 
Kafka Streams: What it is, and how to use it?
Kafka Streams: What it is, and how to use it?Kafka Streams: What it is, and how to use it?
Kafka Streams: What it is, and how to use it?
 
Prometheus Overview
Prometheus OverviewPrometheus Overview
Prometheus Overview
 
Writing HTTP Middleware In Go
Writing HTTP Middleware In GoWriting HTTP Middleware In Go
Writing HTTP Middleware In Go
 

Similaire à Building your First gRPC Service

CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsTim Burks
 
Microservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPCMicroservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPCWSO2
 
Building Services With gRPC, Docker and Go
Building Services With gRPC, Docker and GoBuilding Services With gRPC, Docker and Go
Building Services With gRPC, Docker and GoMartin Kess
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to ThriftDvir Volk
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQNahidul Kibria
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleTim Burks
 
Rockin' Protobuf in Ruby
Rockin' Protobuf in RubyRockin' Protobuf in Ruby
Rockin' Protobuf in RubyBJ Neilsen
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxowww.ixxo.io
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Ovidiu Farauanu
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通炜龙 何
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpcJohnny Pork
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdfsupport58
 
Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Tim Burks
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyGabor Paller
 
CRUD Operation With Dgraph
CRUD Operation With DgraphCRUD Operation With Dgraph
CRUD Operation With DgraphKnoldus Inc.
 
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays
 

Similaire à Building your First gRPC Service (20)

CocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIsCocoaConf: The Language of Mobile Software is APIs
CocoaConf: The Language of Mobile Software is APIs
 
Microservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPCMicroservices Communication Patterns with gRPC
Microservices Communication Patterns with gRPC
 
Building Services With gRPC, Docker and Go
Building Services With gRPC, Docker and GoBuilding Services With gRPC, Docker and Go
Building Services With gRPC, Docker and Go
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to Thrift
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQ
 
What I learned about APIs in my first year at Google
What I learned about APIs in my first year at GoogleWhat I learned about APIs in my first year at Google
What I learned about APIs in my first year at Google
 
Rockin' Protobuf in Ruby
Rockin' Protobuf in RubyRockin' Protobuf in Ruby
Rockin' Protobuf in Ruby
 
Golang proto buff_ixxo
Golang proto buff_ixxoGolang proto buff_ixxo
Golang proto buff_ixxo
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Thrfit从入门到精通
Thrfit从入门到精通Thrfit从入门到精通
Thrfit从入门到精通
 
Rpc mechanism
Rpc mechanismRpc mechanism
Rpc mechanism
 
project_docs
project_docsproject_docs
project_docs
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpc
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
 
Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)Creating Great REST and gRPC API Experiences (in Swift)
Creating Great REST and gRPC API Experiences (in Swift)
 
Ice
IceIce
Ice
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
 
Connect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low EnergyConnect your Android to the real world with Bluetooth Low Energy
Connect your Android to the real world with Bluetooth Low Energy
 
CRUD Operation With Dgraph
CRUD Operation With DgraphCRUD Operation With Dgraph
CRUD Operation With Dgraph
 
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
apidays LIVE Helsinki - Implementing OpenAPI and GraphQL Services with gRPC b...
 

Dernier

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Dernier (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Building your First gRPC Service

  • 1. Building your First gRPC Service
  • 3. Why gRPC? From gRPC’s website: “gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to back end services.”
  • 4. Benefits of gRPC ● Low latency ○ Lack of parsing call parameters from paths and query strings allows for faster services ● Full-duplex streaming ○ Both requests and responses can be optionally streamed - gRPC uses HTTP/2 by default ● Supports multiple data formats ○ Protobuf, JSON, XML, FlatBuffers, and Thrift (varying levels of support) ● Static Types & Versioned Service Interface ○ Alleviates headaches where fields may be an object or an array/usually is an int, but under some circumstance is a string ○ Fields can be depreciated without negative downstream consequences (when handled properly)
  • 5. Supported Languages ● C/C++ ● C# ● Dart ● Go ● Java ● Node.js ● PHP ● Python ● Ruby ● Web (with Envoy)
  • 6. gRPC vs REST gRPC ● Supports streaming APIs over HTTP/2 ● Uses Messages ● Strong Typing ● Not as straightforward to call from the the browser as REST ● Supports many types of encoding (protocol buffers by default) ● Fields can be deprecated without causing breaking changes REST ● Request/Response model over HTTP/1.1 ● Utilizes resources and verbs ● Serialization ● Easy to call from the browser ● Supports limited number of encoding (JSON by default) ● Deprecating fields is a breaking change that requires versioning
  • 7. Protocol Buffers Building a gRPC Service: Part One
  • 8. What is a protocol buffer? From Google: Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. In plain speak: Protocol buffers are essentially a way to serialize a binary wire format message, where fields are identified by order.
  • 9. Protocol Buffers gRPC uses Protocol Buffers as its default data format. To get started with gRPC, you will need to create a .proto file that defines: 1. What services you will implement 2. The message definition for requests 3. The message definition for responses
  • 10. Service Definition Let’s create a service for running a pet store (Lovingly borrowed from Swagger’s example docs). We’ll create endpoints for: ● Retrieving all pets ● Retrieving a pet by ID ● Creating a new pet ● Updating an existing pet ● Deleting a pet We’ll define a pet as an entity with the following: ● id: unique identifier for the pet ● name: the pet’s name ● status: one of available, pending, or sold
  • 11. The .proto file Before we can write any code for our services, we’ll need to define them in a .proto file. The file will need to first define the syntax and package name. After that we can define what messages we’ll send/receive and what services are available. syntax = "proto3"; package petstore;
  • 12. Creating the Pet Message We’ve already determined a Pet is an entity with the following: ● id: unique identifier for the pet ● name: the pet’s name ● status: one of available, pending, or sold We’ll create a message that has three string fields to represent this data. By default, all fields are optionally. This will allow us to use the same message for the request and response for retrieving a pet. message Pet { string id = 1; string name = 2; string status = 3; }
  • 13. Defining Messages Messages can be composed of a mixture of scalar and custom types. Scalar types include: double, float, int32, int64, uint32, uint64, sint32, sint64, fixed32, fixed64, sfixed32, sfixed64, bool, string, bytes Numeric values default to zero, booleans to false, and strings to an empty string. message Pet { string id = 1; string name = 2; string status = 3; }
  • 14. Assigning Field Numbers You’ll notice that each field in the message definition has a unique number. These fields numbers are used to identify your fields for the binary parser and should not be changed once your message is in use. Fields 1-15 take one byte to encode, and should be reserved for frequently occurring message elements. Fields 16-2047 take two bytes to encode. Deprecated fields should still be defined in your message definition with their original field number. message Pet { string id = 1; string name = 2; string status = 3; }
  • 15. Enums We wanted to limit status to be one of available, pending, or sold. We can do this by defining an enum. In order to future proof our service, we’ll define the default status as unknown in case we decide to deprecate the field in the future. enum Status { UNKNOWN = 0; AVAILABLE = 1; PENDING = 2; SOLD = 3; } message Pet { string id = 1; string name = 2; Status status = 3; }
  • 16. Repeated Fields and Empty Messages What if we want to return multiple pets? We can do this by using the repeated key. How about instances where we want to send empty messages? We will still need to define a message for those instances, but we will not give it any fields. message Pet { string id = 1; string name = 2; Status status = 3; } message Pets { repeated Pet pets = 1; } message Empty {}
  • 17. Defining Services Now that we’ve defined our messages, we’ll need to define what services we’ll make available. We’ll create services for: ● Retrieving all pets ● Retrieving a pet by ID ● Creating a new pet ● Updating an existing pet ● Deleting a pet service PetStore { rpc GetAll(Empty) returns (Pets) {} rpc GetPet(Pet) returns (Pet) {} rpc CreatePet(Pet) returns (Pet) {} rpc UpdatePet(Pet) returns (Pet) {} rpc DeletePet(Pet) returns (Empty) {} }
  • 18. Putting it all Together syntax = "proto3"; package petstore; service PetStore { rpc GetAll(Empty) returns (Pets) {} rpc GetPet(Pet) returns (Pet) {} rpc CreatePet(Pet) returns (Pet) {} rpc UpdatePet(Pet) returns (Pet) {} rpc DeletePet(Pet) returns (Empty) {} } message Empty {} enum Status { UNKNOWN = 0; AVAILABLE = 1; PENDING = 2; SOLD = 3; } message Pet { string id = 1; string name = 2; Status status = 3; } message Pets { repeated Pet pets = 1; }
  • 19. Loading the Protobuf in Node Building a gRPC Service: Part Two
  • 20. Using Protobufs in Node In Node, there are two options for using Protocol Buffers. Static generation or dynamic loading. With static generation, you’ll use the proto tool to load your protobuf definition and create static JavaScript files that you’ll use for creating your server and client. This is how protobufs are handled in most other supported languages. With Node you also have the option to dynamically load your proto definition, making it function similar to any other dependency you might have. For our example, we’ll use dynamic loading.
  • 21. Loading the Protobuf You’ll need two modules to get started ● grpc ● @grpc/proto-loader When loading the protocol definition, you’ll have a couple of options. ● keepCase: keep field casing instead of converting to camelcase ● longs: Should long numbers be treated as strings or numbers ● enums: Should enums use the string or numeric value ● defaults: Should default values be set
  • 22. Loading the .proto file const { resolve } = require('path') const { loadPackageDefinition } = require('grpc') const { loadSync } = require('@grpc/proto-loader') const PETSTORE_PROTO_PATH = resolve(__dirname, 'petstore.proto') const petstorePackageDefinition = loadSync( PETSTORE_PROTO_PATH, { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true } ) const { petstore } = loadPackageDefinition(petstorePackageDefinition) module.exports = { petstore }
  • 23. Creating the gRPC Server Building a gRPC Service: Part Three
  • 24. Creating the gRPC Server To run a gRPC server, you will need to create one in Node. const grpc = require('grpc') const server = new grpc.Server() Additionally, you must tell it what security to use and what port to run on. For our example, we’ll use the simplest type of security: insecure. server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()) server.start()
  • 25. Adding the Service Definition We now have a running server, but it doesn’t actually do anything. Let’s assume we have a local package that already contains all of the service logic. We’ll also need to load the proto definition from before. const services = require('./services') const proto = require('../proto') For our server to register as a valid petstore server for our protobuf definition, it must implement all of the services we defined.
  • 26. Full Server File const grpc = require('grpc') const proto = require('../proto') const services = require('./services') const server = new grpc.Server() server.addService(proto.petstore.PetStore.service, { getAll: services.getPets, getPet: services.getPet, createPet: services.createPet, updatePet: services.updatePet, deletePet: services.deletePet }) server.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()) server.start()
  • 27. Writing Services A service will receive two arguments, a call and a callback. The call contains the request along with some other metadata. The callback has a signature of (error, message). const getPet = (call, callback) => { const pet = db.getPet(call.request) callback(null, pet) }
  • 28. Error Handling We have a service that looks up a pet by ID, but what if we don’t have a record for that pet? Should we send back a pet message with defaulted fields? While that is an option, gRPC has standardized status codes, much like REST. We can build standardized status messages using the @grpc/grpc-js package. const { status } = require('grpc') const grpc = require('@grpc/grpc-js') const getPet = (call, callback) => { const pet = db.getPet(call.request) if (!pet) { const err = new grpc.StatusBuilder().withCode(status.NOT_FOUND).withDetails('Pet Not Found').build() callback(err) return } callback(null, pet) }
  • 29. Unimplemented Methods As stated earlier, for our server to be of type petstore, it must implement all five services, but what if we didn’t want to implement one of them? gRPC provides a status code for unimplemented services. const { status } = require('grpc') const grpc = require('@grpc/grpc-js') const deletePet = (call, callback) => { const err = new grpc.StatusBuilder().withCode(status.UNIMPLEMENTED).withDetails('Service Not Implemented').build() callback(err) }
  • 30. Available Status Messages ● Status (Closest HTTP Equivalent) ● OK (200) ● CANCELLED (499) ● UNKNOWN (500) ● INVALID_ARGUMENT (400) ● DEADLINE_EXCEEDED (504) ● NOT_FOUND (404) ● ALREADY_EXISTS (409) ● PERMISSION_DENIED (403) ● UNAUTHENTICATED (401) ● RESOURCE_EXHAUSTED (429) ● FAILED_PRECONDITION (400) ● ABORTED (409) ● OUT_OF_RANGE (400) ● UNIMPLEMENTED (501) ● INTERNAL (500) ● UNAVAILABLE (503) ● DATA_LOSS (500)
  • 31. Creating the gRPC Client Building a gRPC Service: Part Four
  • 32. Creating the gRPC Client Creating a gRPC client is fairly straightforward. You will just need to load a protocol definition and know where the gRPC server is running. const { credentials } = require('grpc') const proto = require('../proto') const client = new proto.petstore.PetStore('localhost:50051', credentials.createInsecure()) From there, you can make calls to your server and handle any errors. client.getPet({ id:'23' }, (err, pet) => { if (err) { console.log(err.details) // Prints the error message, in this case “Pet Not Found” } console.log(pet) })
  • 34. Health Checks ● gRPC provides a standard protobuf definition for health checks. ● This defines two methods, Check and Watch. ○ Check is standard call. Watch is a streaming call. ● Returns one of three statuses: UNKNOWN, SERVING, NOT_SERVING ● The client can decide how to handle these statuses ○ For instance, if the health check returns NOT_SERVING you can fail the request, or queue it and try again with an exponential backoff
  • 35. Implementing Multiple Server Types A single server can implement multiple server types. For instance, you can implement both petstore and a health check server. const server = new grpc.Server() server.addService(proto.petstore.PetStore.service, { // service functions }) server.addService(proto.health.Health.service, { // service functions })
  • 36. Importing Proto Definitions You can reuse definitions from one proto file by importing them in another. import "myproject/other_protos.proto"; For well known types, you can use the “Any” type which takes a definition URL for deserializing arbitrary JSON and the value { "@type": "type.googleapis.com/google.protobuf.Duration", "value": "1.212s" }
  • 37. Security gRPC supports 3 security schemes: 1. Unsecure 2. SSL/TLS 3. Token-based authentication with Google (should only be used with Google services)