Skip to content
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

Refactor ServiceRegistration and RegisterWithServiceRegistry #1489

Open
TimMoore opened this issue Jul 25, 2018 · 0 comments
Open

Refactor ServiceRegistration and RegisterWithServiceRegistry #1489

TimMoore opened this issue Jul 25, 2018 · 0 comments
Labels

Comments

@TimMoore
Copy link
Contributor

Lagom's development mode has two classes (for Scala and Java services) that implement registration with the service registry:

/**
* Automatically registers the service on start, and also unregister it on stop.
*/
@Singleton
private class RegisterWithServiceRegistry @Inject() (
lifecycle: ApplicationLifecycle,
resolvedServices: ResolvedServices,
config: ServiceConfig,
registry: ServiceRegistry
)(implicit ec: ExecutionContext) {
private lazy val logger: Logger = Logger(this.getClass())
private val locatableServices = resolvedServices.services.filter(_.descriptor.locatableService)
lifecycle.addStopHook { () =>
Future.sequence(locatableServices.map { service =>
registry.unregister(service.descriptor.name).invoke().toScala
}).map(_ => ())
}
locatableServices.foreach { service =>
val c = ServiceRegistryService.of(config.url, service.descriptor.acls)
registry.register(service.descriptor.name).invoke(c).exceptionally(new JFunction[Throwable, NotUsed] {
def apply(t: Throwable) = {
logger.error(s"Service name=[${service.descriptor.name}] couldn't register itself to the service locator.", t)
NotUsed.getInstance()
}
})
}
}

class ServiceRegistration(serviceInfo: ServiceInfo, lifecycle: ApplicationLifecycle, config: Config,
registry: ServiceRegistry)(implicit ec: ExecutionContext) {
private val logger: Logger = Logger(this.getClass)
private val uri = {
val httpAddress = config.getString("play.server.http.address")
val httpPort = config.getString("play.server.http.port")
URI.create(s"http://$httpAddress:$httpPort")
}
lifecycle.addStopHook { () =>
Future.sequence(serviceInfo.locatableServices.map {
case (service, _) => registry.unregister(service).invoke()
}).map(_ => ())
}
serviceInfo.locatableServices.foreach {
case (service, acls) =>
registry.register(service)
.invoke(ServiceRegistryService(uri, acls))
.onComplete {
case Success(_) =>
logger.debug(s"Service name=[$service] successfully registered with service locator.")
case Failure(e) =>
logger.error(s"Service name=[$service}] couldn't register itself to the service locator.", e)
}
}
}

These are very similar, but have some incidental differences, and some necessary differences due to the split javadsl and scaladsl APIs. They could be refactored to use the new ServiceRegistryClient abstraction, so that the common code can live in service-registry-client-core, and the necessary differences are encapsulated within the ServiceRegistryClient implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants