@Value for properties
This commit is contained in:
@@ -5,10 +5,13 @@ import com.ecwid.consul.v1.agent.model.NewService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
@@ -17,51 +20,55 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
* Created by nicu on 11.03.2015.
|
||||
*/
|
||||
@Slf4j
|
||||
public class TtlScheduler implements ApplicationContextAware {
|
||||
private static final int DEFAULT_TTL = 3; // must be > 1
|
||||
public static final int HEARTBEAT_INTERVAL_RATIO = 2 / 3;
|
||||
@ConfigurationProperties
|
||||
public class TtlScheduler {
|
||||
|
||||
private final Map<String, Long> serviceHeartbeats = new ConcurrentHashMap<>();
|
||||
private final AtomicBoolean heartbeatingNow = new AtomicBoolean();
|
||||
private volatile int ttl;
|
||||
private volatile int heartbeatInterval;
|
||||
private final Map<String, Long> serviceHeartbeats = new ConcurrentHashMap<>();
|
||||
private final AtomicBoolean heartbeatingNow = new AtomicBoolean();
|
||||
|
||||
@Autowired
|
||||
private ConsulClient client;
|
||||
@Value("${consul.ttl:3}")
|
||||
private volatile int ttl;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
ttl = context.getEnvironment().getProperty("consul.ttl", Integer.class, DEFAULT_TTL);
|
||||
ttl = Math.min(2, ttl);
|
||||
// heartbeat at 2/3 ttl, but no later than ttl -1s and, (under lesser priority), no sooner than 1s from now
|
||||
heartbeatInterval = Math.max(ttl - 1, Math.min(ttl * HEARTBEAT_INTERVAL_RATIO, 1));
|
||||
@Value("${consul.heartbeatIntervalRatio:0.6}")
|
||||
private volatile int heartbeatIntervalRatio;
|
||||
|
||||
private volatile int heartbeatInterval;
|
||||
|
||||
@Autowired
|
||||
private ConsulClient client;
|
||||
|
||||
@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 = Math.max(ttl - 1, Math.min(ttl * heartbeatIntervalRatio, 1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a service to the checks loop.
|
||||
*/
|
||||
public void add(final NewService service) {
|
||||
serviceHeartbeats.put(service.getId(), 0L);
|
||||
}
|
||||
/**
|
||||
* Add a service to the checks loop.
|
||||
*/
|
||||
public void add(final NewService service) {
|
||||
serviceHeartbeats.put(service.getId(), 0L);
|
||||
}
|
||||
|
||||
public void remove(String serviceId) {
|
||||
serviceHeartbeats.remove(serviceId);
|
||||
}
|
||||
public void remove(String serviceId) {
|
||||
serviceHeartbeats.remove(serviceId);
|
||||
}
|
||||
|
||||
public int getTTL() {
|
||||
return ttl;
|
||||
}
|
||||
public int getTTL() {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
@Scheduled(initialDelay = 0, fixedRate = 100)
|
||||
void heartbeatServices() {
|
||||
if (heartbeatingNow.compareAndSet(false, true)) {
|
||||
for (String serviceId : serviceHeartbeats.keySet()) {
|
||||
long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId);
|
||||
if(latestHeartbeatDoneForService + heartbeatInterval <= System.currentTimeMillis()) {
|
||||
client.agentCheckPass(serviceId);
|
||||
serviceHeartbeats.put(serviceId, System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@Scheduled(initialDelay = 0, fixedRate = 100)
|
||||
private void heartbeatServices() {
|
||||
if (heartbeatingNow.compareAndSet(false, true)) {
|
||||
for (String serviceId : serviceHeartbeats.keySet()) {
|
||||
long latestHeartbeatDoneForService = serviceHeartbeats.get(serviceId);
|
||||
if (latestHeartbeatDoneForService + heartbeatInterval <= System
|
||||
.currentTimeMillis()) {
|
||||
client.agentCheckPass(serviceId);
|
||||
serviceHeartbeats.put(serviceId, System.currentTimeMillis());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user