Çağrı Özer

Senior Software Developer @ Hurriyet

Protobuf ve GRPC

Tanıtım ve Demo

Protobuf

  • Nedir
  • Ne değildir
  • Nerede kullanırım
  • Nasıl kullanırım

Protobuf

Nedir

Yapılı datayı serileştirmek için bir yöntem

  • Esnek
  • Platform ve dil agnostik
  • Binary format
  • Hafif
  • Şema bazlı

Xml gibi, ama;

  • Daha küçük
  • Daha hızlı
  • Daha basit

Protobuf

.proto dosyası

  • Veri şemasını belirtir
  • Indexler değiştirilmedikçe geriye dönük tam uyumludur
  • Optional/required gibi keywordler ile ek fonksiyonelite eklenebilir

Protobuf

Ne değildir

  • İnsan tarafından okunamaz/düzenlenemez
  • .proto dosyası olmadan tek başına anlamı yoktur
  • Veri depolama için kullanılmamalı
  • Gümüş mermi değildir

Protobuf

Nerede kullanırım

  • Mesajlaşma protokolü olarak
  • Mikro servis iletişimi
  • Mobil uygulamalar
  • Serverlar arası iletişim

Mesajların insan okunabilir olmasının önemli olmadığı, paket boyutunu ile hızın kritik olduğu ve son kullanıcının tarayıcı olmadığı senaryolar

 

Protobuf

Nasıl kullanırım

Şu anda desteklenen diller: Java, Python, Objective-C, C++, Go, JavaNano, Ruby, C#

  • İstenilen formatta .proto dosyası oluşturulur
  • Protobuf derleyicisi indirilir (https://github.com/google/protobuf)
  • CLI aracı ile şema istenilen dilde sınıflara dönüştürülür

 

 

protoc --proto_path=. --csharp_out . .\addressbook.proto

Protobuf

Demo

GRPC

gRPC Remote Procedure Calls

Google'ın protobuf mimarisi üzerine kurduğu bir servis framework. 

  • Hızlı
  • Açık kaynak kodlu
  • Dil ve platform agnostik
  • Düşük gecikmeli
  • Ölçeklenebilir
  • Dağıtık
  • HTTP/2

GRPC

Kim kullanıyor?

  • Google
  • Docker
  • Square
  • Netflix
  • CoreOS
  • CockroachDb
  • Cisco

GRPC

Kullanım

service PhoneBookService
{
    rpc AddPerson(Person) returns (AddPersonReply) {}
    rpc GetPhoneBook(Empty) returns(AddressBook) {}
}
  • Derleyici çalıştırılır
protoc --proto_path=. --csharp_out .\AddressBook.Core\ --grpc_out .\AddressBook.Core\ .\addressbook.proto 
--plugin=protoc-gen-grpc=grpc_csharp_plugin.exe
  • Server ve Client kodları yazılır

GRPC

Desteklenen diller

  • C++
  • Java
  • Objective-C
  • Python
  • Ruby
  • Go
  • C#
  • Node.js.

GRPC

RPC çeşitleri

  • Basit RPC : Sıradan bir method çağrısı gibi
  • Server taraflı yayın akışı : Cevap istemciye akış halinde döner
  • İstemci taraflı yayın akışı : İstek servera akış halinde ulaşır
  • Çift taraflı yayın akışı: İstek ve cevap asenkron şekilde akar
rpc GetPerson(GetPersonRequest) returns (Person) {}
rpc GetPersonBiography(GetPersonRequest) returns (stream Biography) {}
rpc SavePersonBiography(stream biography) returns (StatusReply) {}
rpc GramarCheck(stream Text) returns (stream Text) {}

GRPC

Protobuf

By cagri.ozer@outlook.com

Protobuf

  • 556