Added Javadocs for Zookeeper Config

This commit is contained in:
Marcin Grzejszczak
2016-03-25 11:03:38 +01:00
parent 0092162a68
commit 502ed3719f
7 changed files with 64 additions and 13 deletions

View File

@@ -20,7 +20,11 @@ import org.apache.curator.framework.CuratorFramework;
import org.springframework.core.env.EnumerablePropertySource;
/**
* A {@link EnumerablePropertySource} that has a notion of a context which is
* the root folder in Zookeeper.
*
* @author Spencer Gibb
* @since 1.0.0
*/
public abstract class AbstractZookeeperPropertySource extends EnumerablePropertySource<CuratorFramework> {
@@ -28,9 +32,7 @@ public abstract class AbstractZookeeperPropertySource extends EnumerableProperty
public AbstractZookeeperPropertySource(String context, CuratorFramework source) {
super(context, source);
this.context = context;
if (!this.context.startsWith("/")) {
this.context = "/" + this.context;
}

View File

@@ -24,6 +24,7 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
@@ -38,12 +39,15 @@ import static org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type.NOD
import static org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type.NODE_UPDATED;
/**
* Class that registers a {@link TreeCache} for each context.
* It publishes events upon element change in Zookeeper.
*
* @author Spencer Gibb
* @since 1.0.0
*/
public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationEventPublisherAware{
private static final Log log = org.apache.commons.logging.LogFactory
.getLog(ConfigWatcher.class);
private static final Log log = LogFactory.getLog(ConfigWatcher.class);
private AtomicBoolean running = new AtomicBoolean(false);
private List<String> contexts;
@@ -69,7 +73,6 @@ public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationE
if (!context.startsWith("/")) {
context = "/" + context;
}
try {
TreeCache cache = TreeCache.newBuilder(this.source, context).build();
cache.getListenable().addListener(this);
@@ -105,12 +108,12 @@ public class ConfigWatcher implements Closeable, TreeCacheListener, ApplicationE
}
public String getEventDesc(TreeCacheEvent event) {
StringBuffer out = new StringBuffer();
out.append("type="+event.getType());
out.append(", path="+event.getData().getPath());
StringBuilder out = new StringBuilder();
out.append("type=").append(event.getType());
out.append(", path=").append(event.getData().getPath());
byte[] data = event.getData().getData();
if (data != null && data.length > 0) {
out.append(", data="+new String(data, Charset.forName("UTF-8")));
out.append(", data=").append(new String(data, Charset.forName("UTF-8")));
}
return out.toString();
}

View File

@@ -24,7 +24,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration Auto-configuration}
* that registers a Zookeeper configuration watcher.
*
* @author Spencer Gibb
* @since 1.0.0
*/
@Configuration
public class ZookeeperConfigAutoConfiguration {

View File

@@ -25,12 +25,16 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* Bootstrap Configuration for Zookeeper Configuration
*
* @author Spencer Gibb
* @since 1.0.0
*/
@Configuration
@Import(ZookeeperAutoConfiguration.class)
@EnableConfigurationProperties
public class ZookeeperConfigBootstrapConfiguration {
@Bean
@ConditionalOnMissingBean
public ZookeeperPropertySourceLocator zookeeperPropertySourceLocator(

View File

@@ -20,17 +20,31 @@ import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties related to keeping configuration in Zookeeper.
*
* @author Spencer Gibb
* @since 1.0.0
*
* @see ZookeeperPropertySourceLocator
*/
@ConfigurationProperties("spring.cloud.zookeeper.config")
public class ZookeeperConfigProperties {
private boolean enabled = true;
/**
* Root folder where the configuration for Zookeeper is kept
*/
private String root = "config";
/**
* The name of the default context
*/
@NotEmpty
private String defaultContext = "application";
/**
* Separator for profile appended to the application name
*/
@NotEmpty
private String profileSeparator = ",";

View File

@@ -23,23 +23,26 @@ import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.KeeperException;
import org.springframework.util.ReflectionUtils;
/**
* {@link org.springframework.core.env.PropertySource} that stores properties
* from Zookeeper inside a map. Properties are loaded upon class initialization.
*
* @author Spencer Gibb
* @since 1.0.0
*/
public class ZookeeperPropertySource extends AbstractZookeeperPropertySource {
private static final Log log = org.apache.commons.logging.LogFactory
.getLog(ZookeeperPropertySource.class);
private static final Log log = LogFactory.getLog(ZookeeperPropertySource.class);
private Map<String, String> properties = new LinkedHashMap<>();
public ZookeeperPropertySource(String context, CuratorFramework source) {
super(context, source);
findProperties(this.getContext());
}
@@ -87,7 +90,6 @@ public class ZookeeperPropertySource extends AbstractZookeeperPropertySource {
if (children == null || children.isEmpty()) {
return;
}
for (String child : children) {
String childPath = path + "/" + child;
byte[] bytes = getPropertyBytes(childPath);

View File

@@ -30,7 +30,29 @@ import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
/**
* Zookeeper provides a <a href="http://zookeeper.apache.org/doc/current/zookeeperOver.html#sc_dataModelNameSpace">hierarchical namespace</a> that allows
* clients to store arbitrary data, such as configuration data. Spring Cloud Zookeeper Config is an alternative to the
* <a href="https://github.com/spring-cloud/spring-cloud-config">Config Server and Client</a>. Configuration is loaded into the Spring Environment during
* the special "bootstrap" phase. Configuration is stored in the {@code /config} namespace by default. Multiple
* {@code PropertySource} instances are created based on the application's name and the active profiles that mimicks the Spring Cloud Config
* order of resolving properties. For example, an application with the name "testApp" and with the "dev" profile will have the following property sources
* created:
*
* <pre>{@code
* config/testApp,dev
* config/testApp
* config/application,dev
* config/application
* }</pre>
*
* </p>
* The most specific property source is at the top, with the least specific at the
* bottom. Properties is the {@code config/application} namespace are applicable to all applications
* using zookeeper for configuration. Properties in the {@code config/testApp} namespace are only available
* to the instances of the service named "testApp".
*
* @author Spencer Gibb
* @since 1.0.0
*/
public class ZookeeperPropertySourceLocator implements PropertySourceLocator {