Merge pull request #105 from aatarasoff/issue-101

* issue-101:
  add hostname from consul properties as service address for service and its management
This commit is contained in:
Spencer Gibb
2015-10-27 06:09:38 -06:00
3 changed files with 73 additions and 0 deletions

View File

@@ -74,6 +74,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
Assert.notNull(service.getPort(), "service.port has not been set");
String appName = getAppName();
service.setId(getServiceId());
service.setAddress(properties.getHostname());
service.setName(normalizeForDns(appName));
service.setTags(createTags());
@@ -119,6 +120,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
protected void registerManagement() {
NewService management = new NewService();
management.setId(getManagementServiceId());
management.setAddress(properties.getHostname());
management.setName(getManagementServiceName());
management.setPort(getManagementPort());
management.setTags(properties.getManagementTags());

View File

@@ -0,0 +1,65 @@
package org.springframework.cloud.consul.discovery;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import java.util.Map;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
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.consul.ConsulAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
/**
* @author Aleksandr Tarasov (aatarasov)
*/
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringApplicationConfiguration(classes = TestConfig.class)
@WebIntegrationTest(value = {"spring.application.name=myTestService",
"spring.cloud.consul.discovery.instanceId=myTestService1", "management.port=0"}, randomPort = true)
public class ConsulLifecycleManagementServiceTests {
@Autowired
ConsulLifecycle lifecycle;
@Autowired
ConsulClient consul;
@Autowired
ConsulDiscoveryProperties discoveryProperties;
@Test
public void contextLoads() {
Response<Map<String, Service>> response = consul.getAgentServices();
Map<String, Service> services = response.getValue();
Service service = services.get("myTestService-0-management");
assertNotNull("service was null", service);
assertEquals("service port is not 0", 0, service.getPort().intValue());
assertEquals("service id was wrong", "myTestService-0-management", service.getId());
assertEquals("service name was wrong", "myTestService-management", service.getService());
assertFalse("service address must not be empty", StringUtils.isEmpty(service.getAddress()));
assertEquals("service address must equals hostname from discovery properties", discoveryProperties.getHostname(), service.getAddress());
}
@Configuration
@EnableAutoConfiguration
@Import({ConsulAutoConfiguration.class,
ConsulDiscoveryClientConfiguration.class})
public static class TestConfig {
}
}

View File

@@ -36,6 +36,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
@@ -56,6 +57,9 @@ public class ConsulLifecycleTests {
@Autowired
private ConsulClient consul;
@Autowired
private ConsulDiscoveryProperties discoveryProperties;
@Autowired
private ApplicationContext context;
@@ -69,6 +73,8 @@ public class ConsulLifecycleTests {
assertFalse("service id contained invalid character: " + service.getId(), service.getId().contains(":"));
assertEquals("service id was wrong", lifecycle.getServiceId(), service.getId());
assertEquals("service name was wrong", "myTestService1-something", service.getService());
assertFalse("service address must not be empty", StringUtils.isEmpty(service.getAddress()));
assertEquals("service address must equals hostname from discovery properties", discoveryProperties.getHostname(), service.getAddress());
}
@Test