ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect…

Follow publication

Nameko is ready to play with all the kids in the yard.

gRPC APIs with Nameko

Jakub Borys
ITNEXT
Published in
3 min readFeb 28, 2019

gRPC is a great tool for fast, cross platform RPC communication. With a support for many languages, it brings down barriers between disparate service implementations.

Nameko is a robust framework for building Python Microservices. Nameko-gRPC extension library has just landed and in this article I’d like to show you how easy you can utilise power of gRPC with Nameko.

TLDR: Dive straight into implementation of Nameko gRPC Examples here: https://github.com/nameko/nameko-examples-grpc

Traditionally to communicate with Nameko services you had an option of either using HTTP REST APIs or implementing Nameko’s own RPC over AMQP protocol. Nameko’s RPC, while fairly straightforward and language independent, is not a standard and has to be implemented for each language/platform independently.

New gRPC extensions open up many new opportunities for Nameko and some of them are:

  • Straight forward approach to exposing and consuming APIs from other services in your stack.
  • Playing nicely with Istio type Service Mesh deployments.
  • Ability to orchestrate Canary deployments for your services.

Using Nameko-gRPC

As with other Nameko extensions, Nameko-gRPC library gives us Entrypoint and a Client Proxy implementations to work with.

Let’s start with defining our Protobuf service definition:

To expose gRPC Entrypoint for the definition above you simply add @grpc decorator to your method implementation:

Your method name corresponds to the name of the service method as described in protobuf definition in our case get_product. It accepts two parameters:

  • request is an instance of gRPC request message type in our case GetProduct
  • context gives you access to both gRPC and Nameko request metadata

To consume gRPC Service you use GrpcProxy:

Cross Platform Examples

For end-to-end illustration of these concepts you can take a look at example repository https://github.com/nameko/nameko-examples-grpc which demonstrates cross platform integration between NodeJS Gateway service exposing GraphQL API and Nameko Services exposing gRPC APIs.

High level architecture of the examples:

NodeJS Service consuming Nameko gRPC API

Main components are:

  • Gateway: NodeJS Service with GraphQL API.
  • Orders: Python Nameko Service with gRPC API and PostgreSQL as its data store. This service will also publish order_created event.
  • Products: Python Nameko Service with gRPC API and Redis as its data store. This service will also consume order_created event.

Gateway NodeJS service exposes simple GraphQL schema for our API:

Gateway’s GraphQL Schema

To play with the examples follow steps as described in the repository’s README and start GraphQL Playground. There you can run queries and mutations available to us.

GraphQL Playground

GraphQL Schema Resolvers in our example use NodeJS gRPC clients which are configured with the same Products and Orders Protobuf service definitions as used by Nameko Services themselves.

Conclusion

Bringing Nameko to your existing Microservices platform has never been easier. If you are already using gRPC for service-to-service communication Nameko will fit right in. With Nameko your developers can concentrate on implementing application logic while tapping to a rich Python ecosystem.

Head over to examples:
https://github.com/nameko/nameko-examples-grpc

Checkout Nameko-gRPC implementation:
https://github.com/nameko/nameko-grpc

Visit Nameko:
Website: https://www.nameko.io
Forum: https://discourse.nameko.io
Twitter: https://twitter.com/nameko_python

Published in ITNEXT

ITNEXT is a platform for IT developers & software engineers to share knowledge, connect, collaborate, learn and experience next-gen technologies.

Written by Jakub Borys

Software Engineer. Product Developer. Mentor. Expat. Windsurfer Wannabe. http://jakubborys.com

Responses (1)

Write a response