fix + rename package, format eclipse
This commit is contained in:
@@ -29,8 +29,8 @@ public class TtlScheduler {
|
||||
@Value("${consul.ttl:3}")
|
||||
private volatile int ttl;
|
||||
|
||||
@Value("${consul.heartbeatIntervalRatio:0.6}")
|
||||
private volatile int heartbeatIntervalRatio;
|
||||
@Value("${consul.heartbeatIntervalRatio:0.66}")
|
||||
private volatile float heartbeatIntervalRatio;
|
||||
|
||||
private volatile int heartbeatInterval;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class TtlScheduler {
|
||||
@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));
|
||||
heartbeatInterval = Math.round(Math.max(ttl - 1, Math.min(ttl * heartbeatIntervalRatio, 1)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package org.springframework.cloud.consul.alive;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Injects a server list filter for giving servers hosting a service only for the live servers per serf status.
|
||||
* @author nicu marasoiu on 10.03.2015.
|
||||
*/
|
||||
@Configuration
|
||||
public class AliveFilteringContext {
|
||||
@Bean
|
||||
@Autowired
|
||||
public ServerListFilter<Server> aliveServerListFilter(FilteringAgentClient filteringAgentClient) {
|
||||
return adapter(new AliveServerListFilter(filteringAgentClient));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Autowired
|
||||
public ServerListFilter<Server> ttlServerListFilter() {
|
||||
return adapter(new ServiceCheckServerListFilter());
|
||||
}
|
||||
|
||||
private ServerListFilter<Server> adapter(final ServerListFilter<ConsulServer> consulServerList) {
|
||||
|
||||
return new ServerListFilter<Server>() {
|
||||
@Override
|
||||
public List<Server> getFilteredListOfServers(List<Server> servers) {
|
||||
return adapt2(consulServerList.getFilteredListOfServers(adapt1(servers)));
|
||||
}
|
||||
|
||||
private List<ConsulServer> adapt1(List<Server> servers) {
|
||||
List<ConsulServer> consulServers = new ArrayList<ConsulServer>(servers.size());
|
||||
for (Server consulServer : servers) {
|
||||
consulServers.add((ConsulServer) consulServer);
|
||||
}
|
||||
return consulServers;
|
||||
}
|
||||
|
||||
private List<Server> adapt2(List<ConsulServer> consulServers) {
|
||||
List<Server> servers = new ArrayList<Server>(consulServers.size());
|
||||
for (ConsulServer consulServer : consulServers) {
|
||||
servers.add(consulServer);
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package org.springframework.cloud.consul.alive;
|
||||
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Server filter: returns only alive servers.
|
||||
* Each consul agent runs a serf agent which is a member of the serf gossip pool.
|
||||
* The serf status (alive/failed/etc) is reflected in 2 consul APIs: in the agent API and in the catalog API.
|
||||
* We prefer the agent API because it is most up to date (or perhaps we should intersect them and pick members that are liv in both).
|
||||
* @author nicu marasoiu on 10.03.2015.
|
||||
*/
|
||||
public class AliveServerListFilter implements ServerListFilter<ConsulServer>{
|
||||
private FilteringAgentClient filteringAgentClient;
|
||||
|
||||
@Autowired
|
||||
public AliveServerListFilter(FilteringAgentClient filteringAgentClient) {
|
||||
this.filteringAgentClient = filteringAgentClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConsulServer> getFilteredListOfServers(List<ConsulServer> servers) {
|
||||
Set<String> liveNodes = filteringAgentClient.getAliveAgentsAddresses();
|
||||
List<ConsulServer> filteredServers = new ArrayList<>();
|
||||
for (ConsulServer server : servers) {
|
||||
if (liveNodes.contains(server.getAddress())) {
|
||||
filteredServers.add(server);
|
||||
}
|
||||
}
|
||||
return filteredServers;
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.springframework.cloud.consul.alive;
|
||||
|
||||
import com.ecwid.consul.v1.agent.model.Member;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A CatalogClient which decorates some methods of CatalogClient with filtering, retaining info pertaining to live nodes.
|
||||
* @author nicu on 10.03.2015.
|
||||
*/
|
||||
public interface FilteringAgentClient {
|
||||
/**
|
||||
* @return the set of alive gossip pool members (client or server consul agents).
|
||||
*/
|
||||
List<Member> getAliveAgents();
|
||||
/**
|
||||
* @return the set of alive gossip pool members addresses.
|
||||
*/
|
||||
Set<String> getAliveAgentsAddresses();
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package org.springframework.cloud.consul.alive;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.agent.model.Member;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.model.SerfStatusEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class FilteringAgentClientImpl implements FilteringAgentClient {
|
||||
public static final int ALIVE_STATUS = SerfStatusEnum.StatusAlive.getCode();
|
||||
private final ConsulClient client;
|
||||
|
||||
@Autowired
|
||||
public FilteringAgentClientImpl(ConsulClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Member> getAliveAgents() {
|
||||
List<Member> members = client.getAgentMembers().getValue();
|
||||
List<Member> liveMembers = new ArrayList<>(members.size());
|
||||
for (Member peer : members) {
|
||||
if (peer.getStatus() == ALIVE_STATUS) {
|
||||
liveMembers.add(peer);
|
||||
}
|
||||
}
|
||||
return liveMembers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAliveAgentsAddresses() {
|
||||
Set<String> addresses = new HashSet<String>();
|
||||
for (Member server : getAliveAgents()) {
|
||||
addresses.add(server.getAddress());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.springframework.cloud.consul.alive;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.health.model.Check;
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Created by nicu on 12.03.2015.
|
||||
*/
|
||||
public class ServiceCheckServerListFilter implements ServerListFilter<ConsulServer> {
|
||||
|
||||
@Autowired
|
||||
private ConsulClient client;
|
||||
|
||||
@Override
|
||||
public List<ConsulServer> getFilteredListOfServers(List<ConsulServer> servers) {
|
||||
Set<String> passingServiceIds = getPassingServiceIds(servers);
|
||||
List<ConsulServer> okServers = new ArrayList<>(servers.size());
|
||||
for(ConsulServer consulServer: servers){
|
||||
String serviceId = consulServer.getMetaInfo().getInstanceId();
|
||||
if(passingServiceIds.contains(serviceId)){
|
||||
List<Check> nodeChecks = client.getHealthChecksForNode(consulServer.getNode(), QueryParams.DEFAULT).getValue();
|
||||
boolean passingNodeChecks = true;
|
||||
for(Check check: nodeChecks) {
|
||||
if(check.getStatus() != Check.CheckStatus.PASSING) {
|
||||
passingNodeChecks = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(passingNodeChecks){
|
||||
okServers.add(consulServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
return okServers;
|
||||
}
|
||||
|
||||
private Set<String> getPassingServiceIds(List<ConsulServer> servers) {
|
||||
Set<String> serviceIds = new HashSet<>(1);
|
||||
for(ConsulServer server:servers){
|
||||
serviceIds.add(server.getMetaInfo().getInstanceId());
|
||||
}
|
||||
for(String serviceId: serviceIds) {
|
||||
List<Check> serviceChecks = client.getHealthChecksForService(serviceId, QueryParams.DEFAULT).getValue();
|
||||
for(Check check: serviceChecks){
|
||||
if(check.getStatus() != Check.CheckStatus.PASSING) {
|
||||
serviceIds.remove(check.getServiceId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return serviceIds;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package org.springframework.cloud.consul.serverlistfilters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
|
||||
/**
|
||||
* Server filter: returns only alive servers. Each consul agent runs a serf agent which is
|
||||
* a member of the serf gossip pool. The serf status (alive/failed/etc) is reflected in 2
|
||||
* consul APIs: in the agent API and in the catalog API. We prefer the agent API because
|
||||
* it is most up to date (or perhaps we should intersect them and pick members that are
|
||||
* liv in both).
|
||||
* @author nicu marasoiu on 10.03.2015.
|
||||
*/
|
||||
public class AliveServerListFilter implements ServerListFilter<ConsulServer> {
|
||||
private FilteringAgentClient filteringAgentClient;
|
||||
|
||||
@Autowired
|
||||
public AliveServerListFilter(FilteringAgentClient filteringAgentClient) {
|
||||
this.filteringAgentClient = filteringAgentClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ConsulServer> getFilteredListOfServers(List<ConsulServer> servers) {
|
||||
Set<String> liveNodes = filteringAgentClient.getAliveAgentsAddresses();
|
||||
List<ConsulServer> filteredServers = new ArrayList<>();
|
||||
for (ConsulServer server : servers) {
|
||||
if (liveNodes.contains(server.getAddress())) {
|
||||
filteredServers.add(server);
|
||||
}
|
||||
}
|
||||
return filteredServers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.springframework.cloud.consul.serverlistfilters;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.ecwid.consul.v1.agent.model.Member;
|
||||
|
||||
/**
|
||||
* A CatalogClient which decorates some methods of CatalogClient with filtering, retaining
|
||||
* info pertaining to live nodes.
|
||||
* @author nicu on 10.03.2015.
|
||||
*/
|
||||
public interface FilteringAgentClient {
|
||||
/**
|
||||
* @return the set of alive gossip pool members (client or server consul agents).
|
||||
*/
|
||||
List<Member> getAliveAgents();
|
||||
|
||||
/**
|
||||
* @return the set of alive gossip pool members addresses.
|
||||
*/
|
||||
Set<String> getAliveAgentsAddresses();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package org.springframework.cloud.consul.serverlistfilters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.model.SerfStatusEnum;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.agent.model.Member;
|
||||
|
||||
@Service
|
||||
public class FilteringAgentClientImpl implements FilteringAgentClient {
|
||||
public static final int ALIVE_STATUS = SerfStatusEnum.StatusAlive.getCode();
|
||||
private final ConsulClient client;
|
||||
|
||||
@Autowired
|
||||
public FilteringAgentClientImpl(ConsulClient client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Member> getAliveAgents() {
|
||||
List<Member> members = client.getAgentMembers().getValue();
|
||||
List<Member> liveMembers = new ArrayList<>(members.size());
|
||||
for (Member peer : members) {
|
||||
if (peer.getStatus() == ALIVE_STATUS) {
|
||||
liveMembers.add(peer);
|
||||
}
|
||||
}
|
||||
return liveMembers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAliveAgentsAddresses() {
|
||||
Set<String> addresses = new HashSet<String>();
|
||||
for (Member server : getAliveAgents()) {
|
||||
addresses.add(server.getAddress());
|
||||
}
|
||||
return addresses;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package org.springframework.cloud.consul.serverlistfilters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.QueryParams;
|
||||
import com.ecwid.consul.v1.health.model.Check;
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
|
||||
/**
|
||||
* Created by nicu on 12.03.2015.
|
||||
*/
|
||||
public class ServiceCheckServerListFilter implements ServerListFilter<ConsulServer> {
|
||||
|
||||
@Autowired
|
||||
private ConsulClient client;
|
||||
|
||||
@Override
|
||||
public List<ConsulServer> getFilteredListOfServers(List<ConsulServer> servers) {
|
||||
Set<String> passingServiceIds = getPassingServiceIds(servers);
|
||||
List<ConsulServer> okServers = new ArrayList<>(servers.size());
|
||||
for (ConsulServer consulServer : servers) {
|
||||
String serviceId = consulServer.getMetaInfo().getInstanceId();
|
||||
if (passingServiceIds.contains(serviceId)) {
|
||||
List<Check> nodeChecks = client.getHealthChecksForNode(
|
||||
consulServer.getNode(), QueryParams.DEFAULT).getValue();
|
||||
boolean passingNodeChecks = true;
|
||||
for (Check check : nodeChecks) {
|
||||
if (check.getStatus() != Check.CheckStatus.PASSING) {
|
||||
passingNodeChecks = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (passingNodeChecks) {
|
||||
okServers.add(consulServer);
|
||||
}
|
||||
}
|
||||
}
|
||||
return okServers;
|
||||
}
|
||||
|
||||
private Set<String> getPassingServiceIds(List<ConsulServer> servers) {
|
||||
Set<String> serviceIds = new HashSet<>(1);
|
||||
for (ConsulServer server : servers) {
|
||||
serviceIds.add(server.getMetaInfo().getInstanceId());
|
||||
}
|
||||
for (String serviceId : serviceIds) {
|
||||
List<Check> serviceChecks = client.getHealthChecksForService(serviceId,
|
||||
QueryParams.DEFAULT).getValue();
|
||||
for (Check check : serviceChecks) {
|
||||
if (check.getStatus() != Check.CheckStatus.PASSING) {
|
||||
serviceIds.remove(check.getServiceId());
|
||||
}
|
||||
}
|
||||
}
|
||||
return serviceIds;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package org.springframework.cloud.consul.serverlistfilters;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.consul.discovery.ConsulServer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerListFilter;
|
||||
|
||||
/**
|
||||
* Injects a server list filter for giving servers hosting a service only for the live
|
||||
* servers per serf status.
|
||||
* @author nicu marasoiu on 10.03.2015.
|
||||
*/
|
||||
@Configuration
|
||||
public class UsefulConsulServerListFiltersFilteringContext {
|
||||
@Bean
|
||||
@Autowired
|
||||
public ServerListFilter<Server> aliveServerListFilter(
|
||||
FilteringAgentClient filteringAgentClient) {
|
||||
return adapter(new AliveServerListFilter(filteringAgentClient));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Autowired
|
||||
public ServerListFilter<Server> ttlServerListFilter() {
|
||||
return adapter(new ServiceCheckServerListFilter());
|
||||
}
|
||||
|
||||
private ServerListFilter<Server> adapter(
|
||||
final ServerListFilter<ConsulServer> consulServerList) {
|
||||
|
||||
return new ServerListFilter<Server>() {
|
||||
@Override
|
||||
public List<Server> getFilteredListOfServers(List<Server> servers) {
|
||||
return adapt2(consulServerList.getFilteredListOfServers(adapt1(servers)));
|
||||
}
|
||||
|
||||
private List<ConsulServer> adapt1(List<Server> servers) {
|
||||
List<ConsulServer> consulServers = new ArrayList<ConsulServer>(
|
||||
servers.size());
|
||||
for (Server consulServer : servers) {
|
||||
consulServers.add((ConsulServer) consulServer);
|
||||
}
|
||||
return consulServers;
|
||||
}
|
||||
|
||||
private List<Server> adapt2(List<ConsulServer> consulServers) {
|
||||
List<Server> servers = new ArrayList<Server>(consulServers.size());
|
||||
for (ConsulServer consulServer : consulServers) {
|
||||
servers.add(consulServer);
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user