From 6bbe82998d021d86fb1d870663467972d6ddd23f Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 9 Apr 2020 02:12:18 -0400 Subject: [PATCH 1/3] Adds support for metadata in a backwards compatible way. fixes gh-575 --- .../discovery/ConsulDiscoveryClient.java | 3 +- .../discovery/ConsulDiscoveryProperties.java | 64 ++++++++++++++++++- .../cloud/consul/discovery/ConsulServer.java | 7 +- .../consul/discovery/ConsulServerList.java | 3 +- .../consul/discovery/ConsulServerUtils.java | 13 +++- .../ConsulReactiveDiscoveryClient.java | 3 +- .../ConsulAutoRegistration.java | 56 ++++++++++++---- .../serviceregistry/ConsulRegistration.java | 5 +- .../ConsulReactiveDiscoveryClientTests.java | 3 +- 9 files changed, 138 insertions(+), 19 deletions(-) diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java index f64cf6fd..bfd542ee 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryClient.java @@ -88,7 +88,8 @@ public class ConsulDiscoveryClient implements DiscoveryClient { for (HealthService service : services.getValue()) { String host = findHost(service); - Map metadata = getMetadata(service); + Map metadata = getMetadata(service, + this.properties.isTagsAsMetadata()); boolean secure = false; if (metadata.containsKey("secure")) { secure = Boolean.parseBoolean(metadata.get("secure")); diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index e180f228..2889dbfc 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -50,12 +50,28 @@ public class ConsulDiscoveryProperties { /** Tags to use when registering service. */ private List tags = new ArrayList<>(); + /** Metadata to use when registering service. */ + private Map metadata; + + /** Enable tag override for the registered service. */ + private Boolean enableTagOverride; + + /** Use tags as metadata, defaults to true. */ + @Deprecated + private boolean tagsAsMetadata = true; + /** Is service discovery enabled? */ private boolean enabled = true; /** Tags to use when registering management service. */ private List managementTags = new ArrayList<>(); + /** Enable tag override for the registered management service. */ + private Boolean managementEnableTagOverride; + + /** Metadata to use when registering management service. */ + private Map managementMetadata; + /** Alternate server path to invoke for health checking. */ private String healthCheckPath = "/actuator/health"; @@ -522,6 +538,48 @@ public class ConsulDiscoveryProperties { this.order = order; } + public Map getMetadata() { + return this.metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + @Deprecated + public boolean isTagsAsMetadata() { + return this.tagsAsMetadata; + } + + @Deprecated + public void setTagsAsMetadata(boolean tagsAsMetadata) { + this.tagsAsMetadata = tagsAsMetadata; + } + + public Map getManagementMetadata() { + return this.managementMetadata; + } + + public void setManagementMetadata(Map managementMetadata) { + this.managementMetadata = managementMetadata; + } + + public Boolean getEnableTagOverride() { + return this.enableTagOverride; + } + + public void setEnableTagOverride(Boolean enableTagOverride) { + this.enableTagOverride = enableTagOverride; + } + + public Boolean getManagementEnableTagOverride() { + return this.managementEnableTagOverride; + } + + public void setManagementEnableTagOverride(Boolean managementEnableTagOverride) { + this.managementEnableTagOverride = managementEnableTagOverride; + } + @Override public String toString() { return new ToStringCreator(this).append("hostInfo", this.hostInfo) @@ -558,7 +616,11 @@ public class ConsulDiscoveryProperties { .append("registerHealthCheck", this.registerHealthCheck) .append("failFast", this.failFast) .append("healthCheckTlsSkipVerify", this.healthCheckTlsSkipVerify) - .append("order", this.order).toString(); + .append("order", this.order).append("metadata", this.metadata) + .append("tagsAsMetadata", this.tagsAsMetadata) + .append("enableTagOverride", this.enableTagOverride) + .append("managementEnableTagOverride", this.managementEnableTagOverride) + .append("managementMetadata", this.managementMetadata).toString(); } /** diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java index 5f0fdae4..7c28c523 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServer.java @@ -36,9 +36,14 @@ public class ConsulServer extends Server { private final Map metadata; public ConsulServer(final HealthService healthService) { + this(healthService, true); + } + + @Deprecated + public ConsulServer(final HealthService healthService, boolean tagsAsMetadata) { super(findHost(healthService), healthService.getService().getPort()); this.service = healthService; - this.metadata = ConsulServerUtils.getMetadata(this.service); + this.metadata = ConsulServerUtils.getMetadata(this.service, tagsAsMetadata); this.metaInfo = new MetaInfo() { @Override public String getAppName() { diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java index 60901403..b2ebbcb1 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerList.java @@ -99,7 +99,8 @@ public class ConsulServerList extends AbstractServerList { protected List transformResponse(List healthServices) { List servers = new ArrayList<>(); for (HealthService service : healthServices) { - ConsulServer server = new ConsulServer(service); + ConsulServer server = new ConsulServer(service, + properties.isTagsAsMetadata()); if (server.getMetadata() .containsKey(this.properties.getDefaultZoneMetadataName())) { server.setZone(server.getMetadata() diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerUtils.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerUtils.java index 52f3be12..ee5632da 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerUtils.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulServerUtils.java @@ -69,10 +69,21 @@ public final class ConsulServerUtils { } } + @Deprecated public static Map getMetadata(HealthService healthService) { - return getMetadata(healthService.getService().getTags()); + return getMetadata(healthService, true); } + @Deprecated + public static Map getMetadata(HealthService healthService, + boolean tagsAsMetadata) { + if (tagsAsMetadata) { + return getMetadata(healthService.getService().getTags()); + } + return healthService.getService().getMeta(); + } + + @Deprecated public static Map getMetadata(List tags) { LinkedHashMap metadata = new LinkedHashMap<>(); if (tags != null) { diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClient.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClient.java index db8325c4..9ac2f3ba 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClient.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClient.java @@ -94,7 +94,8 @@ public class ConsulReactiveDiscoveryClient implements ReactiveDiscoveryClient { private ServiceInstance mapToServiceInstance(HealthService service, String serviceId) { String host = findHost(service); - Map metadata = getMetadata(service); + Map metadata = getMetadata(service, + properties.isTagsAsMetadata()); boolean secure = false; if (metadata.containsKey("secure")) { secure = Boolean.parseBoolean(metadata.get("secure")); diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java index 0f02b94a..32b212b7 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java @@ -17,8 +17,10 @@ package org.springframework.cloud.consul.serviceregistry; import java.util.Collections; +import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import com.ecwid.consul.v1.agent.model.NewService; @@ -31,6 +33,7 @@ import org.springframework.cloud.consul.discovery.HeartbeatProperties; import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -87,6 +90,8 @@ public class ConsulAutoRegistration extends ConsulRegistration { } service.setName(normalizeForDns(appName)); service.setTags(createTags(properties)); + service.setEnableTagOverride(properties.getEnableTagOverride()); + service.setMeta(getMetadata(properties)); if (properties.getPort() != null) { service.setPort(properties.getPort()); @@ -142,6 +147,8 @@ public class ConsulAutoRegistration extends ConsulRegistration { .setName(getManagementServiceName(properties, context.getEnvironment())); management.setPort(getManagementPort(properties, context)); management.setTags(properties.getManagementTags()); + management.setEnableTagOverride(properties.getManagementEnableTagOverride()); + management.setMeta(properties.getManagementMetadata()); if (properties.isRegisterHealthCheck()) { management.setCheck(createCheck(getManagementPort(properties, context), heartbeatProperties, properties)); @@ -201,25 +208,52 @@ public class ConsulAutoRegistration extends ConsulRegistration { return normalized.toString(); } + @Deprecated public static List createTags(ConsulDiscoveryProperties properties) { List tags = new LinkedList<>(properties.getTags()); + if (properties.isTagsAsMetadata()) { + if (!StringUtils.isEmpty(properties.getInstanceZone())) { + tags.add(properties.getDefaultZoneMetadataName() + "=" + + properties.getInstanceZone()); + } + if (!StringUtils.isEmpty(properties.getInstanceGroup())) { + tags.add("group=" + properties.getInstanceGroup()); + } - if (!StringUtils.isEmpty(properties.getInstanceZone())) { - tags.add(properties.getDefaultZoneMetadataName() + "=" - + properties.getInstanceZone()); + // store the secure flag in the tags so that clients will be able to figure + // out whether to use http or https automatically + tags.add("secure=" + + Boolean.toString(properties.getScheme().equalsIgnoreCase("https"))); } - if (!StringUtils.isEmpty(properties.getInstanceGroup())) { - tags.add("group=" + properties.getInstanceGroup()); - } - - // store the secure flag in the tags so that clients will be able to figure out - // whether to use http or https automatically - tags.add("secure=" - + Boolean.toString(properties.getScheme().equalsIgnoreCase("https"))); return tags; } + private static Map getMetadata(ConsulDiscoveryProperties properties) { + LinkedHashMap metadata = new LinkedHashMap<>(); + if (!CollectionUtils.isEmpty(properties.getMetadata())) { + metadata.putAll(properties.getMetadata()); + } + + if (!properties.isTagsAsMetadata()) { + // add metadata from other properties. See createTags above. + if (!StringUtils.isEmpty(properties.getInstanceZone())) { + metadata.put(properties.getDefaultZoneMetadataName(), + properties.getInstanceZone()); + } + if (!StringUtils.isEmpty(properties.getInstanceGroup())) { + metadata.put("group", properties.getInstanceGroup()); + } + + // store the secure flag in the tags so that clients will be able to figure + // out whether to use http or https automatically + metadata.put("secure", + Boolean.toString(properties.getScheme().equalsIgnoreCase("https"))); + } + + return metadata; + } + public static NewService.Check createCheck(Integer port, HeartbeatProperties ttlConfig, ConsulDiscoveryProperties properties) { NewService.Check check = new NewService.Check(); diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistration.java index 4e1bb2a2..954c6340 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistration.java @@ -78,7 +78,10 @@ public class ConsulRegistration implements Registration { @Override public Map getMetadata() { - return ConsulServerUtils.getMetadata(getService().getTags()); + if (properties.isTagsAsMetadata()) { + return ConsulServerUtils.getMetadata(getService().getTags()); + } + return getService().getMeta(); } } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClientTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClientTests.java index c455c7d6..b10f4ec0 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClientTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/reactive/ConsulReactiveDiscoveryClientTests.java @@ -41,6 +41,7 @@ import static java.util.Collections.singletonMap; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -172,7 +173,7 @@ class ConsulReactiveDiscoveryClientTests { when(healthService.getService()).thenReturn(service); when(service.getAddress()).thenReturn("localhost"); when(service.getPort()).thenReturn(443); - when(service.getTags()).thenReturn(singletonList("secure=true")); + lenient().when(service.getTags()).thenReturn(singletonList("secure=true")); return new Response<>(singletonList(healthService), 0L, true, System.currentTimeMillis()); From 4aab57f59dff1d7d65001da6ff41080816e16e00 Mon Sep 17 00:00:00 2001 From: Bradley Barrett Date: Sun, 18 Aug 2019 20:22:01 -0400 Subject: [PATCH 2/3] Add metadata and enableTagOverride properties for service registration --- .../discovery/ConsulDiscoveryProperties.java | 19 ++++++++++++++++++- ...rviceRegistrationCustomizedPropsTests.java | 11 ++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index 2889dbfc..1dacbaae 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -257,6 +257,22 @@ public class ConsulDiscoveryProperties { this.tags = tags; } + public boolean isEnableTagOverride() { + return enableTagOverride; + } + + public void setEnableTagOverride(boolean enableTagOverride) { + this.enableTagOverride = enableTagOverride; + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + public boolean isEnabled() { return this.enabled; } @@ -584,7 +600,8 @@ public class ConsulDiscoveryProperties { public String toString() { return new ToStringCreator(this).append("hostInfo", this.hostInfo) .append("aclToken", this.aclToken).append("tags", this.tags) - .append("enabled", this.enabled) + .append("enableTagOverride", this.enableTagOverride) + .append("metadata", this.metadata).append("enabled", this.enabled) .append("managementTags", this.managementTags) .append("healthCheckPath", this.healthCheckPath) .append("healthCheckUrl", this.healthCheckUrl) diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java index c6a70d9d..bc980d08 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java @@ -54,7 +54,10 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen "spring.cloud.consul.discovery.hostname=myhost", "spring.cloud.consul.discovery.ipAddress=10.0.0.1", "spring.cloud.consul.discovery.registerHealthCheck=false", - "spring.cloud.consul.discovery.failFast=false" }, + "spring.cloud.consul.discovery.failFast=false", + "spring.cloud.consul.discovery.enableTagOverride=true", + "spring.cloud.consul.discovery.metadata.key1=value1", + "spring.cloud.consul.discovery.metadata.key2=value2" }, webEnvironment = RANDOM_PORT) public class ConsulAutoServiceRegistrationCustomizedPropsTests { @@ -82,6 +85,12 @@ public class ConsulAutoServiceRegistrationCustomizedPropsTests { .isEqualTo(this.properties.getIpAddress()); assertThat("myhost").as("service address was wrong") .isEqualTo(service.getAddress()); + assertThat(service.getEnableTagOverride()) + .as("property enableTagOverride was wrong").isTrue(); + assertThat(service.getMeta()).as("property metadata contains the wrong keys") + .containsOnlyKeys("key1", "key2"); + assertThat(service.getMeta()).as("property metadata contains the wrong values") + .containsEntry("key1", "value1").containsEntry("key2", "value2"); Response> checkResponse = this.consul.getHealthChecksForService( "myTestService-B", HealthChecksForServiceRequest.newBuilder() From f711bda1f8e110caa43ea77d4dad9d3cdc44fe49 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Thu, 9 Apr 2020 12:35:25 -0400 Subject: [PATCH 3/3] Polishes consul metadata support --- .../main/asciidoc/spring-cloud-consul.adoc | 26 ++++ .../discovery/ConsulDiscoveryProperties.java | 14 +- ...ationCustomizedPropsRealMetadataTests.java | 129 ++++++++++++++++++ ...rviceRegistrationCustomizedPropsTests.java | 20 ++- 4 files changed, 173 insertions(+), 16 deletions(-) create mode 100644 spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests.java diff --git a/docs/src/main/asciidoc/spring-cloud-consul.adoc b/docs/src/main/asciidoc/spring-cloud-consul.adoc index 9d417dce..b7fd53a8 100644 --- a/docs/src/main/asciidoc/spring-cloud-consul.adoc +++ b/docs/src/main/asciidoc/spring-cloud-consul.adoc @@ -180,6 +180,32 @@ spring: The above configuration will result in a map with `foo->bar` and `baz->baz`. +===== Generated Metadata + +The Consul Auto Registration will generate a few entries automatically. + +.Auto Generated Metadata +|=== +| Key | Value + +| 'group' +| Property `spring.cloud.consul.discovery.instance-group`. This values is only generated if `instance-group` is not empty.' + +| 'secure' +| True if property `spring.cloud.consul.discovery.scheme` equals 'https', otherwise false. + +| Property `spring.cloud.consul.discovery.default-zone-metadata-name`, defaults to 'zone' +| Property `spring.cloud.consul.discovery.instance-zone`. This values is only generated if `instance-zone` is not empty.' + +|=== + + +===== Official Consul Metadata + +Consul added official support for a `meta` field that is a `Map`. Spring Cloud Consul has added `spring.cloud.consul.discovery.metadata` and `spring.cloud.consul.discovery.management-metadata` properties to support it. + +NOTE: By default, the `ServiceInstance.getMetadata()` method from Spring Cloud Commons will continue to populated by parsing the `spring.cloud.consul.discovery.tags` property for backwards compatibility. To change this behaviour set `spring.cloud.consul.discovery.tags-as-metadata=false` and the metadata will be populated from `spring.cloud.consul.discovery.metadata`. In a future version, parsing the `tags` property will be removed. + ==== Making the Consul Instance ID Unique By default a consul instance is registered with an ID that is equal to its Spring Application Context ID. By default, the Spring Application Context ID is `${spring.application.name}:comma,separated,profiles:${server.port}`. For most cases, this will allow multiple instances of one service to run on one machine. If further uniqueness is required, Using Spring Cloud you can override this by providing a unique identifier in `spring.cloud.consul.discovery.instanceId`. For example: diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index 1dacbaae..c64a8b51 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -554,14 +554,6 @@ public class ConsulDiscoveryProperties { this.order = order; } - public Map getMetadata() { - return this.metadata; - } - - public void setMetadata(Map metadata) { - this.metadata = metadata; - } - @Deprecated public boolean isTagsAsMetadata() { return this.tagsAsMetadata; @@ -600,8 +592,9 @@ public class ConsulDiscoveryProperties { public String toString() { return new ToStringCreator(this).append("hostInfo", this.hostInfo) .append("aclToken", this.aclToken).append("tags", this.tags) + .append("enabled", this.enabled) .append("enableTagOverride", this.enableTagOverride) - .append("metadata", this.metadata).append("enabled", this.enabled) + .append("metadata", this.metadata) .append("managementTags", this.managementTags) .append("healthCheckPath", this.healthCheckPath) .append("healthCheckUrl", this.healthCheckUrl) @@ -633,8 +626,7 @@ public class ConsulDiscoveryProperties { .append("registerHealthCheck", this.registerHealthCheck) .append("failFast", this.failFast) .append("healthCheckTlsSkipVerify", this.healthCheckTlsSkipVerify) - .append("order", this.order).append("metadata", this.metadata) - .append("tagsAsMetadata", this.tagsAsMetadata) + .append("order", this.order).append("tagsAsMetadata", this.tagsAsMetadata) .append("enableTagOverride", this.enableTagOverride) .append("managementEnableTagOverride", this.managementEnableTagOverride) .append("managementMetadata", this.managementMetadata).toString(); diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests.java new file mode 100644 index 00000000..f11015ca --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests.java @@ -0,0 +1,129 @@ +/* + * Copyright 2013-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.consul.serviceregistry; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.QueryParams; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.agent.model.Service; +import com.ecwid.consul.v1.health.HealthChecksForServiceRequest; +import com.ecwid.consul.v1.health.model.Check; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration; +import org.springframework.cloud.consul.ConsulAutoConfiguration; +import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Spencer Gibb + * @author Venil Noronha + */ +@RunWith(SpringRunner.class) +@SpringBootTest( + classes = ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests.TestPropsConfig.class, + properties = { "spring.application.name=myTestServiceRealMetadata-B", + "spring.cloud.consul.discovery.instanceId=myTestServiceRealMetadata1-B", + "spring.cloud.consul.discovery.port=4452", + "spring.cloud.consul.discovery.hostname=myhost", + "spring.cloud.consul.discovery.ipAddress=10.0.0.1", + "spring.cloud.consul.discovery.registerHealthCheck=false", + "spring.cloud.consul.discovery.failFast=false", + "spring.cloud.consul.discovery.default-zone-metadata-name=mydefaultzonemetadataname", + "spring.cloud.consul.discovery.instance-zone=myzone", + "spring.cloud.consul.discovery.instance-group=mygroup", + "spring.cloud.consul.discovery.tags[0]=mytag", + "spring.cloud.consul.discovery.enableTagOverride=true", + "spring.cloud.consul.discovery.tags-as-metadata=false", + "spring.cloud.consul.discovery.metadata.key1=value1", + "spring.cloud.consul.discovery.metadata.key2=value2" }, + webEnvironment = RANDOM_PORT) +public class ConsulAutoServiceRegistrationCustomizedPropsRealMetadataTests { + + @Autowired + private ConsulClient consul; + + @Autowired + private ConsulDiscoveryProperties properties; + + @Test + public void propertiesAreCorrect() { + Response> response = this.consul.getAgentServices(); + Map services = response.getValue(); + Service service = services.get("myTestServiceRealMetadata1-B"); + assertThat(service).as("service was null").isNotNull(); + assertThat(service.getPort()).as("service port is discovery port") + .isEqualTo(4452); + assertThat("myTestServiceRealMetadata1-B").as("service id was wrong") + .isEqualTo(service.getId()); + assertThat("myTestServiceRealMetadata-B").as("service name was wrong") + .isEqualTo(service.getService()); + assertThat("myhost").as("property hostname was wrong") + .isEqualTo(this.properties.getHostname()); + assertThat("10.0.0.1").as("property ipAddress was wrong") + .isEqualTo(this.properties.getIpAddress()); + assertThat("myhost").as("service address was wrong") + .isEqualTo(service.getAddress()); + assertThat(service.getEnableTagOverride()) + .as("property enableTagOverride was wrong").isTrue(); + assertThat(service.getTags()).as("property tags contains the wrong values") + .containsExactly("mytag"); + HashMap entries = new HashMap<>(); + entries.put("key1", "value1"); + entries.put("key2", "value2"); + entries.put("mydefaultzonemetadataname", "myzone"); + entries.put("group", "mygroup"); + entries.put("secure", "false"); + assertThat(service.getMeta()).as("property metadata contains the wrong entries") + .containsExactlyInAnyOrderEntriesOf(entries); + + Response> checkResponse = this.consul.getHealthChecksForService( + "myTestServiceRealMetadata-B", HealthChecksForServiceRequest.newBuilder() + .setQueryParams(QueryParams.DEFAULT).build()); + List checks = checkResponse.getValue(); + assertThat(checks).as("checks was wrong size").hasSize(0); + } + + @Test + public void testFailFastDisabled() { + assertThat(this.properties.isFailFast()).as("property failFast was wrong") + .isFalse(); + } + + @Configuration(proxyBeanMethods = false) + @EnableAutoConfiguration + @ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, + ConsulAutoConfiguration.class, + ConsulAutoServiceRegistrationAutoConfiguration.class }) + public static class TestPropsConfig { + + } + +} diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java index bc980d08..5db7f11c 100644 --- a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedPropsTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.consul.serviceregistry; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -55,10 +56,15 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen "spring.cloud.consul.discovery.ipAddress=10.0.0.1", "spring.cloud.consul.discovery.registerHealthCheck=false", "spring.cloud.consul.discovery.failFast=false", + "spring.cloud.consul.discovery.default-zone-metadata-name=mydefaultzonemetadataname", + "spring.cloud.consul.discovery.instance-zone=myzone", + "spring.cloud.consul.discovery.instance-group=mygroup", + "spring.cloud.consul.discovery.tags[0]=mytag", "spring.cloud.consul.discovery.enableTagOverride=true", "spring.cloud.consul.discovery.metadata.key1=value1", "spring.cloud.consul.discovery.metadata.key2=value2" }, webEnvironment = RANDOM_PORT) +@Deprecated public class ConsulAutoServiceRegistrationCustomizedPropsTests { @Autowired @@ -68,7 +74,7 @@ public class ConsulAutoServiceRegistrationCustomizedPropsTests { private ConsulDiscoveryProperties properties; @Test - public void contextLoads() { + public void propertiesAreCorrect() { Response> response = this.consul.getAgentServices(); Map services = response.getValue(); Service service = services.get("myTestService1-B"); @@ -87,10 +93,14 @@ public class ConsulAutoServiceRegistrationCustomizedPropsTests { .isEqualTo(service.getAddress()); assertThat(service.getEnableTagOverride()) .as("property enableTagOverride was wrong").isTrue(); - assertThat(service.getMeta()).as("property metadata contains the wrong keys") - .containsOnlyKeys("key1", "key2"); - assertThat(service.getMeta()).as("property metadata contains the wrong values") - .containsEntry("key1", "value1").containsEntry("key2", "value2"); + assertThat(service.getTags()).as("property tags contains the wrong values") + .containsExactly("mytag", "mydefaultzonemetadataname=myzone", + "group=mygroup", "secure=false"); + HashMap entries = new HashMap<>(); + entries.put("key1", "value1"); + entries.put("key2", "value2"); + assertThat(service.getMeta()).as("property metadata contains the wrong entries") + .containsExactlyInAnyOrderEntriesOf(entries); Response> checkResponse = this.consul.getHealthChecksForService( "myTestService-B", HealthChecksForServiceRequest.newBuilder()