-
Notifications
You must be signed in to change notification settings - Fork 540
Reinstate tags-as-metadata adding option to merge tags. Fixes #699 #700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ff5368a
8e10f73
bc89868
3450384
f060e70
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is already a constructor with default merge value as I just moved the default constructor to be the first in the file. |
||
| 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) { | ||
|
|
@@ -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) { | ||
|
|
@@ -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(); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to
mergeTagsEnabledUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok