Lagom 开发模式 serviceRegistry
Dev模式 lagom application 继承的是LagomDevModeServiceLocatorComponents,
该类初始化了一个ServiceRegistry,代码如下
val staticServiceLocator = new ServiceLocator { override def doWithService[T](name: String, serviceCall: Call[_, _])(block: (URI) => Future[T])(implicit ec: ExecutionContext): Future[Option[T]] = { if (name == ServiceRegistry.ServiceName) { block(devModeServiceLocatorUrl).map(Some.apply) } else { Future.successful(None) } } override def locate(name: String, serviceCall: Call[_, _]): Future[Option[URI]] = { if (name == ServiceRegistry.ServiceName) { Future.successful(Some(devModeServiceLocatorUrl)) } else { Future.successful(None) } } } val serviceClient = new ScaladslServiceClient(wsClient, scaladslWebSocketClient, serviceInfo, staticServiceLocator, new ScaladslServiceResolver(new DefaultExceptionSerializer(environment)), None)(executionContext, materializer) serviceClient.implement[ServiceRegistry] }
如上,其中devModeServiceLocatorUrl表示ServiceLocator的地址,可以在application.conf通过lagom.service-locator.url来设置。staticServiceLocator是ServiceLocator的实现,他有两个用处,一个是用来定位serviceCall 的url,另一个是用于实现熔断机制。ScaladslServiceClient继承自ServiceClient,其作用是用于创建service call,具体的创建工作交给了ServiceClientContext完成。
serviceClient.implement[ServiceRegistry]创建了一个ServiceRegistry的微服务客户端,微服务地址由devModeServiceLocatorUrl给出。