Apache ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. It is used as a cluster coordination service for demanding systems such as Hadoop, Kafka, Storm, Akka, Neo4j, Talend ESB and so on.

ZooKeeper is a very versatile tool and a perfect partner for a microservice architecture. These are its most common uses:

  • Centralized configuration
  • Service discovery
  • Resource allocation
  • Failure detection
  • Leader election for failover scenarios
  • High priority queues

I have started using ZooKeeper as a configuration repository for a spring-boot application. I have found it extremelly simple thanks to Apache Curator and Spring Cloud Config.

Spring Cloud ZooKeeper Config has a specific Spring PropertySource that loads transparently the configuration properties from ZooKeeper. It enables me to switch the source of my properties without affecting my application.

The source code of this example is here

SampleSimpleApplication.java

@Configuration
@ComponentScan
@EnableScheduling
@EnableAutoConfiguration
public class SampleSimpleApplication {
    // Simple example shows how a command line spring application can execute an
    // injected bean service. Also demonstrates how you can use @Value to inject
    @Autowired
    private HelloWorldService helloWorldService;
    @Scheduled(fixedRate=10000)
    public void run() {
        System.out.println(this.helloWorldService.getHelloMessage());
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleSimpleApplication.class, args);
    }
}

HelloWorldService.java

@Component
public class HelloWorldService {
	@Value("${name}")
	private String name;
	public String getHelloMessage() {
		return "Hello " + this.name;
	}
}

application.properties

spring.application.name=myapplication
server.port=8088
# name=James

Loading properties in ZooKeeper

zkCli.sh
create /config "
create /config/myapplication "
create /config/myapplication/name Mark
quit

All in action