ConsulDiscoveryClient now respects preferIpAddress
fixes gh-41
This commit is contained in:
7
pom.xml
7
pom.xml
@@ -256,4 +256,11 @@
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
@@ -33,17 +33,29 @@ import com.ecwid.consul.v1.agent.model.Member;
|
||||
import com.ecwid.consul.v1.agent.model.Self;
|
||||
import com.ecwid.consul.v1.agent.model.Service;
|
||||
import com.ecwid.consul.v1.catalog.model.CatalogService;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
public class ConsulDiscoveryClient implements DiscoveryClient, ApplicationContextAware {
|
||||
|
||||
@Autowired
|
||||
ApplicationContext context;
|
||||
private ApplicationContext context;
|
||||
|
||||
@Autowired
|
||||
ConsulClient client;
|
||||
private ConsulClient client;
|
||||
|
||||
private ConsulDiscoveryProperties properties;
|
||||
|
||||
public ConsulDiscoveryClient(ConsulClient client, ConsulDiscoveryProperties properties) {
|
||||
this.client = client;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext context) throws BeansException {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
@@ -62,7 +74,9 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
Response<Self> agentSelf = client.getAgentSelf();
|
||||
Member member = agentSelf.getValue().getMember();
|
||||
if (member != null) {
|
||||
if (member.getName() != null) {
|
||||
if (properties.isPreferIpAddress()) {
|
||||
host = member.getAddress();
|
||||
} else if (StringUtils.hasText(member.getName())) {
|
||||
host = member.getName();
|
||||
}
|
||||
}
|
||||
@@ -82,7 +96,13 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
Response<List<CatalogService>> services = client.getCatalogService(serviceId,
|
||||
QueryParams.DEFAULT);
|
||||
for (CatalogService service : services.getValue()) {
|
||||
instances.add(new DefaultServiceInstance(serviceId, service.getNode(),
|
||||
String host;
|
||||
if (this.properties.isPreferIpAddress()) {
|
||||
host = service.getAddress();
|
||||
} else {
|
||||
host = service.getNode();
|
||||
}
|
||||
instances.add(new DefaultServiceInstance(serviceId, host,
|
||||
service.getServicePort(), false));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ public class ConsulDiscoveryClientConfiguration {
|
||||
|
||||
@Bean
|
||||
public ConsulDiscoveryClient consulDiscoveryClient() {
|
||||
return new ConsulDiscoveryClient();
|
||||
return new ConsulDiscoveryClient(consulClient, consulDiscoveryProperties());
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.WebIntegrationTest;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = ConsulDiscoveryClientTests.MyTestConfig.class)
|
||||
@WebIntegrationTest(value = {"spring.application.name=testConsulDiscovery", "spring.cloud.consul.discovery.preferIpAddress=true"}, randomPort = true)
|
||||
public class ConsulDiscoveryClientTests {
|
||||
|
||||
@Autowired
|
||||
private ConsulDiscoveryClient discoveryClient;
|
||||
|
||||
@Test
|
||||
public void getInstancesForServiceWorks() {
|
||||
List<ServiceInstance> instances = discoveryClient.getInstances("consul");
|
||||
assertNotNull("instances was null", instances);
|
||||
assertFalse("instances was empty", instances.isEmpty());
|
||||
|
||||
ServiceInstance instance = instances.get(0);
|
||||
assertIpAddress(instance);
|
||||
}
|
||||
|
||||
private void assertIpAddress(ServiceInstance instance) {
|
||||
assertTrue("host isn't an ip address", Character.isDigit(instance.getHost().charAt(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocalInstance() {
|
||||
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
|
||||
assertNotNull("instance was null", instance);
|
||||
assertIpAddress(instance);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableDiscoveryClient
|
||||
@EnableAutoConfiguration
|
||||
@Import({ ConsulAutoConfiguration.class, ConsulDiscoveryClientConfiguration.class })
|
||||
public static class MyTestConfig {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user