diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/HeartbeatProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/HeartbeatProperties.java index 74025e5e..08be4f85 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/HeartbeatProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/HeartbeatProperties.java @@ -24,11 +24,13 @@ import javax.validation.constraints.NotNull; import lombok.Data; +import lombok.extern.apachecommons.CommonsLog; import org.joda.time.Period; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "consul.heartbeat") @Data +@CommonsLog public class HeartbeatProperties { @Min(1) private int ttlValue = 30; @@ -43,14 +45,22 @@ public class HeartbeatProperties { private Period heartbeatInterval; @PostConstruct - public void computeHeartbeatInterval() { - // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser - // priority), no sooner than 1s from now - heartbeatInterval = new Period(Math.round(1000 * Math.max(ttlValue - 1, - Math.min(ttlValue * intervalRatio, 1)))); + public void init() { + this.heartbeatInterval = computeHearbeatInterval(); + log.debug("Computed heartbeatInterval: " + heartbeatInterval); } - public String getTtl() { + protected Period computeHearbeatInterval() { + // heartbeat rate at ratio * ttl, but no later than ttl -1s and, (under lesser + // priority), no sooner than 1s from now + double interval = ttlValue * intervalRatio; + double max = Math.max(interval, 1); + int ttlMinus1 = ttlValue - 1; + double min = Math.min(ttlMinus1, max); + return new Period(Math.round(1000 * min)); + } + + public String getTtl() { return ttlValue + ttlUnit; } } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java index c24ac0e7..faec79ca 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TtlScheduler.java @@ -67,7 +67,7 @@ public class TtlScheduler { checkId = "service:" + checkId; } client.agentCheckPass(checkId); - log.info("Sending consul heartbeat for: " + serviceId); + log.debug("Sending consul heartbeat for: " + serviceId); serviceHeartbeats.put(serviceId, DateTime.now()); } } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/HeartbeatPropertiesTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/HeartbeatPropertiesTests.java new file mode 100644 index 00000000..bfdb5023 --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/HeartbeatPropertiesTests.java @@ -0,0 +1,50 @@ +/* + * Copyright 2013-2015 the original author or authors. + * + * 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. + */ + +package org.springframework.cloud.consul.discovery; + +import static org.junit.Assert.assertThat; +import static org.hamcrest.Matchers.*; + +import org.joda.time.Period; +import org.junit.Test; + +/** + * @author Spencer Gibb + */ +public class HeartbeatPropertiesTests { + + @Test + public void computeHeartbeatIntervalWorks() { + HeartbeatProperties properties = new HeartbeatProperties(); + Period period = properties.computeHearbeatInterval(); + + assertThat(period, is(notNullValue())); + assertThat(period.getSeconds(), is(20)); + } + + @Test + public void computeShortHeartbeat() { + HeartbeatProperties properties = new HeartbeatProperties(); + properties.setTtlValue(2); + Period period = properties.computeHearbeatInterval(); + + assertThat(period, is(notNullValue())); + assertThat(period.getSeconds(), is(1)); + } + + +} diff --git a/spring-cloud-consul-sample/src/main/resources/application.yml b/spring-cloud-consul-sample/src/main/resources/application.yml index d3542bfd..c8d3ccb3 100644 --- a/spring-cloud-consul-sample/src/main/resources/application.yml +++ b/spring-cloud-consul-sample/src/main/resources/application.yml @@ -15,4 +15,8 @@ endpoints: restart: enabled: true shutdown: - enabled: true \ No newline at end of file + enabled: true + +logging: + level: + org.springframework.cloud.consul: DEBUG \ No newline at end of file