Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor health checking logic separate objects.
- Loading branch information
Showing
5 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package services.doctor; | ||
|
||
import services.Health; | ||
import services.registry.ServiceRegistry; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Doctor { | ||
public EnvironmentHealth check(ServiceRegistry.Environment environment) { | ||
List<Health> health = new ArrayList<Health>(); | ||
for (ServiceRegistry.Service service : environment.getServices()) { | ||
health.add(new Health(service.getDomain(), service.getPort(), true)); | ||
} | ||
return new EnvironmentHealth(environment.getEnvironment(), health); | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
...main/java/services/EnvironmentHealth.java → ...va/services/doctor/EnvironmentHealth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
health/src/main/java/services/registry/ServiceRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package services.registry; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import org.xbill.DNS.Lookup; | ||
import org.xbill.DNS.Record; | ||
import org.xbill.DNS.SRVRecord; | ||
import org.xbill.DNS.SimpleResolver; | ||
import org.xbill.DNS.Type; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ServiceRegistry { | ||
public static class Service { | ||
private final String domain; | ||
private final int port; | ||
|
||
public Service(String domain, int port) { | ||
this.domain = domain; | ||
this.port = port; | ||
} | ||
|
||
public String getDomain() { | ||
return domain; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
} | ||
|
||
public static class Environment { | ||
private final String environment; | ||
private final List<Service> services; | ||
|
||
public Environment(String environment, List<Service> services) { | ||
this.environment = environment; | ||
this.services = services; | ||
} | ||
|
||
public Environment(String environment) { | ||
this(environment, ImmutableList.<Service>of()); | ||
} | ||
|
||
public String getEnvironment() { | ||
return environment; | ||
} | ||
|
||
public List<Service> getServices() { | ||
return services; | ||
} | ||
} | ||
|
||
private final String domain; | ||
private final String dns; | ||
|
||
public ServiceRegistry(String dns, String domain) { | ||
this.dns = dns; | ||
this.domain = domain; | ||
} | ||
|
||
public Environment resolve(String environment) { | ||
Record[] records = null; | ||
try { | ||
Lookup lookup = new Lookup(environment + domain, Type.SRV); | ||
lookup.setResolver(new SimpleResolver(dns)); | ||
records = lookup.run(); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (records == null) { | ||
return new Environment(environment); | ||
} else { | ||
List<Service> services = new ArrayList<Service>(); | ||
for (Record record : records) { | ||
int port = ((SRVRecord) record).getPort(); | ||
String host = ((SRVRecord) record).getTarget().toString(); | ||
services.add(new Service(host, port)); | ||
} | ||
return new Environment(environment, services); | ||
} | ||
} | ||
} |