Building Mancala Game in Microservices Using Spring Boot (Part 1: Solution Architecture)

DZone 's Guide to

Building Mancala Game in Microservices Using Spring Boot (Part 1: Solution Architecture)

Sound fun?

· Microservices Zone ·
Free Resource


Make a Mancala game like this! 

Nowadays with the popularity of Microservices, when we talk about scalable application development, we inevitably think of composing the application into highly decoupled microservices which can be scaled up independently according to the customers' needs and yet can be managed through various available industry-standard tools such as Docker-compose, Kubernetes, Istio, Linkerd, etc.

You may also like: Microservices With Spring Boot, Part 1 — Getting Started

If you are familiar with Microservices terms and willing to dive directly into implementation details, you can skip this part and read Building Mancala Game in Microservices using Spring Boot (Part 2)

Below, is a typical example of implementing an online game called mancala which I have developed using this Java, Spring Boot and Spring Cloud technologies based on the Microservices approach.

You can find the complete source for this article hosted in my GitHub account.

The implementation consists of two microservices designed for this game:

  1. 'mancala-api' microservice: A TDD based implementation of Mancala Game API with two endpoints.

    • 'CreateGame' Endpoint: To create a new Game.

    • 'SowGame' Endpoint: To sow specific pit index of the Game.

  2. 'mancala-web' microservice: provides a simple User interface to consume Manacala API service.

Technology Stack

Technologies used for this implementation are:

  • 'Spring Cloud', used for building Microservices applications in Java.

  • 'Spring MVC', for creating RESTful API.

  • 'Vaadin', A modern UI components for building web applications in Java.

  • 'MongoDB', NoSQL database for persisting the Game information.

  • 'Redis', Key-Value in-memory data structure for caching the Game instances.

  • 'Cassandra', distributed NoSQL database used as Zipkin storage.

  • 'Docker', for containerization of services.

  • 'Docker-Compose', to link the containers.

  • 'Consul', service lookup/discovery.

  • 'Ribbon', client load balancing for service invocation.

  • 'Apache Httpd', request routing and HTTP load balancer.

  • 'Swagger', Swagger-UI, for API documentation.

  • 'Micrometer', Spring boot default metrics export engine.

  • 'Actuator', Monitoring the app, gathering metrics, understanding traffic.

  • 'Spring Cloud Sleuth', distributed tracing solution for Spring Cloud.

  • 'Zipkin', distributed tracing system for tracing service calls and visualizing the traces.

  • 'WireMock', inter-service communication testing.

  • 'Spring Cloud Contract', Consumer contract-based Testing for Microservices.

Here is the overall architecture of the solution:

Image title

Below is a walk-through the solution and discussing technologies have been used and how to configure it further for your own interest.

1— Consul Service Registry and Discovery

As it's depicted in the above picture, our microservices are registered within Consul service registry and therefore, we will be using service discovery API provided by Spring Cloud in order to discover instances of those microservices from Consul registry by adding below dependency to the client's pom.xml

and using @EnableDiscoveryClient at our client's Spring Boot Application:

public class MancalaGameWebApplication {

2 — Apache HTTP Load Balancer

Apache HTTP is used to forward HTTP requests to our microservices whose ports are not exposed! Apache HTTP is configured as a reverse proxy for this purpose and as a load balancer i.e. if you start multiple instances of a microservices by below command, Apache will recognize the new instances:

[.\mancal-game\docker] docker-compose scale mancala-api=3

3 — Ribbon Client-Side Load Balancer

Once we have requirements for scaling up our services in a microservice environment using many available tools such as Docker-compose, Kubernetes and etc, there is a need to know how to balance the load between these microservices and make sure that we can configure these invocations based on our requirements. In this application, I have used Netflix Ribbon client-side load balancing to provide solid experience for clients while communicating with other microservices and still maintaining the service-state if we are dealing with Stateful-services.

4 — Zipkin Distributed Tracing With Spring Sleuth

One of the strict requirements within microservices architecture is to have an end-to-end visualization on complex inter-microservice communication scenarios where the requests initiated by the client application possibly span more than one microservices and lead to spawning new messages alongside. As our microservice application evolves, more services added to the system and therefore we need a tool to prob any performance bottlenecks might occur within a specific business transaction or between two services and get runtime information about those events.

Zipkin is an application for visualizing traces between and within services. Spring Cloud Sleuth can be used by microservices developed using Spring Cloud, to simplify both the creation of trace events and sending them to the Zipkin server. Spring Cloud Sleuth intercepts incoming requests and either pick up the trace id from the incoming message or creates a new trace id if none was specified. For outgoing requests, it fills in the current trace id in the outgoing message.

You can enable Zipkin and Spring Sleuth by adding below dependency into your pom.xml file:

<!-- Zipkin -->

In this application, Zipkin dashboard shows trace calls between mancala-web and mancala-API microservices which can be accessed here: http://localhost:9411.

Image title

5 — Prometheus

Prometheus is an open-source monitoring system with a dimensional data model, flexible query language, efficient time-series database, and modern alerting approach. In this application, microservices are configured to send metrics to Prometheus server for monitoring which can be seen at http://localhost:9090.

Image title

Micrometer is default Spring boot metrics export engine which can be enabled and used to send metrics to Prometheus server by adding below dependencies to your pom.xml file:

<!-- Micormeter core   -->

<!-- Micrometer Prometheus registry  -->

6 — Grafana Dashboard

Grafana is the leading open-source project for visualizing collected metrics provided by the Prometheus server. You can access to Grafana dashboard at http://localhost:3000.

Image title

7 — ELK-Stack

We often need to have access to application logs at runtime for debugging purposes or analyzing the application behavior over certain conditions. The best open source tool for ingesting data from different sources, transforming data to custom formats and finally providing searching functionality on top of them is provided by ELK-Stack which consists of Elasticsearch, Logstash, and Kibana.

In this application, we have configured Filebeat to send logs generated by our Microservices to Logstash which in turn are transferred to the Elastic search server.

You can access to Kibana dashboard at http://localhost:5601.

After you configure the Kibana dashboard, you can search on Logs for any data collected by our application, e.g. 'PlayerTurn' to see how 'playerTurn' parameter has changed over several mancala-API call to sow() Endpoint.

Image title

8 — Build and Run Using Docker-Compose

You can find the complete source for this article hosted on my GitHub repository. A detail instructions are provided here which show you how to build and run the game with three different options. Once you run the server you can access the Game landing page at this address: http://localhost

Before building the game, there is an environment variable called 'MANCALA_PIT_STONES' within the docker-compose.yml files for 'mancala-api' service which allows you to customize the Mancala game based on the number of stones. The default value for this variable is 6.

9 — Implementation Details

I have provided detail implementation for the above solution in three separate articles:

Your valuable comments and feedbacks are highly appreciated!

Further Reading

Quick Guide to Microservices With Spring Boot 2.0, Eureka, and Spring Cloud

Microservices Architecture With Spring Boot and Spring Cloud

Creating Microservices With Spring and Eureka

consul ,docker-compose ,microservices ,mongodb ,ribbon ,spring boot 2.2 ,spring cloud contract ,vaadin ,wiremock ,zipkin

Published at DZone with permission of Esfandiyar Talebi . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}