简体   繁体   English

如何仅使用grpc服务实现创建lagom服务器?

[英]How to create a lagom server with grpc service implementation only?

I am trying to create a grpc service using Lagom framework following this documentation .我正在尝试按照本文档使用 Lagom 框架创建 grpc 服务。 Here the hello service also exposes rest API besides grpc service.这里hello服务除了 grpc 服务外还暴露了 rest API。

Now while creating lagom server in the ApplicationLoader we assign grpc service impl as additionalRouter like so:现在,在 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])

}

Its all fine for the purpose of demo but we may not need to always create a REST endpoint besides gRPC endpoint.出于演示的目的,一切都很好,但除了 gRPC 端点之外,我们可能不需要总是创建 REST 端点。 In that case I won't need either HelloService or HelloServiceImpl .在那种情况下,我不需要HelloServiceHelloServiceImpl The problem is how would you create lagom server with only HelloGrpcServiceImpl ?问题是如何仅使用HelloGrpcServiceImpl创建 lagom 服务器? I can't see to find a way either any documentation or the APIs itself to be able to achieve this!我无法找到任何文档或 API 本身来实现这一目标的方法!

Please suggest.请建议。

Based on the answer in the link I provided as a comment to my question, the solution would look something like this:根据我作为问题评论提供的链接中的答案,解决方案如下所示:

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])
}

Here we have to create dummy service trait and implementation, namely, PrimeGeneratorService and PrimeGeneratorServiceImpl , respectively.在这里,我们必须创建虚拟服务特征和实现,即分别为PrimeGeneratorServicePrimeGeneratorServiceImpl

Don't forget to add following in the loader class:不要忘记在加载器类中添加以下内容:

override def describeService = Some(readDescriptor[PrimeGeneratorService])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM