Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.isMergeTagsEnabled()));
}
}

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 mergeTagsEnabled = 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 isMergeTagsEnabled() {
return this.mergeTagsEnabled;
}

public void setMergeTagsEnabled(boolean mergeTagsEnabled) {
this.mergeTagsEnabled = mergeTagsEnabled;
}

@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("mergeTagsEnabled", this.mergeTagsEnabled)
.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,6 +16,7 @@

package org.springframework.cloud.consul.discovery;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -25,23 +26,29 @@

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a public API so existing constructor signatures will need to remain with a default value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a constructor with default merge value as false.

I just moved the default constructor to be the first in the file.

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

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().getPort(), getSecure(healthService), getMetadata(healthService),
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);
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 +58,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 +117,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.isMergeTagsEnabled()));
}
return Flux.fromIterable(instances);
}).onErrorResume(exception -> {
Expand Down