MicroServices Example with Spring Boot and ZooKeeper

To look at my GitHub example directly, follow this link.

This is an example to get introduced to micro-services registering and interacting with each other via a discovery service in the cloud.

To get introduced to micro-services themselves, read Martin Fowler's excellent introduction and resource guide here and here.

The Ingredients:

  • Apache ZooKeeper - Tool for Service Discovery - Zookeeper Wiki 
  • Spring Boot - Framework/Library to quickly stand up sample Micro-services - Boot Documentation
  • Spring Cloud ZooKeeper - Integrating Spring Boot with Apache ZooKeeper - Documentation  

The Recipe:

Install ZooKeeper for a standalone operation. Download a stable version. De-compress it. Then create and set configuration as shown below. To read more, follow Apache's documentation here.

/conf/zoo.cfg

tickTime=2000
#Set dataDir the same level of access you 
#need for running zookeeper. Otherwise, the zookeeper 
#executable will complain about insufficient access
dataDir=/var/zookeeper
#This will set zookeeper to run on localhost:2181
clientPort=2181

Launch ZooKeeper. Execute  zkServer.sh (or zkServer.cmd on Windows). Available at /your-zookeeper-directory/bin/zkServer.sh

Now, we will use spring-boot to stand up a couple of trivial micro-services on two different ports, 8081 and 8083.​

  • First MicroService - Diner: A service that accepts a day ("1", "2", etc.) and returns a status as "open" or "closed".
  • Second MicroService - Foody: A service that calls the diner to check if it is open.

I have pushed the code-example to github, so we can focus on how that works here.

Github link: spring-boot-zookeeper-example
  1. Download the files from the above github repository.
  2. Install (mvn clean install) Diner. 
  3. Navigate (cd) to the target, then run the built jar (e.g. java -jar diner-0.0.1-SNAPSHOT.jar). This step registers the diner microservice with zookeeper under name "diner" (see /src/main/resources/bootstrap.yml), and launches it on localhost's port 8081.
  4. Use a RestClient to test the service at http://localhost:8081/diner/isopenThe service expects a "Content-Type" header of "application/json" and request data like {"day":"1"}.
  5. Install (mvn clean install) foody. 
  6. Navigate (cd) to the target, then run the built jar (e.g. java -jar foody-0.0.1-SNAPSHOT.jar). This step fetches the "diner" micro service from zookeeper, injects it as a client (using FeignClient) in "foody", registers this service with zookeeper under name "foody" (see /src/main/resources/bootstrap.yml), and launches the service on localhost port 8083.
  7. Use a RestClient to test the service at http://localhost:8083/foody/mydiner. The service expects a "Content-Type" header of "application/json" and request data like {"day":"1"}.