Logo

Navigation
  • Home
  • Android
  • Web
  • Raspberry Pi
  • PHP
  • Go

Golang Go-Micro with Docker Compose

By Rajinder Deol | on August 3, 2017 | 7 Comments
Education Go Technology Web

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:[email protected]: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.

micro-web landing page

Click on Registry in the top bar or open http://localhost/registry. This page will show you the list of services running.

micro-web registry

Click on Query in the top bar or open http://localhost/query. This page will be used to query our service.

micro-web query

From the service dropdown select our service “service.helloworld”

micro-web select service

From the method dropdown select our service method “Greeter.Hello”

micro-web query select service method

In the request section add the JSON request string. The hello-world service accepts “name” as string.


{"name":"Raj"}

micro-web query request body

Press execute button and you will see the response in response section.

micro-web query response body

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.

Related

Share this story:
  • tweet

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere

    September 22, 2021 - 2 Comments
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years

    September 13, 2021 - 1 Comment
  • NodeJS Consumer for AWS SQS using Docker

    November 10, 2018 - 3 Comments

Author Description

7 Responses to “Golang Go-Micro with Docker Compose”

  1. October 16, 2017

    devops online training in hyderabad Reply

    Nice Article. In short description good explanation about the DevOps. Thanks For sharing the informative news.

  2. November 2, 2017

    Pritam Jain Reply

    Perfect, who wants to work with limited knowledge of docker . docker compose..

  3. November 23, 2017

    Yannick Reply

    Great 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 Deol Reply

      Hey 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.

  4. April 12, 2019

    anirudh Reply

    i 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.

  5. May 4, 2020

    Ravinder Reply

    Nice 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

    [email protected] 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.

  6. December 21, 2020

    CS Reply

    Great post. Really enjoyed reading that. Thanks very much!

Leave a Reply Cancel Reply

Your email address will not be published. Required fields are marked *


*
*

Recent Posts

  • RaspberryPi with PiHole to block ads everywhere
  • Reviving RaspberryPi 1 Model B in 2021 After 7 years
  • NodeJS Consumer for AWS SQS using Docker
  • Laravel development using docker-compose with nginx and Mysql
  • Writing Your First Service in Golang Go-Micro

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 9 other subscribers

Categories

  • Android (7)
  • Education (9)
  • Go (2)
  • PHP (5)
  • Raspberry Pi (4)
  • Technology (17)
  • Web (8)

Author details:

Raj Deol

Raj Deol

Traveller, Foodie and electronics enthusiast.

View Full Profile →

© 2016. All Rights Reserved.