Skip to content

Commit

Permalink
Merge pull request #1 from pahearn73/develop
Browse files Browse the repository at this point in the history
Added support for optional payload parameters.
  • Loading branch information
jchan005 committed Jul 15, 2014
2 parents 4bdd2be + 2b5feef commit d7db8c0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
25 changes: 25 additions & 0 deletions discovery-client/src/main/java/com/comcast/tvx/cloud/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.comcast.tvx.cloud;

import java.util.Map;
import java.util.UUID;

import org.codehaus.jackson.annotate.JsonProperty;
Expand Down Expand Up @@ -43,6 +44,10 @@ public final class MetaData {
@JsonProperty
private String serviceName;

/** The parameter map. */
@JsonProperty
private Map<String, String> parameters;

/**
* Instantiates a new worker metadata.
*/
Expand Down Expand Up @@ -106,4 +111,24 @@ public int getListenPort() {
public String getServiceName() {
return serviceName;
}

/**
* Gets the parameter map.
*
* @return the parameter map
*/
@JsonProperty
public Map<String, String> getParameters() {
return parameters;
}

/**
* Sets the parameter map.
* @param parameters - The parameter map
* @return void
*/
@JsonProperty
public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ public final class RegistrationClient {
/** The log. */
private Logger log = LoggerFactory.getLogger(this.getClass());

/** Payload parameters." */
private Map<String, String> parameters;

/**
* Instantiates a new registration client.
*
* @param curatorFramework the curator framework
* @param basePath Zookeeper registration path
* @param flavor Application flavor of this client.
* @param listenAddress Local IP address
* @param serviceSpec A list of services and corresponding ports.
* @param parameters A map of optional payload parameters.
*/
public RegistrationClient(CuratorFramework curatorFramework, String basePath,
String flavor, String listenAddress,
String serviceSpec,
Map<String, String> parameters) {
this(curatorFramework, basePath, flavor, listenAddress, serviceSpec);
this.parameters = parameters;
}

/**
* Instantiates a new registration client.
*
Expand All @@ -80,6 +101,7 @@ public RegistrationClient(CuratorFramework curatorFramework, String basePath,
this(curatorFramework, basePath, flavor, listenAddress, ServiceUtil.parseServiceSpec(serviceSpec));
}


/**
* Instantiates a new registration client.
*
Expand Down Expand Up @@ -120,7 +142,7 @@ public RegistrationClient advertiseAvailability() {
String regPath = constructRegistrationPath(basePath, flavor);
int port = entry.getValue().intValue();
ServiceDiscovery<MetaData> discovery = ServiceUtil.getDiscovery(regPath, curatorFramework);
ServiceInstance<MetaData> service = ServiceUtil.getServiceInstance(serviceName, port, listenAddress);
ServiceInstance<MetaData> service = ServiceUtil.getServiceInstance(serviceName, port, listenAddress, parameters);

/*
* Having >1 instance with of the same name with same listenAddress + listPort is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,23 @@ protected static Map<String, Integer> parseServiceSpec(String serviceSpec) {
* @return single instance of RegistrationClient
* @throws Exception the exception
*/
protected static ServiceInstance<MetaData> getServiceInstance(String serviceName, int servicePort,
String serviceAddress) throws Exception {
protected static ServiceInstance<MetaData> getServiceInstance(
String serviceName,
int servicePort,
String serviceAddress,
Map<String, String> parameters) throws Exception {

ServiceInstanceBuilder<MetaData> builder = ServiceInstance.builder();

// Address is optional. The Curator library will automatically use the IP from the first
// ethernet device
String registerAddress = (serviceAddress == null) ? builder.build().getAddress() : serviceAddress;

builder.name(serviceName).payload(new MetaData(UUID.randomUUID(), registerAddress, servicePort, serviceName)).id(
registerAddress + ":" + String.valueOf(servicePort)).serviceType(ServiceType.DYNAMIC).address(
registerAddress).port(servicePort);
MetaData metadata = new MetaData(UUID.randomUUID(), registerAddress, servicePort, serviceName);
metadata.setParameters(parameters);

builder.name(serviceName).payload(metadata).id(registerAddress + ":" +
String.valueOf(servicePort)).serviceType(ServiceType.DYNAMIC).address(registerAddress).port(servicePort);

return builder.build();
}
Expand Down

0 comments on commit d7db8c0

Please sign in to comment.