Update to use new Registration interface.
See https://github.com/spring-cloud/spring-cloud-commons/issues/265
This commit is contained in:
@@ -17,6 +17,9 @@
|
||||
package org.springframework.cloud.zookeeper.discovery;
|
||||
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
@@ -110,6 +113,29 @@ public class ZookeeperServiceDiscovery implements ZookeeperRegistration, Applica
|
||||
return this.appName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
return getServiceInstance().getAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
return getServiceInstance().getSslPort() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
return URI.create(getServiceInstance().buildUriSpec());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
if (getServiceInstance().getPayload() != null) {
|
||||
return getServiceInstance().getPayload().getMetadata();
|
||||
}
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds Service Instance - needs to be used when you want to register your application
|
||||
* in Zookeeper
|
||||
|
||||
@@ -22,6 +22,10 @@ import org.apache.curator.x.discovery.ServiceType;
|
||||
import org.apache.curator.x.discovery.UriSpec;
|
||||
import org.springframework.cloud.zookeeper.discovery.ZookeeperInstance;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.cloud.zookeeper.discovery.ZookeeperDiscoveryProperties.DEFAULT_URI_SPEC;
|
||||
|
||||
/**
|
||||
@@ -149,4 +153,36 @@ public class ServiceInstanceRegistration implements ZookeeperRegistration {
|
||||
this.builder.port(port);
|
||||
this.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHost() {
|
||||
if (this.serviceInstance == null) {
|
||||
return null;
|
||||
}
|
||||
return this.serviceInstance.getAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSecure() {
|
||||
if (this.serviceInstance == null) {
|
||||
return false;
|
||||
}
|
||||
return this.serviceInstance.getSslPort() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URI getUri() {
|
||||
if (this.serviceInstance == null) {
|
||||
return null;
|
||||
}
|
||||
return URI.create(this.serviceInstance.buildUriSpec());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getMetadata() {
|
||||
if (this.serviceInstance == null || this.serviceInstance.getPayload() == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return this.serviceInstance.getPayload().getMetadata();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,5 @@ public interface ZookeeperRegistration extends Registration {
|
||||
|
||||
ServiceInstance<ZookeeperInstance> getServiceInstance();
|
||||
|
||||
int getPort();
|
||||
|
||||
void setPort(int port);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -60,6 +61,9 @@ public class SampleZookeeperApplication {
|
||||
@Autowired
|
||||
private AppClient appClient;
|
||||
|
||||
@Autowired(required = false)
|
||||
private Registration registration;
|
||||
|
||||
@RequestMapping("/")
|
||||
public ServiceInstance lb() {
|
||||
return this.loadBalancer.choose(this.appName);
|
||||
@@ -67,7 +71,7 @@ public class SampleZookeeperApplication {
|
||||
|
||||
@RequestMapping("/hi")
|
||||
public String hi() {
|
||||
return "Hello World! from " + this.discovery.getLocalServiceInstance();
|
||||
return "Hello World! from " + this.registration;
|
||||
}
|
||||
|
||||
@RequestMapping("/self")
|
||||
|
||||
Reference in New Issue
Block a user