Update ConsulRegistration to use new Registration interface.

See https://github.com/spring-cloud/spring-cloud-commons/issues/265
This commit is contained in:
Spencer Gibb
2017-10-18 12:12:16 -04:00
parent 70f0cdca3e
commit 64e610adac
3 changed files with 45 additions and 7 deletions

View File

@@ -39,13 +39,11 @@ public class ConsulAutoRegistration extends ConsulRegistration {
public static final char SEPARATOR = '-';
private final ConsulDiscoveryProperties properties;
private final ApplicationContext context;
private final HeartbeatProperties heartbeatProperties;
public ConsulAutoRegistration(NewService service, ConsulDiscoveryProperties properties, ApplicationContext context, HeartbeatProperties heartbeatProperties) {
super(service);
this.properties = properties;
super(service, properties);
this.context = context;
this.heartbeatProperties = heartbeatProperties;
}
@@ -58,11 +56,11 @@ public class ConsulAutoRegistration extends ConsulRegistration {
// we might not have a port until now, so this is the earliest we
// can create a check
setCheck(getService(), this.properties, this.context, this.heartbeatProperties);
setCheck(getService(), getProperties(), this.context, this.heartbeatProperties);
}
public ConsulAutoRegistration managementRegistration() {
return managementRegistration(this.properties, this.context, this.heartbeatProperties);
return managementRegistration(getProperties(), this.context, this.heartbeatProperties);
}
public static ConsulAutoRegistration registration(ConsulDiscoveryProperties properties, ApplicationContext context,

View File

@@ -16,9 +16,15 @@
package org.springframework.cloud.consul.serviceregistry;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import com.ecwid.consul.v1.agent.model.NewService;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.discovery.ConsulServerUtils;
import java.net.URI;
import java.util.Map;
/**
* @author Spencer Gibb
@@ -26,15 +32,21 @@ import com.ecwid.consul.v1.agent.model.NewService;
public class ConsulRegistration implements Registration {
private final NewService service;
private ConsulDiscoveryProperties properties;
public ConsulRegistration(NewService service) {
public ConsulRegistration(NewService service, ConsulDiscoveryProperties properties) {
this.service = service;
this.properties = properties;
}
public NewService getService() {
return service;
}
protected ConsulDiscoveryProperties getProperties() {
return properties;
}
public String getInstanceId() {
return getService().getId();
}
@@ -43,4 +55,28 @@ public class ConsulRegistration implements Registration {
return getService().getName();
}
@Override
public String getHost() {
return getService().getAddress();
}
@Override
public int getPort() {
return getService().getPort();
}
@Override
public boolean isSecure() {
return this.properties.getScheme().equalsIgnoreCase("https");
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
}
@Override
public Map<String, String> getMetadata() {
return ConsulServerUtils.getMetadata(getService().getTags());
}
}

View File

@@ -29,6 +29,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.test.context.junit4.SpringRunner;
import com.ecwid.consul.v1.agent.model.NewService;
@@ -53,6 +54,9 @@ public class ConsulServiceRegistryTests {
@Autowired
private ConsulDiscoveryClient discoveryClient;
@Autowired
private ConsulDiscoveryProperties properties;
@LocalServerPort
private int port;
@@ -70,7 +74,7 @@ public class ConsulServiceRegistryTests {
service.setPort(port);
service.setTags(Collections.singletonList("mytag"));
ConsulRegistration registration = new ConsulRegistration(service);
ConsulRegistration registration = new ConsulRegistration(service, this.properties);
Throwable t = null;
try {
serviceRegistry.register(registration);