-
Notifications
You must be signed in to change notification settings - Fork 720
Create a ServiceRegistry interface. #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
aa8318e
2996c2f
60896a3
8d10450
2c95bd2
802dd8f
81993a4
0558fc6
20eaeae
dd39bfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,15 +26,20 @@ | |
| import org.springframework.beans.BeansException; | ||
| import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; | ||
| import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; | ||
| import org.springframework.cloud.client.serviceregistry.ServiceRegistry; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.context.ApplicationContextAware; | ||
| import org.springframework.context.ApplicationListener; | ||
| import org.springframework.core.env.Environment; | ||
|
|
||
| /** | ||
| * Lifecycle methods that may be useful and common to various DiscoveryClient implementations. | ||
| * | ||
| * @deprecated use {@link org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration} instead. This class will be removed in the next release train. | ||
| * | ||
| * @author Spencer Gibb | ||
| */ | ||
| @Deprecated | ||
| public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, | ||
| ApplicationContextAware, ApplicationListener<EmbeddedServletContainerInitializedEvent> { | ||
|
|
||
|
|
@@ -89,6 +94,9 @@ public void stop(Runnable callback) { | |
| @Override | ||
| public void start() { | ||
| if (!isEnabled()) { | ||
| if (logger.isDebugEnabled()) { | ||
| logger.debug("Discovery Lifecycle disabled. Not starting"); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -103,7 +111,7 @@ public void start() { | |
| if (shouldRegisterManagement()) { | ||
| registerManagement(); | ||
| } | ||
| this.context .publishEvent(new InstanceRegisteredEvent<>(this, | ||
| this.context.publishEvent(new InstanceRegisteredEvent<>(this, | ||
| getConfiguration())); | ||
| this.running.compareAndSet(false, true); | ||
| } | ||
|
|
@@ -113,17 +121,18 @@ public void start() { | |
| protected abstract void setConfiguredPort(int port); | ||
|
|
||
| /** | ||
| * @return if the management service should be registered with the DiscoveryService | ||
| * @return if the management service should be registered with the {@link ServiceRegistry} | ||
| */ | ||
| protected boolean shouldRegisterManagement() { | ||
| return getManagementPort() != null && ManagementServerPortUtils.isDifferent(this.context); | ||
| } | ||
|
|
||
| /** | ||
| * @return the object used to configure the DiscoveryClient | ||
| * @return the object used to configure the registration | ||
| */ | ||
| protected abstract Object getConfiguration(); | ||
|
|
||
|
|
||
| /** | ||
| * Register the local service with the DiscoveryClient | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this doc need to be changed now?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, |
||
| */ | ||
|
|
@@ -147,7 +156,7 @@ protected void deregisterManagement() { | |
| } | ||
|
|
||
| /** | ||
| * @return if the DiscoveryClient is enabled | ||
| * @return true, if the {@link DiscoveryLifecycle} is enabled | ||
| */ | ||
| protected abstract boolean isEnabled(); | ||
|
|
||
|
|
@@ -201,6 +210,10 @@ public boolean isRunning() { | |
| return this.running.get(); | ||
| } | ||
|
|
||
| protected AtomicBoolean getRunning() { | ||
| return running; | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return this.order; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle; | ||
|
|
||
| /** | ||
| * Lifecycle methods that may be useful and common to {@link ServiceRegistry} implementations. | ||
| * | ||
| * TODO: document the lifecycle | ||
| * | ||
| * @param <R> registration type passed to the {@link ServiceRegistry}. | ||
| * | ||
| * @author Spencer Gibb | ||
| */ | ||
| @SuppressWarnings("deprecation") | ||
| public abstract class AbstractAutoServiceRegistration<R extends Registration> extends AbstractDiscoveryLifecycle implements AutoServiceRegistration { | ||
|
|
||
| private ServiceRegistry<R> serviceRegistry; | ||
|
|
||
| protected AbstractAutoServiceRegistration(ServiceRegistry<R> serviceRegistry) { | ||
| this.serviceRegistry = serviceRegistry; | ||
| } | ||
|
|
||
| protected ServiceRegistry<R> getServiceRegistry() { | ||
| return this.serviceRegistry; | ||
| } | ||
|
|
||
| protected abstract R getRegistration(); | ||
|
|
||
| protected abstract R getManagementRegistration(); | ||
|
|
||
| /** | ||
| * Register the local service with the {@link ServiceRegistry} | ||
| */ | ||
| @Override | ||
| protected void register() { | ||
| this.serviceRegistry.register(getRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * Register the local management service with the {@link ServiceRegistry} | ||
| */ | ||
| @Override | ||
| protected void registerManagement() { | ||
| this.serviceRegistry.register(getManagementRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * De-register the local service with the {@link ServiceRegistry} | ||
| */ | ||
| @Override | ||
| protected void deregister() { | ||
| this.serviceRegistry.deregister(getRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * De-register the local management service with the {@link ServiceRegistry} | ||
| */ | ||
| @Override | ||
| protected void deregisterManagement() { | ||
| this.serviceRegistry.deregister(getManagementRegistration()); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| if (this.getRunning().compareAndSet(true, false) && isEnabled()) { | ||
| deregister(); | ||
| if (shouldRegisterManagement()) { | ||
| deregisterManagement(); | ||
| } | ||
| this.serviceRegistry.close(); | ||
| } | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| /** | ||
| * @author Spencer Gibb | ||
| */ | ||
| public interface AutoServiceRegistration { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
|
|
||
| /** | ||
| * @author Spencer Gibb | ||
| */ | ||
| @Configuration | ||
| @EnableConfigurationProperties(AutoServiceRegistrationProperties.class) | ||
| public class AutoServiceRegistrationConfiguration { | ||
|
|
||
| @Autowired(required = false) | ||
| private AutoServiceRegistration autoServiceRegistration; | ||
|
|
||
| @Autowired | ||
| private AutoServiceRegistrationProperties properties; | ||
|
|
||
| @PostConstruct | ||
| protected void init() { | ||
| if (autoServiceRegistration == null && this.properties.isFailFast()) { | ||
| throw new IllegalStateException("Auto Service Registration has been requested, but there is no AutoServiceRegistration bean"); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| /** | ||
| * @author Spencer Gibb | ||
| */ | ||
| @ConfigurationProperties("spring.cloud.service-registry.auto-registration") | ||
| public class AutoServiceRegistrationProperties { | ||
|
|
||
| /** Should startup fail if there is no AutoServiceRegistration, default to false. */ | ||
| private boolean failFast = false; | ||
|
|
||
| public boolean isFailFast() { | ||
| return failFast; | ||
| } | ||
|
|
||
| public void setFailFast(boolean failFast) { | ||
| this.failFast = failFast; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| /** | ||
| * @author Spencer Gibb | ||
| */ | ||
| public interface Registration { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is a marker interface needed?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, cause looking at the test I don't really see any benefit of having this
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, leftover from a refactor where this interface had a method and I removed it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ended up putting this back in, because I needed to lookup the registration in the spring context without knowing the implementation. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| /** | ||
| * TODO: write javadoc | ||
| * @author Spencer Gibb | ||
| */ | ||
| public interface ServiceRegistry<R extends Registration> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here or somewhere else, we should qualify with an example of what a registration might be |
||
| void register(R registration); | ||
|
|
||
| void deregister(R registration); | ||
|
|
||
| void close(); | ||
|
|
||
| // TODO: return value for success? | ||
| void setStatus(R registration, String status); | ||
|
|
||
| // TODO: concrete return value? Interface? | ||
| Object getStatus(R registration); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.actuate.endpoint.Endpoint; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.cloud.client.serviceregistry.endpoint.ServiceRegistryEndpoint; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** | ||
| * @author Spencer Gibb | ||
| */ | ||
| @ConditionalOnBean(ServiceRegistry.class) | ||
| @Configuration | ||
| public class ServiceRegistryAutoConfiguration { | ||
|
|
||
| @Autowired(required = false) | ||
| private Registration registration; | ||
|
|
||
| @ConditionalOnClass(Endpoint.class) | ||
| @Bean | ||
| public ServiceRegistryEndpoint serviceRegistryEndpoint(ServiceRegistry serviceRegistry) { | ||
| ServiceRegistryEndpoint endpoint = new ServiceRegistryEndpoint(serviceRegistry); | ||
| endpoint.setRegistration(registration); | ||
| return endpoint; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it still various? or is registration (the
Rtype) intended to work only with one sort of client now? I'd probably doc the type param regardless of whether it stays a marker or not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rwill be specific to the impl (though it won't extendRegistration).