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

gRPC on Spring Boot

Kenta Suzuki
November 17, 2017

gRPC on Spring Boot

会社のTech Talkで話しました

Kenta Suzuki

November 17, 2017
Tweet

More Decks by Kenta Suzuki

Other Decks in Technology

Transcript

  1. gRPC
    on
    Spring Boot (Kotlin)
    2017/11/17 M3 Tech Talk #82
    @suusan2go

    View full-size slide

  2. Զ is ୭
    • Name

    Kenta Suzuki
    • like

    Rails / Ruby / JS(React / ES next) / Golang

    ࠷ۙ͸Kotlin & Spring Boot
    • ͦͷଞ

    ່͕ՄѪ͍͜ͱʹఆධ͕͋Γ·͢

    Twitter
    GitHub
    @suusan2go

    View full-size slide

  3. gRPC Overview
    • In gRPC a client application can directly call methods on a server application
    on a different machine.
    • By default gRPC uses protocol buffers, Google’s mature open source
    mechanism for serializing structured data.

    View full-size slide

  4. Working with Protocol
    Buffers
    • Define data structures and RPC methods( with parameters
    and return types)
    RPC method
    Request Type
    Return Type

    View full-size slide

  5. Generate server and client
    code from Protocol Buffer
    Java
    • Server Side
    • Extend auto-generated class and implement it.
    • Client Side
    • Create Stub object and set gRPC server endpoint, and call RPC method.

    View full-size slide

  6. Client sample
    gRPC Server Endpoint
    Create Request Object

    View full-size slide

  7. https://speakerdeck.com/thesandlord/grpc-vs-rest-api-strat-2016

    View full-size slide

  8. gRPC Summary
    • Auto code generation for client and server.
    • Supports many language.
    • Faster than REST ( using HTTP/2 and Protocol Buffers).
    • type-safety
    • Do no have to worry about RESTful API Designing.

    (URL, HTTP method, status code,,,)

    View full-size slide

  9. How to use gRPC
    on
    Spring Boot ?

    View full-size slide

  10. https://github.com/LogNet/
    grpc-spring-boot-starter
    • Auto-configures and runs the embedded
    gRPC server with @GRpcService-enabled
    beans as part of spring-boot application.
    gRPC Server
    Spring Boot

    View full-size slide

  11. How to build application
    on
    gRPC x Spring Boot ?

    View full-size slide

  12. How to handle Exception?(1)
    • When gRPC server throws Exception, client
    just receive exception like below.

    View full-size slide

  13. How to handle Exception?(2)
    • Use gRPC Interceptor to catch exception and
    return proper error to client.
    ࢀߟ http://blog.soushi.me/entry/2017/08/18/234615

    View full-size slide

  14. How to handle Exception?(3)
    • Use gRPC Interceptor to catch exception and
    return proper error to client.
    ࢀߟ http://blog.soushi.me/entry/2017/08/18/234615

    View full-size slide

  15. How to authenticate user?(1)
    • gRPC provides simple authentication API

    ( SSL/TLS, Token-based authentication with Google)
    • But I think this is not for application level
    authentication. (this is a gRPC connection or
    channel authentication.)
    • We can use Metadata for application level
    authentication.

    View full-size slide

  16. How to authenticate user?(2)
    • Use Interceptor for Authentication.
    • This interceptor check token for every gRPC request.
    ࢀߟ: https://eng.revinate.com/2017/11/07/grpc-spring-security.html

    View full-size slide

  17. How to authenticate user?(3)
    • Client side, Set metadata when call gRPC
    method.

    View full-size slide

  18. But we still need REST
    endpoints…
    • Currently we can’t use gRPC directly on
    browser. 

    ※ grpc-web provides client and proxy server implementation for running gRPC
    on browser (gRPC-Web protocol is a cutting-edge spec that enables invoking gRPC
    services from modern browsers.).
    • Also, we have to support internal legacy
    services using REST.
    • We can use grpc-gateway for REST Client.

    View full-size slide

  19. grpc-gateway
    • grpc-gateway provides proxy implementation from Protocol IDL(.proto files).
    • grpc-gateway implemented by Golang. But we can use another language
    for gRPC Server ! (e.q. Spring Boot)

    https://github.com/grpc-ecosystem/grpc-gateway

    View full-size slide

  20. Documentation and code
    generation for REST client
    • grpc-gateway supports swagger definitions.
    • So we can generate REST client and API
    document automatically.

    View full-size slide

  21. Summary
    • gRPC is ready for production ! ( I think)
    • InterceptorͱMetadataΛཧղ͢Ε͹ɺΞϓϦέʔγϣϯϨϕϧͰ΍Γ͍ͨ͜ͱ͸େମ࣮ݱͰ
    ͖ͦ͏ɻ
    • ·ͣ͸ࣗ෼ͷؔΘΔϦχϡʔΞϧϓϩδΣΫτ͔Β΍͍͖͍ͬͯͨ
    • Πϯϑϥ໘Ͱ͸ී௨ͷAPPαʔόʔͱ͸ҧ͏ߟྀϙΠϯτ΋͋Δ…

    View full-size slide

  22. ΍͍͖ͬͯ·͠ΐ͏
    Yatteiki

    View full-size slide

  23. Reference
    • https://medium.com/@shiladitya16/building-
    micro-services-using-grpc-on-
    ruby-41acb755ee06
    • https://speakerdeck.com/kazegusuri/grpc-and-
    rest-with-grpc-in-practice
    • https://speakerdeck.com/kazegusuri/go-
    conference-2016-spring
    • http://blog.soushi.me/entry/2017/10/15/100616

    View full-size slide