Log warning if local instance fails.

If DiscoveryClient.getLocalServiceInstance() throws an exception (ie consul isn't available), log a warning and continue. This is useful for dev type environments.

fixes gh-1603
This commit is contained in:
Spencer Gibb
2017-02-28 10:26:15 -07:00
parent 99d9b17a2d
commit 309b280d0d
2 changed files with 19 additions and 1 deletions

View File

@@ -57,7 +57,12 @@ public class DiscoveryClientRouteLocator extends SimpleRouteLocator
super(servletPath, properties);
if (properties.isIgnoreLocalService()) {
ServiceInstance instance = discovery.getLocalServiceInstance();
ServiceInstance instance = null;
try {
instance = discovery.getLocalServiceInstance();
} catch (Exception e) {
log.warn("Error locating local service instance", e);
}
if (instance != null) {
String localServiceId = instance.getServiceId();
if (!properties.getIgnoredServices().contains(localServiceId)) {

View File

@@ -601,6 +601,19 @@ public class DiscoveryClientRouteLocatorTests {
assertMapping(routesMap, MYSERVICE);
}
@Test
public void testLocalServiceExceptionIgnored() {
given(this.discovery.getServices())
.willReturn(Collections.<String>emptyList());
given(this.discovery.getLocalServiceInstance()).willThrow(new RuntimeException());
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/",
this.discovery, this.properties);
// if no exception is thrown in constructor, this is a success
routeLocator.locateRoutes();
}
@Test
public void testRegExServiceRouteMapperNoServiceIdMatches() {
given(this.discovery.getServices())