Only use preferIpAddress for data sent to consul.
fixes gh-114
This commit is contained in:
@@ -111,7 +111,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
Response<List<CatalogService>> services = client.getCatalogService(serviceId,
|
||||
QueryParams.DEFAULT);
|
||||
for (CatalogService service : services.getValue()) {
|
||||
String host = getCatalogServiceHost(service, properties.isPreferIpAddress());
|
||||
String host = getCatalogServiceHost(service);
|
||||
instances.add(new DefaultServiceInstance(serviceId, host,
|
||||
service.getServicePort(), false));
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ public class ConsulDiscoveryProperties {
|
||||
|
||||
private String hostname = hostInfo[1];
|
||||
|
||||
/**
|
||||
* Use ip address rather than hostname during registration
|
||||
*/
|
||||
private boolean preferIpAddress = false;
|
||||
|
||||
private int catalogServicesWatchDelay = 10;
|
||||
|
||||
@@ -50,9 +50,6 @@ public class ConsulRibbonClientConfiguration {
|
||||
@Autowired
|
||||
private ConsulClient client;
|
||||
|
||||
@Autowired
|
||||
private ConsulDiscoveryProperties properties;
|
||||
|
||||
@Value("${ribbon.client.name}")
|
||||
private String serviceId = "client";
|
||||
|
||||
@@ -70,7 +67,7 @@ public class ConsulRibbonClientConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ServerList<?> ribbonServerList(IClientConfig config) {
|
||||
ConsulServerList serverList = new ConsulServerList(client, properties);
|
||||
ConsulServerList serverList = new ConsulServerList(client);
|
||||
serverList.initWithNiwsConfig(config);
|
||||
return serverList;
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ public class ConsulServer extends Server {
|
||||
private final String address;
|
||||
private final String node;
|
||||
|
||||
public ConsulServer(final CatalogService service, boolean preferAddress) {
|
||||
super(getCatalogServiceHost(service, preferAddress), service.getServicePort());
|
||||
public ConsulServer(final CatalogService service) {
|
||||
super(getCatalogServiceHost(service), service.getServicePort());
|
||||
address = service.getAddress();
|
||||
node = service.getNode();
|
||||
metaInfo = new MetaInfo() {
|
||||
|
||||
@@ -33,13 +33,11 @@ import com.netflix.loadbalancer.AbstractServerList;
|
||||
public class ConsulServerList extends AbstractServerList<ConsulServer> {
|
||||
|
||||
private final ConsulClient client;
|
||||
private ConsulDiscoveryProperties properties;
|
||||
|
||||
private String serviceId;
|
||||
|
||||
public ConsulServerList(ConsulClient client, ConsulDiscoveryProperties properties) {
|
||||
public ConsulServerList(ConsulClient client) {
|
||||
this.client = client;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,7 +66,7 @@ public class ConsulServerList extends AbstractServerList<ConsulServer> {
|
||||
}
|
||||
List<ConsulServer> servers = new ArrayList<>();
|
||||
for (CatalogService service : response.getValue()) {
|
||||
servers.add(new ConsulServer(service, properties.isPreferIpAddress()));
|
||||
servers.add(new ConsulServer(service));
|
||||
}
|
||||
return servers;
|
||||
}
|
||||
|
||||
@@ -35,13 +35,11 @@ import com.ecwid.consul.v1.catalog.model.CatalogService;
|
||||
@CommonsLog
|
||||
public class IpAddressUtils {
|
||||
|
||||
public static String getCatalogServiceHost(CatalogService service, boolean preferAddress) {
|
||||
if (preferAddress) {
|
||||
if (StringUtils.hasText(service.getServiceAddress())) {
|
||||
return service.getServiceAddress();
|
||||
} else {
|
||||
return service.getAddress();
|
||||
}
|
||||
public static String getCatalogServiceHost(CatalogService service) {
|
||||
if (StringUtils.hasText(service.getServiceAddress())) {
|
||||
return service.getServiceAddress();
|
||||
} else if (StringUtils.hasText(service.getAddress())) {
|
||||
return service.getAddress();
|
||||
}
|
||||
return service.getNode();
|
||||
}
|
||||
|
||||
@@ -52,9 +52,6 @@ public class ConsulDiscoveryClientCustomizedTests {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("consul");
|
||||
assertNotNull("instances was null", instances);
|
||||
assertFalse("instances was empty", instances.isEmpty());
|
||||
|
||||
ServiceInstance instance = instances.get(0);
|
||||
assertNotIpAddress(instance);
|
||||
}
|
||||
|
||||
private void assertNotIpAddress(ServiceInstance instance) {
|
||||
|
||||
Reference in New Issue
Block a user