Building APIs with GRPC, PHP, and Golang

045d790f605ea8d482380c5075d802d5?s=47 Cecy C.
June 02, 2018

Building APIs with GRPC, PHP, and Golang

How to use GRPC, Protobufs, PHP (and a bit of Golang) to build APIs and microservices.

045d790f605ea8d482380c5075d802d5?s=128

Cecy C.

June 02, 2018
Tweet

Transcript

  1. BUILDING APIs WITH GRPC & PHP (and golang) @cecycorrea //

    software engineer // context.io
  2. (or gRPC for dummies)

  3. Hi, I’m Cecy Correa @cecycorrea Software Engineer, Context.IO

  4. gRPC

  5. gRPC Remote Procedure Calls

  6. oh ok

  7. what is RPC?

  8. Data exchange between 2 processes

  9. None
  10. so an API?

  11. RPC or REST?

  12. None
  13. The “RPC” part stands for “remote procedure call,” and it’s

    essentially the same as calling a function in JavaScript, PHP, Python and so on, taking a method name and arguments. Source: Smashing Magazine
  14. RPC == good for actions

  15. REST == CRUD & modeling your domain

  16. [REST] GET users/:id/photos/:photo_id

  17. [RPC] getUserPhotos(args...)

  18. Why gRPC?

  19. Why gRPC? • Use HTTP/2 • Language agnostic • Highly

    efficient / scalable • Handle large amounts of data
  20. HTTP2! • Binary: moar data! Moar efficient! • Multiplexed: multiple

    files at a time • Same connection!
  21. Source: CSS Tricks

  22. Who is using it? • Google • Netflix • Docker

    • Square • CoreOS • Twitch (Twirp)
  23. None
  24. gRPC + Protobuf

  25. GRPC PROTOBUF The protocol Define the service Messages that are

    exchanged back and forth
  26. Let’s get started!

  27. What you will need • Protocol Buffers to define your

    service • Protoc to generate client and server stubs based on your service definition • gRPC server to handle requests
  28. Install (Mac OS) • brew install grpc • brew install

    protobuf • brew install protoc • brew install golang go to grpc.io for other alternatives
  29. Boom! We’re done here!

  30. Step 1: Create a proto file

  31. Sample proto file

  32. None
  33. None
  34. Sample proto file

  35. Step 2: Create your server / client stubs

  36. protoc -I proto --php_out=lib --grpc_out=lib --plugin=protoc-gen-grpc=`which grpc_php_plugin` service.proto

  37. protoc -I proto --LANG_out=DIR --grpc_out=DIR --plugin=protoc-gen-grpc=`which grpc_php_plugin` WHERE_YOUR_PROTO_IS.proto

  38. project-dir ├── client | └── client.php ├── proto | └──

    service.proto ├── server | └── server.go|js|rb|py ... └── libs └── ruby └── python └── ...
  39. Step 3. Create a gRPC server

  40. None
  41. Sample proto file

  42. Step 4: Write some clients!

  43. None
  44. *Note: you will need to install some stuff to get

    started in PHP • composer install grpc as a dependency • pecl install grpc / protobuf • Full instructions on getting started: https://grpc.io/docs/quickstart/php.html
  45. None
  46. Let’s see it in action!

  47. Use case: Context.IO

  48. None
  49. Pain points • Legacy codebase (~10 years old) • Monolith

    codebase • Heavily coupled == hard to unit test • Scaling problems
  50. How gRPC helped • Replace small pieces of functionality with

    microservices • Easy to do by “resource”
  51. From REST to microservice GET /discovery Discovery service GET /user/:id/contacts

    Contacts service GET | POST /user/:id/webhooks Webhooks service GET | POST /webhooks GET /user/:id/messages Messages services GET /user/:id/folders Folder service
  52. Our PHP API became the frontend for a microservice ecosystem

  53. The brave new future: Completely generated client libraries & docs

  54. This solves... • Feature parity between REST API and client

    libraries / wrappers • Feature parity between API and docs • Lack of knowledge in the team around certain languages • Easy for other teams to consume your service
  55. ...and gRPC can also generate REST stubs!

  56. So you can gRPC -> REST -> gRPC

  57. grpc-gateway

  58. Generate stubs + gateway path/to/your_service.pb.gw.go

  59. Http proxy server

  60. None
  61. [CODE] github.com/cecyc/dad-joke-service

  62. Questions? Happy to talk in the hall! or ping @cecycorrea

    on Twitter