Allows configuration of curator instance-id via properties.

fixes gh-140
This commit is contained in:
Spencer Gibb
2017-10-23 18:17:56 -04:00
parent b2cc6c9fd5
commit 1f10b523f0
6 changed files with 31 additions and 4 deletions

View File

@@ -49,6 +49,9 @@ public class ZookeeperDiscoveryProperties {
*/
private String uriSpec = DEFAULT_URI_SPEC;
/** Id used to register with zookeeper. Defaults to a random UUID. */
private String instanceId;
/**
* Predefined host with which a service can register itself in Zookeeper. Corresponds
* to the {code address} from the URI spec.
@@ -126,6 +129,14 @@ public class ZookeeperDiscoveryProperties {
this.uriSpec = uriSpec;
}
public String getInstanceId() {
return this.instanceId;
}
public void setInstanceId(String instanceId) {
this.instanceId = instanceId;
}
public void setInstanceHost(String instanceHost) {
this.instanceHost = instanceHost;
this.hostInfo.override = true;
@@ -185,6 +196,7 @@ public class ZookeeperDiscoveryProperties {
return "ZookeeperDiscoveryProperties{" + "enabled=" + this.enabled +
", root='" + this.root + '\'' +
", uriSpec='" + this.uriSpec + '\'' +
", instanceId='" + this.instanceId + '\'' +
", instanceHost='" + this.instanceHost + '\'' +
", instancePort='" + this.instancePort + '\'' +
", instanceSslPort='" + this.instanceSslPort + '\'' +

View File

@@ -198,6 +198,9 @@ public class ZookeeperServiceDiscovery implements ZookeeperRegistration, Applica
if (this.properties.getInstanceSslPort() != null) {
builder.sslPort(this.properties.getInstanceSslPort());
}
if (this.properties.getInstanceId() != null) {
builder.id(this.properties.getInstanceId());
}
serviceInstance.set(builder.build());
}
catch (Exception e) {

View File

@@ -36,6 +36,7 @@ public class ZookeeperServiceInstance implements ServiceInstance {
private final boolean secure;
private final URI uri;
private final Map<String, String> metadata;
private final org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> serviceInstance;
/**
* @param serviceId The service id to be used
@@ -43,7 +44,8 @@ public class ZookeeperServiceInstance implements ServiceInstance {
*/
public ZookeeperServiceInstance(String serviceId, org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> serviceInstance) {
this.serviceId = serviceId;
this.host = serviceInstance.getAddress();
this.serviceInstance = serviceInstance;
this.host = this.serviceInstance.getAddress();
this.secure = serviceInstance.getSslPort() != null;
Integer port = serviceInstance.getPort();
if (this.secure) {
@@ -87,4 +89,8 @@ public class ZookeeperServiceInstance implements ServiceInstance {
public Map<String, String> getMetadata() {
return this.metadata;
}
public org.apache.curator.x.discovery.ServiceInstance<ZookeeperInstance> getServiceInstance() {
return this.serviceInstance;
}
}

View File

@@ -11,13 +11,14 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author wmz7year
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(properties = {"pring.application.name=testZookeeperDiscovery",
"spring.cloud.zookeeper.discovery.instance-id=zkpropstestid-123",
"spring.cloud.zookeeper.discovery.preferIpAddress=true",
"spring.cloud.zookeeper.discovery.instanceIpAddress=1.1.1.1"},
classes = ZookeeperDiscoveryPropertiesTests.Config.class,
@@ -29,8 +30,9 @@ public class ZookeeperDiscoveryPropertiesTests {
@Test
public void testPreferIpAddress() {
assertEquals("1.1.1.1", discoveryProperties.getInstanceHost());
}
assertThat(this.discoveryProperties.getInstanceId()).isEqualTo("zkpropstestid-123");
assertThat(this.discoveryProperties.getInstanceHost()).isEqualTo("1.1.1.1");
}
@Configuration
@EnableAutoConfiguration

View File

@@ -66,6 +66,9 @@ public class ZookeeperDiscoveryTests {
//expect:
then(registeredServiceStatus(instance)).isEqualTo("UP");
then(instance.getMetadata().get("testMetadataKey")).isEqualTo("testMetadataValue");
then(instance).isInstanceOf(ZookeeperServiceInstance.class);
ZookeeperServiceInstance zkInstance = (ZookeeperServiceInstance) instance;
then(zkInstance.getServiceInstance().getId()).isEqualTo("ribbon-instance-id-123");
}
@Test public void should_present_application_name_as_id_of_the_service_instance() {

View File

@@ -7,6 +7,7 @@ spring:
metadata:
testMetadataKey: testMetadataValue
uriSpec: "{scheme}://{address}:{port}/contextPath"
instance-id: ribbon-instance-id-123
management:
security:
enabled: false