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
This commit is contained in:
committed by
Spencer Gibb
parent
c32f21556b
commit
7ccdd3568d
@@ -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<ZookeeperInstance> serviceInstance) {
|
||||
boolean secure = serviceInstance.getSslPort() != null;
|
||||
Integer port = serviceInstance.getPort();
|
||||
if (secure) {
|
||||
port = serviceInstance.getSslPort();
|
||||
}
|
||||
Map<String, String> 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
|
||||
|
||||
@@ -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<String, String> 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<ZookeeperInstance> 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<String, String> getMetadata() {
|
||||
return this.metadata;
|
||||
}
|
||||
}
|
||||
@@ -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:
|
||||
|
||||
@@ -5,4 +5,5 @@ spring:
|
||||
zookeeper:
|
||||
discovery:
|
||||
metadata:
|
||||
testMetadataKey: testMetadataValue
|
||||
testMetadataKey: testMetadataValue
|
||||
uriSpec: "{scheme}://{address}:{port}/contextPath"
|
||||
Reference in New Issue
Block a user