首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Akka gRPC + Slick应用程序导致"IllegalStateException:无法初始化ExecutionContext;AsyncExecutor已关闭“

Akka gRPC + Slick应用程序导致"IllegalStateException:无法初始化ExecutionContext;AsyncExecutor已关闭“
EN

Stack Overflow用户
提问于 2020-08-06 01:34:29
回答 1查看 196关注 0票数 0

我尝试用Akka-gRPCSlick开发gRPC服务器。我还将Airframe用于DI。

源代码是here

问题在于,如果它在作为gRPC服务器执行时收到请求,则会导致失败。如果它不是作为gRPC服务器启动,而只是从数据库中读取资源,则该过程成功。

有什么关系?

接下来,它使用slick从数据库中读取对象。...Component是机身对象。它将由主模块使用。

代码语言:javascript
复制
trait UserRepository {
  def getUser: Future[Seq[Tables.UsersRow]]
}

class UserRepositoryImpl(val profile: JdbcProfile, val db: JdbcProfile#Backend#Database) extends UserRepository {
  import profile.api._
  def getUser: Future[Seq[Tables.UsersRow]] = db.run(Tables.Users.result)
}

trait UserResolveService {
  private val repository = bind[UserRepository]
  def getAll: Future[Seq[Tables.UsersRow]] =
    repository.getUser
}


object userServiceComponent {
  val design = newDesign
    .bind[UserResolveService]
    .toSingleton
}

下面是gRPC服务器的源代码。

代码语言:javascript
复制
trait UserServiceImpl extends UserService {

  private val userResolveService            = bind[UserResolveService]
  private val system: ActorSystem           = bind[ActorSystem]
  implicit val ec: ExecutionContextExecutor = system.dispatcher

  override def getAll(in: GetUserListRequest): Future[GetUserListResponse] = {

    userResolveService.getAll.map(us =>
      GetUserListResponse(
        us.map(u =>
          myapp.proto.user.User(
            1,
            "t_horikoshi@example.com",
            "t_horikoshi",
            myapp.proto.user.User.UserRole.Admin
          )
        )
      )
    )
  }

}

trait GRPCServer {

  private val userServiceImpl      = bind[UserServiceImpl]
  implicit val system: ActorSystem = bind[ActorSystem]

  def run(): Future[Http.ServerBinding] = {

    implicit def ec: ExecutionContext = system.dispatcher
    val service: PartialFunction[HttpRequest, Future[HttpResponse]] =
      UserServiceHandler.partial(userServiceImpl)

    val reflection: PartialFunction[HttpRequest, Future[HttpResponse]] =
      ServerReflection.partial(List(UserService))

    // Akka HTTP 10.1 requires adapters to accept the new actors APIs
    val bound = Http().bindAndHandleAsync(
      ServiceHandler.concatOrNotFound(service, reflection),
      interface = "127.0.0.1",
      port = 8080,
      settings = ServerSettings(system)
    )

    bound.onComplete {
      case Success(binding) =>
        system.log.info(
          s"gRPC Server online at http://${binding.localAddress.getHostName}:${binding.localAddress.getPort}/"
        )
      case Failure(ex) =>
        system.log.error(ex, "occurred error")
    }

    bound
  }
}

object grpcComponent {
  val design = newDesign
    .bind[UserServiceImpl]
    .toSingleton
    .bind[GRPCServer]
    .toSingleton
}

下面是主模块。

代码语言:javascript
复制
object Main extends App {

  val conf = ConfigFactory
    .parseString("akka.http.server.preview.enable-http2 = on")
    .withFallback(ConfigFactory.defaultApplication())
  val system = ActorSystem("GRPCServer", conf)

  val dbConfig: DatabaseConfig[JdbcProfile] =
    DatabaseConfig.forConfig[JdbcProfile](path = "mydb")

  val design = newDesign
    .bind[JdbcProfile]
    .toInstance(dbConfig.profile)
    .bind[JdbcProfile#Backend#Database]
    .toInstance(dbConfig.db)
    .bind[UserRepository]
    .to[UserRepositoryImpl]
    .bind[ActorSystem]
    .toInstance(system)
    .add(userServiceComponent.design)
    .add(grpcComponent.design)

  design.withSession(s =>
    // Await.result(s.build[UserResolveService].getUser, Duration.Inf)) // success
    // Await.result(s.build[UserServiceImpl].getAll(GetUserListRequest()), Duration.Inf)) // success
    s.build[GRPCServer].run() // cause IllegalStateException when reciece request.

  )
}

当直接调用UserResolveServiceUserServiceImpl时,从数据库加载对象的过程成功。

但是,当将应用程序作为gRPC服务器运行时,在收到请求时会发生错误。

虽然我想了一整天,但还是无法解决..。你能帮我解决一下吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-06 14:10:03

它解决了。如果执行异步进程,则必须使用newSession启动gRPC服务器。我修得像that一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63270580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档