0

我正在 lagom 项目中创建一个模块。我的模块只有 kafka 消费者用于在 cassandra 中消费消息和存储消息事件。谢谢为什么,在我LagomApplicationLoader没有定义任何服务并lagomServerLagomServer.forServices(). 但是每当我开始我的应用程序时,我都会遇到以下异常:

java.lang.IllegalArgumentException
    at com.lightbend.lagom.scaladsl.server.LagomServer$$anon$2.<init>(LagomServer.scala:35)
    at com.lightbend.lagom.scaladsl.server.LagomServer$.forServices(LagomServer.scala:31)
    at com.knoldus.consumer.impl.TwitterConsumerApplication.lagomServer$lzycompute(TwitterConsumerLoader.scala:33)
    at com.knoldus.consumer.impl.TwitterConsumerApplication.lagomServer(TwitterConsumerLoader.scala:33)
    at com.lightbend.lagom.scaladsl.server.LagomApplication.<init>(LagomApplicationLoader.scala:187)
    at com.knoldus.consumer.impl.TwitterConsumerApplication.<init>(TwitterConsumerLoader.scala:28)
    at com.knoldus.consumer.impl.TwitterConsumerLoader$$anon$1.<init>(TwitterConsumerLoader.scala:25)
    at com.knoldus.consumer.impl.TwitterConsumerLoader.loadDevMode(TwitterConsumerLoader.scala:25)
    at com.lightbend.lagom.scaladsl.server.LagomApplicationLoader.load(LagomApplicationLoader.scala:54)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1$$anonfun$2$$anonfun$3.apply(LagomReloadableDevServerStart.scala:151)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1$$anonfun$2$$anonfun$3.apply(LagomReloadableDevServerStart.scala:148)
    at play.utils.Threads$.withContextClassLoader(Threads.scala:21)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1$$anonfun$2.apply(LagomReloadableDevServerStart.scala:148)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1$$anonfun$2.apply(LagomReloadableDevServerStart.scala:124)
    at scala.Option.map(Option.scala:146)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1.apply(LagomReloadableDevServerStart.scala:124)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1$$anonfun$apply$1.apply(LagomReloadableDevServerStart.scala:122)
    at scala.util.Success.flatMap(Try.scala:231)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1.apply(LagomReloadableDevServerStart.scala:122)
    at play.core.server.LagomReloadableDevServerStart$$anonfun$mainDev$1$$anon$2$$anonfun$get$1.apply(LagomReloadableDevServerStart.scala:114)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
    at scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
    at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

我的代码:

class TwitterConsumerLoader extends LagomApplicationLoader {

  override def load(context: LagomApplicationContext): LagomApplication =
    new TwitterConsumerApplication(context) {
      override def serviceLocator = NoServiceLocator
    }

  override def loadDevMode(context: LagomApplicationContext): LagomApplication =
    new TwitterConsumerApplication(context) with LagomDevModeComponents
}

abstract class TwitterConsumerApplication(context: LagomApplicationContext) extends LagomApplication(context)
  with CassandraPersistenceComponents with AhcWSComponents with LagomKafkaComponents {

  lazy val twitterService = serviceClient.implement[TwitterProducerService]

  override lazy val lagomServer = LagomServer.forServices()
  override lazy val jsonSerializerRegistry = TwitterSerializerRegistry

  persistentEntityRegistry.register(wire[TweetEntity])
  wire[TwitterProducerSubscriber]
}

如何在 lagom 中声明空服务。如果这是不可能的,还有什么其他的选择?因为可能在将来,它可能需要在这个模块中创建一些服务。我该如何解决这个问题?

4

1 回答 1

0

当您没有任何要绑定的服务时,您应该改为声明一个ServiceInfo.

另外,对于一个只从 Kafka 消费,不发布自己的主题的服务,只需要混入LagomKafkaClientComponents而不是LagomKafkaComponents. LagomKafkaClientComponents不需要一个LagomServer,只有一个ServiceInfo

您可以声明ServiceInfo一个不包含任何服务的简单消费者,如下所示:

override lazy val serviceInfo = ServiceInfo(clientName, Map.empty)

whereclientName是此项目的唯一标识字符串,用于命名 Kafka 客户端 ID 和默认使用者组 ID。您可能会"twitterConsumer"在此示例中使用。

http://www.lagomframework.com/documentation/1.3.x/scala/ServiceInfo.html有更多关于ServiceInfo.

于 2017-02-27T23:44:41.020 回答