Go (Golang) is an opensource programming language developed by Google. It is being used in lot of scenarios and is best fit for systems which need to handle a lot of concurrent connections.
Microservices are core of service-oriented architecture pattern and you can write individual microservices which can perform individual tasks across a distributed system. Go has gained a lot of popularity for writing microservices, however writing microservices for distributed system comes with its own challenges.
Microservices at enterprises level require different components to function. It requires a registry to register available microservices so that different systems can discover and access them.
It also requires a broker so that microservices can communicate with other systems deployed on distributed infrastructure.
At enterprise level, services use remote procedure call (RPC) to use other services.
Go-Micro
Go-Micro is a pluggable RPC framework written in GO for writing microservices. It is a framework to write microservices which are ready for distributed systems from start. Go-Micro comes with all the components which are required for writing distributed microservices. It has interfaces to add service discovery and message brokers. By default Consul is used for service discovery and RabbitMQ as message broker. It uses protobuf for inter service communication. To read more about Go-Micro framework follow their github repo here
Go Micro with Docker Compose
I have been using Docker for more than 3 years now and I use it for all my developments including wordpress plugin development.
When I started working on Go-Micro I needed a Docker setup so that I can just start a development environment with all the features of Go-Micro including Consul for service discovery and RabbitMq as message broker. Along with that I use micro-web, a web based dashboard and reverse proxy for my services, micro-api which is an API gateway to route (translate) http requests to my appropriate microservices and Postgres Database for data storage.
micro-web is optional. I use it during development to quickly query my microservices from a easy to use web interface.
Following is my Docker-compose.yml to build docker containers with Consul, RabbitMQ, micro-web and a small service go-micro-hello-world written using Go-Micro. I have created a docker image for the service go-micro-hello-world and pushed it into my DockerHub account.
We will only focus on building up Go-Micro environment in this post. There will be a separate post on how to develop your first microservice in Go-Micro.
docker-compose.yml
###################################
# Cousul will be used to register service which other services and discover
consul:
image: consul:latest
command: consul agent -dev -log-level=warn -ui -client=0.0.0.0
hostname: consul
ports:
- "8500:8500"
##################################
# Message broker to interact with other systems
rabbit:
image: rabbitmq:management
hostname: rabbit
ports:
- "5672:5672"
- "15672:15672"
###################################
# Micro web to view and query service from web dashboard
microweb:
image: microhq/micro
command: web --address=0.0.0.0:8080
ports:
- "80:8080"
environment:
- MICRO_REGISTRY=consul
- MICRO_REGISTRY_ADDRESS=consul
- MICRO_API_NAMESPACE=gomicro.api
links:
- consul
- service.helloworld
###################################
# Our basic Helloworld service
service.helloworld:
image: rajdeol/go-micro-hello-world
ports:
- "3000:3000"
- "8081:8080"
environment:
- MICRO_REGISTRY=consul
- MICRO_REGISTRY_ADDRESS=consul
- MICRO_BROKER=rabbitmq
- MICRO_BROKER_ADDRESS=amqp://guest:guest@rabbit:5672
links:
- consul
- rabbit
Go-Micro provides environment variables which we can use to provide links to required components like MICRO_REGISTRY, MICRO_REGISTRY_ADDRESS, MICRO_BROKER, MICRO_BROKER_ADDRESS etc.
Now we will build the containers. Run the following commands :
docker-compose pull
The above command will download all the docker images we specified in the docker-compose file
docker-compose up
The above command will bring up our containers.Now open the browser and open http://localhost/ you will see the micro-web UI. We will use this to test our service.
Click on Registry in the top bar or open http://localhost/registry. This page will show you the list of services running.
Click on Query in the top bar or open http://localhost/query. This page will be used to query our service.
From the service dropdown select our service “service.helloworld”
From the method dropdown select our service method “Greeter.Hello”
In the request section add the JSON request string. The hello-world service accepts “name” as string.
{"name":"Raj"}
Press execute button and you will see the response in response section.
This was a very quick introduction to get Go-Micro up and running. You can view the code of this post at my github docker-compose-go-micro.
7 Responses to “Golang Go-Micro with Docker Compose”
October 16, 2017
devops online training in hyderabadNice Article. In short description good explanation about the DevOps. Thanks For sharing the informative news.
November 2, 2017
Pritam JainPerfect, who wants to work with limited knowledge of docker . docker compose..
November 23, 2017
YannickGreat article! I was wondering if you tried any approach to auto rebuild your go micro containers to improve development iteration time? If so, could you explain how you did this? Thanks!
November 23, 2017
Rajinder DeolHey Yannick,
Thanks for the kind words. I use Gin for auto rebuilding the go code when I edit any source file. I add and build Gin into my development docker container. You can read my post Writing Your First Service in Golang Go-Micro for complete details on how to include it and run it along with your go project.
April 12, 2019
anirudhi just go through your article it’s very interesting time just pass away by reading your article looking for more updates. Thank you for sharing.
May 4, 2020
RavinderNice Article on go micro with Docker, I get the below error though..
$ docker-compose pull
Pulling consul … done
Pulling rabbit … done
Pulling service.helloworld … done
Pulling microweb … done
AzureAD+RavinderSudhini@DESKTOP-6PDCNOD MINGW32 /c/Ravi/gomicro/docker
$ docker-compose up
Creating docker_rabbit_1 … done
Creating docker_consul_1 … done
Creating docker_service.helloworld_1 … done
Creating docker_microweb_1 … error
ERROR: for docker_microweb_1 Cannot start service microweb: b’Cannot link to a non running container: /docker_service.helloworld_1 AS /docker_microweb_1/docker_service.helloworld_1′
ERROR: for microweb Cannot start service microweb: b’Cannot link to a non running container: /docker_service.helloworld_1 AS /docker_microweb_1/docker_service.helloworld_1′
Encountered errors while bringing up the project.
December 21, 2020
CSGreat post. Really enjoyed reading that. Thanks very much!