Use metadata if tags-as-metadata=false for config server

See gh-630
This commit is contained in:
Spencer Gibb
2020-04-10 15:23:37 -04:00
parent c90a461e9e
commit 3e2357a5cb
3 changed files with 22 additions and 2 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.consul.discovery;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -51,7 +52,7 @@ public class ConsulDiscoveryProperties {
private List<String> tags = new ArrayList<>();
/** Metadata to use when registering service. */
private Map<String, String> metadata;
private Map<String, String> metadata = new LinkedHashMap<>();
/** Enable tag override for the registered service. */
private Boolean enableTagOverride;

View File

@@ -39,6 +39,8 @@ import org.springframework.util.StringUtils;
ConfigServerProperties.class })
public class ConsulConfigServerAutoConfiguration {
public static final String CONFIG_PATH_KEY = "configPath";
@Autowired(required = false)
private ConsulDiscoveryProperties properties;
@@ -52,7 +54,12 @@ public class ConsulConfigServerAutoConfiguration {
}
String prefix = this.server.getPrefix();
if (StringUtils.hasText(prefix)) {
this.properties.getTags().add("configPath=" + prefix);
if (this.properties.isTagsAsMetadata()) {
this.properties.getTags().add(CONFIG_PATH_KEY + "=" + prefix);
}
else {
this.properties.getMetadata().put(CONFIG_PATH_KEY, prefix);
}
}
}

View File

@@ -63,6 +63,18 @@ public class ConsulConfigServerAutoConfigurationTests {
assertThat(properties.getTags()).containsExactly("configPath=/config");
}
@Test
public void onWhenRequestedMetadata() throws Exception {
setup("spring.cloud.config.server.prefix=/config",
"spring.cloud.consul.discovery.tags-as-metadata=false");
assertThat(
this.context.getBeanNamesForType(ConsulDiscoveryProperties.class).length)
.isEqualTo(1);
ConsulDiscoveryProperties properties = this.context
.getBean(ConsulDiscoveryProperties.class);
assertThat(properties.getMetadata()).containsEntry("configPath", "/config");
}
private void setup(String... env) {
this.context = new SpringApplicationBuilder(
PropertyPlaceholderAutoConfiguration.class,