Implement ServiceInstance.getMetadata
fixes gh-57
This commit is contained in:
@@ -19,7 +19,9 @@ package org.springframework.cloud.zookeeper.discovery;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.curator.x.discovery.ServiceInstance;
|
||||
@@ -67,7 +69,13 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient {
|
||||
port = serviceInstance.getSslPort();
|
||||
}
|
||||
|
||||
return new DefaultServiceInstance(serviceId, serviceInstance.getAddress(), port, secure);
|
||||
Map<String, String> metadata;
|
||||
if (serviceInstance.getPayload() != null) {
|
||||
metadata = serviceInstance.getPayload().getMetadata();
|
||||
} else {
|
||||
metadata = new HashMap<>();
|
||||
}
|
||||
return new DefaultServiceInstance(serviceId, serviceInstance.getAddress(), port, secure, metadata);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.zookeeper.discovery;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
@@ -31,6 +34,12 @@ public class ZookeeperDiscoveryProperties {
|
||||
|
||||
private String instanceHost;
|
||||
|
||||
/**
|
||||
* Gets the metadata name/value pairs associated with this instance. This information
|
||||
* is sent to zookeeper and can be used by other instances.
|
||||
*/
|
||||
private Map<String, String> metadata = new HashMap<>();
|
||||
|
||||
public boolean isEnabled() {
|
||||
return this.enabled;
|
||||
}
|
||||
@@ -62,4 +71,24 @@ public class ZookeeperDiscoveryProperties {
|
||||
public void setInstanceHost(String instanceHost) {
|
||||
this.instanceHost = instanceHost;
|
||||
}
|
||||
|
||||
public Map<String, String> getMetadata() {
|
||||
return this.metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(Map<String, String> metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("ZookeeperDiscoveryProperties{");
|
||||
sb.append("enabled=").append(this.enabled);
|
||||
sb.append(", root='").append(this.root).append('\'');
|
||||
sb.append(", uriSpec='").append(this.uriSpec).append('\'');
|
||||
sb.append(", instanceHost='").append(this.instanceHost).append('\'');
|
||||
sb.append(", metadata=").append(this.metadata);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,20 +16,25 @@
|
||||
|
||||
package org.springframework.cloud.zookeeper.discovery;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class ZookeeperInstance {
|
||||
private String id;
|
||||
private String name;
|
||||
private Map<String, String> metadata = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private ZookeeperInstance() {
|
||||
}
|
||||
|
||||
public ZookeeperInstance(String id, String name) {
|
||||
public ZookeeperInstance(String id, String name, Map<String, String> metadata) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
@@ -47,4 +52,22 @@ public class ZookeeperInstance {
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Map<String, String> getMetadata() {
|
||||
return this.metadata;
|
||||
}
|
||||
|
||||
public void setMetadata(Map<String, String> metadata) {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuffer sb = new StringBuffer("ZookeeperInstance{");
|
||||
sb.append("id='").append(this.id).append('\'');
|
||||
sb.append(", name='").append(this.name).append('\'');
|
||||
sb.append(", metadata=").append(this.metadata);
|
||||
sb.append('}');
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ZookeeperServiceDiscovery implements ApplicationContextAware {
|
||||
try {
|
||||
serviceInstance.set(ServiceInstance.<ZookeeperInstance>builder()
|
||||
.name(appName)
|
||||
.payload(new ZookeeperInstance(context.getId(), appName))
|
||||
.payload(new ZookeeperInstance(context.getId(), appName, this.properties.getMetadata()))
|
||||
.port(port.get())
|
||||
.address(host)
|
||||
.uriSpec(uriSpec).build());
|
||||
|
||||
@@ -66,6 +66,7 @@ class ZookeeperDiscoveryISpec extends Specification implements PollingUtils {
|
||||
ServiceInstance instance = instances.first()
|
||||
expect:
|
||||
'UP' == registeredServiceStatus(instance)
|
||||
'testMetadataValue' == instance.metadata.get('testMetadataKey')
|
||||
}
|
||||
|
||||
def 'should present application name as id of the service instance'() {
|
||||
|
||||
@@ -1 +1,8 @@
|
||||
spring.application.name: ribbonApp
|
||||
spring:
|
||||
application:
|
||||
name: ribbonApp
|
||||
cloud:
|
||||
zookeeper:
|
||||
discovery:
|
||||
metadata:
|
||||
testMetadataKey: testMetadataValue
|
||||
Reference in New Issue
Block a user