From 7ccdd3568deb36e1983ac211bc47c5297995f0d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9da=20Housni=20Alaoui?= Date: Thu, 2 Mar 2017 17:52:15 +0100 Subject: [PATCH] Curator UriSpec should be used by discovery to produce service instance uri (#116) * Curator UriSpec should be used by discovery to produce service instance uri --- .../discovery/ZookeeperDiscoveryClient.java | 16 +--- .../discovery/ZookeeperServiceInstance.java | 90 +++++++++++++++++++ .../discovery/ZookeeperDiscoveryTests.java | 9 +- .../src/test/resources/application-ribbon.yml | 3 +- 4 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperServiceInstance.java diff --git a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java index ad79cc3c..051ef11e 100644 --- a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java +++ b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryClient.java @@ -19,14 +19,11 @@ package org.springframework.cloud.zookeeper.discovery; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.List; -import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.curator.x.discovery.ServiceInstance; -import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.zookeeper.discovery.dependency.ZookeeperDependencies; import org.springframework.util.ReflectionUtils; @@ -66,18 +63,7 @@ public class ZookeeperDiscoveryClient implements DiscoveryClient { } private static org.springframework.cloud.client.ServiceInstance createServiceInstance(String serviceId, ServiceInstance serviceInstance) { - boolean secure = serviceInstance.getSslPort() != null; - Integer port = serviceInstance.getPort(); - if (secure) { - port = serviceInstance.getSslPort(); - } - Map metadata; - if (serviceInstance.getPayload() != null) { - metadata = serviceInstance.getPayload().getMetadata(); - } else { - metadata = new HashMap<>(); - } - return new DefaultServiceInstance(serviceId, serviceInstance.getAddress(), port, secure, metadata); + return new ZookeeperServiceInstance(serviceId, serviceInstance); } @Override diff --git a/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperServiceInstance.java b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperServiceInstance.java new file mode 100644 index 00000000..4578f068 --- /dev/null +++ b/spring-cloud-zookeeper-discovery/src/main/java/org/springframework/cloud/zookeeper/discovery/ZookeeperServiceInstance.java @@ -0,0 +1,90 @@ +/* + * Copyright 2013-2017 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 + * + * http://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.zookeeper.discovery; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.cloud.client.ServiceInstance; + +/** + * A specific {@link ServiceInstance} describing a zookeeper service instance + * + * @author Reda.Housni-Alaoui + * @since 1.0.4 + */ +public class ZookeeperServiceInstance implements ServiceInstance { + + private final String serviceId; + private final String host; + private final int port; + private final boolean secure; + private final URI uri; + private final Map metadata; + + /** + * @param serviceId The service id to be used + * @param serviceInstance The zookeeper service instance described by this service instance + */ + public ZookeeperServiceInstance(String serviceId, org.apache.curator.x.discovery.ServiceInstance serviceInstance) { + this.serviceId = serviceId; + this.host = serviceInstance.getAddress(); + this.secure = serviceInstance.getSslPort() != null; + Integer port = serviceInstance.getPort(); + if (this.secure) { + port = serviceInstance.getSslPort(); + } + this.port = port; + this.uri = URI.create(serviceInstance.buildUriSpec()); + if (serviceInstance.getPayload() != null) { + this.metadata = serviceInstance.getPayload().getMetadata(); + } else { + this.metadata = new HashMap<>(); + } + } + + @Override + public String getServiceId() { + return this.serviceId; + } + + @Override + public String getHost() { + return this.host; + } + + @Override + public int getPort() { + return this.port; + } + + @Override + public boolean isSecure() { + return this.secure; + } + + @Override + public URI getUri() { + return this.uri; + } + + @Override + public Map getMetadata() { + return this.metadata; + } +} diff --git a/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryTests.java b/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryTests.java index 2291366d..c89a9550 100644 --- a/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryTests.java +++ b/spring-cloud-zookeeper-discovery/src/test/java/org/springframework/cloud/zookeeper/discovery/ZookeeperDiscoveryTests.java @@ -38,7 +38,7 @@ import static org.assertj.core.api.BDDAssertions.then; * @author Marcin Grzejszczak */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(ZookeeperDiscoveryTests.Config.class) +@SpringApplicationConfiguration(classes = ZookeeperDiscoveryTests.Config.class) @ActiveProfiles("ribbon") @WebIntegrationTest(randomPort = true) public class ZookeeperDiscoveryTests { @@ -70,6 +70,13 @@ public class ZookeeperDiscoveryTests { then(this.springAppName).isEqualTo(instance.getServiceId()); } + @Test public void should_service_instance_uri_match_uriSpec() { + //given: + ServiceInstance instance = this.discoveryClient.getLocalServiceInstance(); + //expect: + then(instance.getUri()).hasPath("/contextPath"); + } + @Test public void should_find_an_instance_using_feign_via_service_id() { final IdUsingFeignClient idUsingFeignClient = this.idUsingFeignClient; //expect: diff --git a/spring-cloud-zookeeper-discovery/src/test/resources/application-ribbon.yml b/spring-cloud-zookeeper-discovery/src/test/resources/application-ribbon.yml index f9297832..1b9a7f52 100644 --- a/spring-cloud-zookeeper-discovery/src/test/resources/application-ribbon.yml +++ b/spring-cloud-zookeeper-discovery/src/test/resources/application-ribbon.yml @@ -5,4 +5,5 @@ spring: zookeeper: discovery: metadata: - testMetadataKey: testMetadataValue \ No newline at end of file + testMetadataKey: testMetadataValue + uriSpec: "{scheme}://{address}:{port}/contextPath" \ No newline at end of file