Go microservice: API gateways pattern

Trong Dinh Thai Hoang
3 min readNov 3, 2019

--

The purpose of this article helps you to understand how to develop basic microservices by using API gateways pattern.

1. Requirements:

It doesn’t require you must have experience in microservices before. But I highly recommend you should read to understand some basic concepts.

  • Microservice:

A microservice application is a collection of autonomous services, each of which does one thing well, that work together to perform more intricate operations. Instead of a single complex system, you build and manage a suite of relatively simple services that might interact in complex ways. These services collaborate with each other through technology-agnostic messaging protocols, either point-to-point or asynchronously.

gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.

  • How many microservices patterns do we have?

https://medium.com/@madhukaudantha/microservice-architecture-and-design-patterns-for-microservices-e0e5013fd58a

Redis is an in-memory data structure project implementing a distributed, in-memory key-value database with optional durability.

2. What we build:

  • This article will focus on building the authentication service which includes login/signup and user profile endpoints.
Postman

3. System architecture:

3.1. API gateway pattern:

3.2. Our system architecture:

4. Happy scenario:

In this architecture, a happy scenario would be:

  • user registers an account and we save the user information to the local database.
  • user logins and we return a Jwt token.
  • user get their profile by sending the Jwt token.

5. Our microservices:

  • Protobuf:

user.proto will have 2 methods: CreateUser and ReadUser which are to help to register/log in.

user.proto
  • Front controller:

This helps handle all requests, consume the message from microservice and return for frontend.

  • Entry cache service:

The basic concept of entry cache service is to get the user from Redis if any. Otherwise, we will get information from the local database.

user.go
  • Entry store service:

The purpose of this microservice is to connect to the local database.

user_dao.go

7. Conclusion:

This article explains how to use gRPC to transfer data between services and Gin framework as a front controller to consume the message and returns to the client. For viewing the complete code go to:

--

--

Trong Dinh Thai Hoang

I’m a peaceful person who wants to make friend with people around the world.