Upgrade to Pro — share decks privately, control downloads, hide ads and more …

gRPC入門

 gRPC入門

Taiga Sakaguchi

November 19, 2022
Tweet

More Decks by Taiga Sakaguchi

Other Decks in Technology

Transcript

  1. gRPC
    入門
    Taiga Sakaguchi
    1

    View Slide

  2. Sakaguchi Taiga
    教員→Web
    エンジニア
    歴3

    バックエンドエンジニア
    都内メガベンチャーに勤務
    2

    View Slide

  3. gRPC
    とは
    Google
    によって開発されたRPC
    フレームワーク
    3

    View Slide

  4. RPC
    とは
    Remote Procedure Call
    の頭字語(遠隔手続き呼び出し)
    ネットワークを通じて別のコンピュータ上で動作するソフトウェア
    の関数を呼び出すための仕様
    4

    View Slide

  5. 一般的な関数呼び出し
    5

    View Slide

  6. RPC
    における関数呼び出し
    6

    View Slide

  7. RPC
    の何が嬉しいのか
    ローカルの関数かリモートの関数かを意識せずにコードを書くこと
    ができる
    7

    View Slide

  8. type Server struct {}
    type User struct{
    ID string
    }
    func (s *Server) GetUser(id string) *User {}
    type Client struct {}
    func (c *Client)DoSomething() {
    u := c.GetUser("userid")
    }
    8

    View Slide

  9. REST
    だったら
    type Server struct {}
    func main() {
    r := gin.Default()
    r.GET("/user/:id", getUser)
    r.Run()
    }
    func getUser(c *gin.Context) {}
    type Client struct {}
    func (c *Client) DoSomething() {
    resp, err := http.Get("http://user/1")
    }
    9

    View Slide

  10. https://grpc.io/docs/what-is-grpc/introduction/
    10

    View Slide

  11. Protocol Buffers
    Google
    により開発
    IDL
    (インターフェース定義言語)
    通信や永続化での利用を目的としたシリアライズフォーマット
    コードやDocs
    など自動生成できる
    11

    View Slide

  12. syntax = "proto3";
    service User {
    rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
    }
    message GetUserRequest {
    string id = 1;
    }
    message GetUserResponse {
    string id = 1;
    string name = 2;
    }
    12

    View Slide

  13. サーバー用の生成コード
    // ...
    type UserServer interface {
    GetUser(context.Context, *GetUserRequest) (*GetUserResponse, error)
    mustEmbedUnimplementedUserServer()
    }
    // ...
    13

    View Slide

  14. クライアント用の生成コード
    // ...
    type UserClient interface {
    GetUser(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error)
    }
    func NewUserClient(cc grpc.ClientConnInterface) UserClient {
    return &userClient{cc}
    }
    // ...
    14

    View Slide

  15. ここでクイズ
    gRPC
    の「g
    」の意味はなんでしょうか?
    1. Google
    2. Good
    3. Great
    4. Game
    15

    View Slide

  16. ここでクイズ
    gRPC
    の「g
    」の意味はなんでしょうか?
    1. Google
    2. Good
    3. Great
    4. Game
    16

    View Slide

  17. gRPC
    の「g
    」はバージョンごとに意味がかわるらしい
    https://github.com/grpc/grpc/blob/master/doc/g_stands_for.md
    17

    View Slide

  18. gRPC
    のメリット
    HTTP/2
    で通信
    Protocol Buffers
    によるシリアライズ
    ストリーミング処理
    多くの言語に対応(Go, Java, Kotlin, Node, PHP, Python, Ruby,...

    18

    View Slide

  19. 4
    つの通信方式
    Unary RPC
    Server streaming RPC
    Client streaming RPC
    Bidirectional streaming PRC
    19

    View Slide

  20. Unary RPC
    1 Request - 1 Response

    20

    View Slide

  21. Server streaming RPC
    1 Request - N Response
    例)push
    通知など

    21

    View Slide

  22. Client streaming RPC
    N Request - 1 Response
    例)データを分割して複数回に分けてアップロード

    22

    View Slide

  23. Bidirectional streaming PRC
    WebSocket
    のような双方向通
    例)
    チャット
    23

    View Slide

  24. gRPC
    のデメリット
    PRC
    の設計に気をつけないと破綻する
    実装に時間がかかる
    ブラウザでのサポートが十分でない
    24

    View Slide

  25. gRPC
    が向いているケース
    Microservice
    プライベートなAPI
    モバイル、IoT
    25

    View Slide

  26. ブラウザ
    -
    サーバー間で導入するには
    grpc-web
    grpc-gateway
    connect
    26

    View Slide

  27. grpc-web
    27

    View Slide

  28. grpc-gateway
    28

    View Slide

  29. connect
    https://connect.build/
    29

    View Slide

  30. connect
    30

    View Slide

  31. 参考
    https://grpc.io/
    https://zenn.dev/hsaki/books/golang-grpc-starting
    https://christina04.hatenablog.com/entry/2017/11/13/190000
    https://qiita.com/gold-kou/items/a1cc2be6045723e242eb
    31

    View Slide

  32. 32

    View Slide