Removes lombok from config, adds logging to ConfigWatch

This commit is contained in:
Spencer Gibb
2018-06-05 13:24:39 -04:00
parent 22a593389c
commit 0e03c2613a
5 changed files with 219 additions and 43 deletions

View File

@@ -47,12 +47,6 @@
<artifactId>spring-cloud-context</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- Only needed at compile time -->
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>

View File

@@ -16,37 +16,37 @@
package org.springframework.cloud.consul.config;
import java.io.Closeable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.PostConstruct;
import io.micrometer.core.annotation.Timed;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import io.micrometer.core.annotation.Timed;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.core.style.ToStringCreator;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.io.Closeable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
/**
* @author Spencer Gibb
*/
@CommonsLog
public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
private static final Log log = LogFactory.getLog(ConfigWatch.class);
private final ConsulConfigProperties properties;
private final ConsulClient consul;
private LinkedHashMap<String, Long> consulIndexes;
@@ -56,7 +56,7 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
@Deprecated
public ConfigWatch(ConsulConfigProperties properties, List<String> contexts, ConsulClient consul) {
this(properties, consul, new LinkedHashMap<String, Long>());
this(properties, consul, new LinkedHashMap<>());
}
public ConfigWatch(ConsulConfigProperties properties, ConsulClient consul, LinkedHashMap<String, Long> initialIndexes) {
@@ -92,6 +92,8 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
currentIndex = -1L;
}
log.trace("watching consul for context '"+context+"' with index "+ currentIndex);
// use the consul ACL token if found
String aclToken = properties.getAclToken();
if (StringUtils.isEmpty(aclToken)) {
@@ -110,11 +112,18 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
if (newIndex != null && !newIndex.equals(currentIndex)) {
// don't publish the same index again, don't publish the first time (-1) so index can be primed
if (!this.consulIndexes.containsValue(newIndex) && !currentIndex.equals(-1L)) {
log.trace("Context "+context + " has new index " + newIndex);
RefreshEventData data = new RefreshEventData(context, currentIndex, newIndex);
this.publisher.publishEvent(new RefreshEvent(this, data, data.toString()));
} else if (log.isTraceEnabled()) {
log.trace("Event for index already published for context "+context);
}
this.consulIndexes.put(context, newIndex);
} else if (log.isTraceEnabled()) {
log.trace("Same index for context "+context);
}
} else if (log.isTraceEnabled()) {
log.trace("No value for context "+context);
}
} catch (Exception e) {
@@ -139,10 +148,51 @@ public class ConfigWatch implements Closeable, ApplicationEventPublisherAware {
this.running.compareAndSet(true, false);
}
@Data
static class RefreshEventData {
private final String context;
private final Long prevIndex;
private final Long newIndex;
public RefreshEventData(String context, Long prevIndex, Long newIndex) {
this.context = context;
this.prevIndex = prevIndex;
this.newIndex = newIndex;
}
public String getContext() {
return this.context;
}
public Long getPrevIndex() {
return this.prevIndex;
}
public Long getNewIndex() {
return this.newIndex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RefreshEventData that = (RefreshEventData) o;
return Objects.equals(context, that.context) &&
Objects.equals(prevIndex, that.prevIndex) &&
Objects.equals(newIndex, that.newIndex);
}
@Override
public int hashCode() {
return Objects.hash(context, prevIndex, newIndex);
}
@Override
public String toString() {
return new ToStringCreator(this)
.append("context", context)
.append("prevIndex", prevIndex)
.append("newIndex", newIndex)
.toString();
}
}
}

View File

@@ -16,21 +16,19 @@
package org.springframework.cloud.consul.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.style.ToStringCreator;
import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import lombok.Data;
/**
* @author Spencer Gibb
*/
@ConfigurationProperties("spring.cloud.consul.config")
@Data
@Validated
public class ConsulConfigProperties {
private boolean enabled = true;
@@ -68,6 +66,9 @@ public class ConsulConfigProperties {
*/
private String name;
public ConsulConfigProperties() {
}
@PostConstruct
public void init() {
if (this.format == Format.FILES) {
@@ -75,7 +76,102 @@ public class ConsulConfigProperties {
}
}
@Data
public boolean isEnabled() {
return this.enabled;
}
public String getPrefix() {
return this.prefix;
}
public @NotEmpty String getDefaultContext() {
return this.defaultContext;
}
public @NotEmpty String getProfileSeparator() {
return this.profileSeparator;
}
public @NotNull Format getFormat() {
return this.format;
}
public @NotEmpty String getDataKey() {
return this.dataKey;
}
public String getAclToken() {
return this.aclToken;
}
public Watch getWatch() {
return this.watch;
}
public boolean isFailFast() {
return this.failFast;
}
public String getName() {
return this.name;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public void setDefaultContext(@NotEmpty String defaultContext) {
this.defaultContext = defaultContext;
}
public void setProfileSeparator(@NotEmpty String profileSeparator) {
this.profileSeparator = profileSeparator;
}
public void setFormat(@NotNull Format format) {
this.format = format;
}
public void setDataKey(@NotEmpty String dataKey) {
this.dataKey = dataKey;
}
public void setAclToken(String aclToken) {
this.aclToken = aclToken;
}
public void setWatch(Watch watch) {
this.watch = watch;
}
public void setFailFast(boolean failFast) {
this.failFast = failFast;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return new ToStringCreator(this)
.append("enabled", enabled)
.append("prefix", prefix)
.append("defaultContext", defaultContext)
.append("profileSeparator", profileSeparator)
.append("format", format)
.append("dataKey", dataKey)
.append("aclToken", aclToken)
.append("watch", watch)
.append("failFast", failFast)
.append("name", name)
.toString();
}
public static class Watch {
/** The number of seconds to wait (or block) for watch query, defaults to 55.
* Needs to be less than default ConsulClient (defaults to 60). To increase ConsulClient
@@ -88,6 +184,42 @@ public class ConsulConfigProperties {
/** The value of the fixed delay for the watch in millis. Defaults to 1000. */
private int delay = 1000;
public Watch() {
}
public int getWaitTime() {
return this.waitTime;
}
public boolean isEnabled() {
return this.enabled;
}
public int getDelay() {
return this.delay;
}
public void setWaitTime(int waitTime) {
this.waitTime = waitTime;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void setDelay(int delay) {
this.delay = delay;
}
@Override
public String toString() {
return new ToStringCreator(this)
.append("waitTime", waitTime)
.append("enabled", enabled)
.append("delay", delay)
.toString();
}
}
/**

View File

@@ -23,6 +23,12 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
@@ -33,21 +39,15 @@ import org.springframework.retry.annotation.Retryable;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import static org.springframework.cloud.consul.config.ConsulConfigProperties.Format.FILES;
import lombok.extern.apachecommons.CommonsLog;
/**
* @author Spencer Gibb
*/
@Order(0)
@CommonsLog
public class ConsulPropertySourceLocator implements PropertySourceLocator {
private static final Log log = LogFactory.getLog(ConsulPropertySourceLocator.class);
private final ConsulClient consul;
private final ConsulConfigProperties properties;

View File

@@ -76,7 +76,7 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
verify(client, times(2)).getInstances("configserver");
ConfigClientProperties locator = this.context
.getBean(ConfigClientProperties.class);
assertEquals("http://foo:7001/", locator.getRawUri());
assertEquals("http://foo:7001/", locator.getUri()[0]);
}
private void setup(String... env) {