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()