Utility for reading/storing properties (name/value pairs) from/to ZooKeeper
A trending architectural principle is the one of micro services, i.e. small self-contained application performing a very limited set of tasks. In an essence stand-alone processes performing some task. A typical problem in a micro services architecture is to configure each individual service. Not only are the services potentially distributed over a number of hosts, there's most likley going to be multiple instances of each service.
The common pattern is to use a centralized database for storage, of which ZooKeeper is very popular.
Properties tend to be something we use property files for and let the application read during startup.
This approach becomes awfully cumbersome when faced with a multitude of instances on different hosts.
This library provides a simple to use to read properties for an application from ZooKeeper.
The data model is simple and straightforward.
There is support for multiple property sets, e.g. each application/service may have its own specific set of properties.
Each property set is stored in its own path under which the name/value of each property is stored.
The example below illustrates two separate property sets global and service-a
[root-path]/properties
/global
/db.host[localhost]
/db.port[6969]
/service-a
/max.threads[100]
It all starts by creating a PropertiesStorageFactory which is in an essence is the builder for creating instance of PropertiesStorage instances.
The factory itself is based on the builder pattern allowing you to choose what properties to set.
PropertiesStorageFactory factory = PropertiesStorageFactory.apply("localhost:6181")
.withRootPath("/etc/data");
Try<PropertiesStorage> propertiesStorage = factory.create();
Now assuming we got a Successful response containing a PropertiesStorage instance we can use it to:
PropertiesStorage propertiesStorage = ...
PropertySet ps = PropertySet.apply("example-app");
ps.set("db.host", "some-host");
ps.set("user.name", "Peter");
Try<Unit> result = propertiesStorage.store(ps);
PropertiesStorage propertiesStorage = ...
Try<List<String>> sets = propertiesStorage.propertySets();
PropertiesStorage propertiesStorage = ...
Try<Option<PropertySet>> properties = propertiesStorage.get("example-app");
PropertiesStorage propertiesStorage = ...
Try<Unit> result = propertiesStorage.delete("example-app");
To further ease the management of the properties in ZooKeeper there is a companion project RESTful ZooKeeper Properties which provides a RESTful interface to manage the data.
Allowing for non-programmatic access using tools such as wget and curl.
This project builds heavily on both Lambda operations as well as a more functional programming paradigm.
Some of the types such as Option,Try and Future may seem a bit confusing at first.
Refer to the project java-scala-util for details and examples on how to work with features such as Option/Try/Future
There are a few companion projects that may be of use.
- RESTful ZooKeeper Properties
Provides a RESTful interface to this project. I.e. the possibility to manage the data model over HTTP/REST. - ZooKeeper Unit
Utility for managing a ZooKeeper server during JUnit testing.
Copyright 2016 Peter Nerg.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.