-
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 1 commit
aa8318e
2996c2f
60896a3
8d10450
2c95bd2
802dd8f
81993a4
0558fc6
20eaeae
dd39bfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Allows service discovery systems to register more than one instance. Automatic registration will still take place. fixes gh-9
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| 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.Registration; | ||
| import org.springframework.cloud.client.serviceregistry.ServiceRegistry; | ||
| import org.springframework.context.ApplicationContext; | ||
| import org.springframework.context.ApplicationContextAware; | ||
| import org.springframework.context.ApplicationListener; | ||
|
|
@@ -35,7 +37,7 @@ | |
| * Lifecycle methods that may be useful and common to various DiscoveryClient implementations. | ||
|
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. is it still various? or is registration (the
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.
|
||
| * @author Spencer Gibb | ||
| */ | ||
| public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, | ||
| public abstract class AbstractDiscoveryLifecycle<R extends Registration> implements DiscoveryLifecycle, | ||
| ApplicationContextAware, ApplicationListener<EmbeddedServletContainerInitializedEvent> { | ||
|
|
||
| private static final Log logger = LogFactory.getLog(AbstractDiscoveryLifecycle.class); | ||
|
|
@@ -52,6 +54,12 @@ public abstract class AbstractDiscoveryLifecycle implements DiscoveryLifecycle, | |
|
|
||
| private AtomicInteger port = new AtomicInteger(0); | ||
|
|
||
| private ServiceRegistry<R> serviceRegistry; | ||
|
|
||
| protected AbstractDiscoveryLifecycle(ServiceRegistry<R> serviceRegistry) { | ||
| this.serviceRegistry = serviceRegistry; | ||
| } | ||
|
|
||
| protected ApplicationContext getContext() { | ||
| return context; | ||
| } | ||
|
|
@@ -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); | ||
| } | ||
|
|
@@ -122,28 +130,44 @@ protected boolean shouldRegisterManagement() { | |
| /** | ||
| * @return the object used to configure the DiscoveryClient | ||
|
||
| */ | ||
| protected abstract Object getConfiguration(); | ||
| protected Object getConfiguration() { | ||
| return null; | ||
| } | ||
|
|
||
| protected abstract R getRegistration(); | ||
|
|
||
| protected abstract R getManagementRegistration(); | ||
|
|
||
| protected ServiceRegistry<R> getServiceRegistry() { | ||
| return this.serviceRegistry; | ||
| } | ||
|
|
||
| /** | ||
| * 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, |
||
| */ | ||
| protected abstract void register(); | ||
| protected void register() { | ||
| this.serviceRegistry.register(getRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * Register the local management service with the DiscoveryClient | ||
| */ | ||
| protected void registerManagement() { | ||
| this.serviceRegistry.register(getManagementRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * De-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. ditto |
||
| */ | ||
| protected abstract void deregister(); | ||
| protected void deregister() { | ||
| this.serviceRegistry.deregister(getRegistration()); | ||
| } | ||
|
|
||
| /** | ||
| * De-register the local management 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. ditto |
||
| */ | ||
| protected void deregisterManagement() { | ||
| this.serviceRegistry.deregister(getManagementRegistration()); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| 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,10 @@ | ||
| package org.springframework.cloud.client.serviceregistry; | ||
|
|
||
| /** | ||
| * @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); | ||
| } | ||
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.
I don't like how this is mixed up here. How about I move
*Lifecycleto it's own package?Uh oh!
There was an error while loading. Please reload this page.
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.
In fact,
DiscoveryLifecycledoesn't even make sense anymore. MaybeAutoRegistration? It's not a function of discovery at all.