Microservice #3: Configuring Discovery Service with Zookeeper

Reviewing previous article, the first step to configure is creating the registry/ discovery service with Zookeeper. This is how I did it,

  • Generate a new Spring Boot Project from Visual Code. Run Ctrl+Shift+P to open Command Palette.
  • Type Spring Initialzr.
  • I chose Gradle as the library.
  • Give name and choose the Spring Boot version. I chose the stable version 2.1.3.
  • Pick necessary dependencies: Actuator, Web, Hystrix, Zookeeper, Sleuth, Zipkin, Swagger, Lombok. I mentioned this dependencies in my previous articles.
  • Voila new project generated!

 

Modify build.gradle

 

// zookeeper
  
implementation ('org.springframework.cloud:spring-cloud-starter-zookeeper-discovery') {
  exclude group: 'org.apache.zookeeper', module: 'zookeeper'
}

implementation('org.apache.zookeeper:zookeeper:3.4.13') {
  	exclude group: 'org.slf4j', module: 'slf4j-log4j12'
}

Add annotation in main application class

path: ~\src\main\java\com\[packagename]\[projectname]\TroposApplication.java

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class TroposApplication {

  public static void main(String[] args) {
    SpringApplication.run(TroposApplication.class, args);
  }

}

Configure application.properties

spring.application.name=service01
server.port=8100

# Zookeeper Setting
spring.cloud.zookeeper.discovery.enabled=true
logging.level.org.apache.zookeeper.ClientCnxn=WARN

Create Services Class

I create a simple hello world program

package com.ninan.tropos.service;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HelloTropos{
    protected String run(){
        log.info("Hello Tropos Discovery Service");
        return "Hello";
    }
}

 

Create Controller Class

package com.ninan.tropos.web;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class TroposController{

    @GetMapping("/hello")
    public String helloTropos(){
        log.info("calling hello world success");
        return "Hello Tropos";
    }

}

You may run the service and access the http://localhost:8100 on your browser.

If you still find some unidentified error, for example log.info, or anything related to logging function. Make sure you install the Lombok extension on your Visual Code. The environment needs that.

My note:

While creating this file, I found a lot of stuff. Just in case I need for troubleshooting in the future, I want to put my finding here.

Before I wrote the Zookeeper Setting in application.properties and enabling circuit breaker in main java class, actually the web application can be run and accessed. However, it can only be accessed once. After that the server or service is dropped automatically. I am not sure why. I checked the log. A lot of error occurred regarding the zookeeper cannot connect to certain port and so on. So I thought I forgot to configure something. Then I added the zookeeper setting in application.properties and enabling the circuit breaker annotation. Yeah, that solved my problem. the error disappeared and I can load the web several times. But sometimes, the service suddenly down. I might be forget something..

I also found something new while reading the log. Port 2181… what is that? I never read anything about Zookeeper. So I didn’t know that zookeeper has default port for client, which is 2181. There is also another port used, such as 2888 for follower port, and 3888 for election port. Please read the reference below for details.

References:

Zookeeper Book – https://www.oreilly.com/library/view/zookeeper/9781449361297/

Zookeeper Fundamental – https://developer.ibm.com/tutorials/bd-zookeeper/

Source Code:

https://gitlab.com/ninankara/atmosphere

the discovery service is Tropos project.

%d bloggers like this: