Skip to content

Commit

Permalink
Reinstate tags-as-metadata by adding option to merge tags with metada…
Browse files Browse the repository at this point in the history
…ta. Fixes spring-cloud#699
  • Loading branch information
lucasoares committed Jan 20, 2021
1 parent eb98d86 commit ff5368a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ private void addInstancesToList(List<ServiceInstance> instances, String serviceI
Response<List<HealthService>> services = this.client.getHealthServices(serviceId, request);

for (HealthService service : services.getValue()) {
instances.add(new ConsulServiceInstance(service, serviceId));
instances
.add(new ConsulServiceInstance(service, serviceId, this.properties.isEnableTagMerge()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,12 @@ public class ConsulDiscoveryProperties {
*/
private int order = 0;

/**
* Enable merge consul tag data with metadata to create service instance (defaults
* to false).
*/
private boolean enableTagMerge = false;

@SuppressWarnings("unused")
private ConsulDiscoveryProperties() {
this.managementTags.add(MANAGEMENT);
Expand Down Expand Up @@ -580,6 +586,14 @@ public void setManagementEnableTagOverride(Boolean managementEnableTagOverride)
this.managementEnableTagOverride = managementEnableTagOverride;
}

public boolean isEnableTagMerge() {
return this.enableTagMerge;
}

public void setEnableTagMerge(boolean enableTagMerge) {
this.enableTagMerge = enableTagMerge;
}

@Override
public String toString() {
return new ToStringCreator(this).append("aclToken", this.aclToken)
Expand All @@ -591,6 +605,7 @@ public String toString() {
.append("enabled", this.enabled).append("enableTagOverride", this.enableTagOverride)
.append("failFast", this.failFast).append("hostInfo", this.hostInfo)
.append("healthCheckCriticalTimeout", this.healthCheckCriticalTimeout)
.append("enableTagMerge", this.enableTagMerge)
.append("healthCheckHeaders", this.healthCheckHeaders)
.append("healthCheckInterval", this.healthCheckInterval).append("healthCheckPath", this.healthCheckPath)
.append("healthCheckTimeout", this.healthCheckTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,35 @@

package org.springframework.cloud.consul.discovery;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import com.ecwid.consul.v1.health.model.HealthService;

import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.core.style.ToStringCreator;
import org.springframework.util.StringUtils;

import static org.springframework.cloud.consul.discovery.ConsulServerUtils.findHost;

public class ConsulServiceInstance extends DefaultServiceInstance {

private HealthService healthService;

public ConsulServiceInstance(HealthService healthService, String serviceId) {
public ConsulServiceInstance(HealthService healthService, String serviceId, boolean mergeTags) {
this(healthService.getService().getId(), serviceId, findHost(healthService),
healthService.getService().getPort(), getSecure(healthService), getMetadata(healthService),
healthService.getService().getTags());
healthService.getService().getTags(), mergeTags);
this.healthService = healthService;
}

public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
Map<String, String> metadata, List<String> tags) {
super(instanceId, serviceId, host, port, secure, metadata);
this(instanceId, serviceId, host, port, secure, metadata, tags, false);
}

public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure,
Map<String, String> metadata, List<String> tags, boolean mergeTags) {
super(instanceId, serviceId, host, port, secure, mergeTags ? mergeTags(metadata, tags) : metadata);
}

public ConsulServiceInstance(String instanceId, String serviceId, String host, int port, boolean secure) {
Expand All @@ -51,6 +54,39 @@ public ConsulServiceInstance(String instanceId, String serviceId, String host, i
public ConsulServiceInstance() {
}

private static Map<String, String> mergeTags(Map<String, String> metadata, List<String> tags) {
Map<String, String> result = new LinkedHashMap<>();

if (metadata != null) {
result.putAll(metadata);
}

if (tags == null || tags.isEmpty()) {
return result;
}

for (String tag : tags) {
String[] parts = StringUtils.delimitedListToStringArray(tag, "=");

switch (parts.length) {
case 0:
break;
case 1:
result.put(parts[0], parts[0]);
break;
case 2:
result.put(parts[0], parts[1]);
break;
default:
String[] end = Arrays.copyOfRange(parts, 1, parts.length);
result.put(parts[0], StringUtils.arrayToDelimitedString(end, "="));
break;
}
}

return result;
}

private static Map<String, String> getMetadata(HealthService healthService) {
Map<String, String> metadata = healthService.getService().getMeta();
if (metadata == null) {
Expand All @@ -77,8 +113,8 @@ public void setHealthService(HealthService healthService) {
}

public List<String> getTags() {
if (healthService != null) {
return healthService.getService().getTags();
if (this.healthService != null) {
return this.healthService.getService().getTags();
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public Flux<ServiceInstance> getInstances(String serviceId) {
return Flux.defer(() -> {
List<ServiceInstance> instances = new ArrayList<>();
for (HealthService healthService : getHealthServices(serviceId)) {
instances.add(new ConsulServiceInstance(healthService, serviceId));
instances.add(new ConsulServiceInstance(healthService, serviceId,
this.properties.isEnableTagMerge()));
}
return Flux.fromIterable(instances);
}).onErrorResume(exception -> {
Expand Down

0 comments on commit ff5368a

Please sign in to comment.