[英]How to create a lagom server with grpc service implementation only?
我正在尝试按照本文档使用 Lagom 框架创建 grpc 服务。 这里hello
服务除了 grpc 服务外还暴露了 rest API。
现在,在 ApplicationLoader 中创建 lagom 服务器时,我们将 grpc 服务 impl 分配为additionalRouter
如下所示:
abstract class HelloApplication(context: LagomApplicationContext)
extends LagomApplication(context)
with AhcWSComponents {
// Bind the service that this server provides
override lazy val lagomServer =
serverFor[HelloService](wire[HelloServiceImpl])
.additionalRouter(wire[HelloGrpcServiceImpl])
}
出于演示的目的,一切都很好,但除了 gRPC 端点之外,我们可能不需要总是创建 REST 端点。 在那种情况下,我不需要HelloService
或HelloServiceImpl
。 问题是如何仅使用HelloGrpcServiceImpl
创建 lagom 服务器? 我无法找到任何文档或 API 本身来实现这一目标的方法!
请建议。
根据我作为问题评论提供的链接中的答案,解决方案如下所示:
trait PrimeGeneratorService extends Service {
override def descriptor: Descriptor = named("prime-generator")
}
class PrimeGeneratorServiceImpl() extends PrimeGeneratorService
abstract class PrimeGeneratorApplication(context: LagomApplicationContext)
extends LagomApplication(context) with AhcWSComponents {
override lazy val lagomServer: LagomServer = {
serverFor[PrimeGeneratorService](wire[PrimeGeneratorServiceImpl])
.additionalRouter(wire[PrimeGeneratorGrpcServiceImpl])
}
}
class PrimeGeneratorLoader extends LagomApplicationLoader {
override def load(context: LagomApplicationContext): LagomApplication =
new PrimeGeneratorApplication(context) {
override def serviceLocator: ServiceLocator = NoServiceLocator
}
override def loadDevMode(context: LagomApplicationContext): LagomApplication =
new PrimeGeneratorApplication(context) with LagomDevModeComponents
override def describeService = Some(readDescriptor[PrimeGeneratorService])
}
在这里,我们必须创建虚拟服务特征和实现,即分别为PrimeGeneratorService
和PrimeGeneratorServiceImpl
。
不要忘记在加载器类中添加以下内容:
override def describeService = Some(readDescriptor[PrimeGeneratorService])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.