Git Product home page Git Product logo

learn-grpc's Introduction

gRPC

Definisi

gRPC merupakan metode pendistribusian data melalui sebuah protofile atau stub.

Kenapa Harus Belajar gRPC?

  • Lebih simple dibanding REST dari segi banyaknya HTTP Method
  • Performa lebih baik dibanding REST (katanya)
  • Terdapat file proto yang dapat menjadi API Contract antara client dan server

Kekurangan gRPC

  • harus ada file Proto
  • payload yang lebih banyak

di REST:

{
  "status": "success",
  "statusCode": "200",
  "message": "Success Creating new Book called Dunia Shopie with book id 53e8dad6-4600-478d-8c14-7b85d483d0a4"
}

di gRPC:

{
  "status": "success",
  "_status": "status",
  "statusCode": "200",
  "_statusCode": "statusCode",
  "message": "Success Creating new Book called Dunia Shopie with book id 53e8dad6-4600-478d-8c14-7b85d483d0a4",
  "_message": "message"
}

Proto File

  • Merupakan file yang mendefinisikan service apa saja yang akan dibuat.
  • Setiap service memiliki message parameter dan message response.
  • Dalam membuat protofile, perlu mendefinisikan proto versi berapa dan masuk ke package apa.
  • Dalam membuat message, terdapat 3 tipe variable. required, optional, repeated. Namun, lebih baik menghindari penggunaan required karena ketika suatu waktu kita mengubah required ke optional, dari sisi client akan membaca bahwa parameter tersebut tetap required. Sehingga, akan menimbulkan error. Lebih baik, validasi required atau tidak dihandle di sisi aplikasi.
  • angka dalam message merupakan urutan variable.

Cara Implementasi

  1. Buat "interface" service di dalam protofile.
  2. tentukan struktur data parameter dan returnnya.
  3. compile proto kita menjadi file pb yang berfungsi sebagai stub server kita. Kita dapat menggunakan command:
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
modules/books/proto/books.proto //lokasi file proto
  1. Implementasikan service yang sudah kita buat di file proto tadi. Contoh implementasinya bisa dilihat di modules/books/repositories.go
  2. Buat server dan masukkan service yang sudah diimplementasikan tadi ke server agar terbaca.
  3. panggil service yang sudah kita buat dengan gRPC-client atau dengan menggunakan bantuan aplikasi bloomRPC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.