Creating your Micro API
So far, we have explicitly called a backend service by name and a method to access it. In this recipe, we will learn how we can access the services using Go Micro API, which implements an API gateway pattern to provide a single entry point to the microservices. The advantage of using Go Micro API is that it serves over HTTP and dynamically routes to the appropriate backend service using HTTP handlers.
Getting ready…
Start consul agent
, micro API
, first-greeting-service.go
, and second-greeting-service.go
in separate terminals by executing the following commands:
$ consul agent -dev $ micro api $ go run first-greeting-service.go $ go run second-greeting-service.go
How to do it…
- Create
greeting-api.go
inside theapi
directory by executing the command$ mkdir api && cd api && touch greeting-api.go
. - Copy the following content to
greeting-api.go
:
package main import ( "context" "encoding/json" "log" "strings" hello "../proto" "github.com/micro/go-micro...