Use metadata if tags-as-metadata=false for config server
See gh-630
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user