发布于 2017-10-29 23:56:45 | 143 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Firefly 分布式游戏服务器端框架

Firefly是免费、开源、稳定、快速扩展、能 “热更新”的分布式游戏服务器端框架,采用Python编写,基于Twisted框架开发。它包括了开发框架和数据库缓存服务等各种游戏服务器基础服务,节省大量游戏开发的工作时间,真正做到让使用者把精力放在游戏玩法逻辑上。用它可以搭建自定义的分布式架构,只需要修改相应的配置文件即可。


Firefly 4.5.0增加了Kotlin DSL风格的API和Spring Reactor适配器来帮助我们更方便的构建异步应用程序。

Kotlin DSL风格的API允许我们以半声明的方式来构造应用,例如:

private val log = Log.getLogger { }    fun main(args: Array<String>) {      val server = HttpServer(requestLocal) {          router {              httpMethod = HttpMethod.GET              path = "/"                asyncHandler {                  end("hello world!")              }          }            router {              httpMethods = listOf(GET, POST)              path = "/product/:type/:id"                asyncHandler {                  statusLine {                      status = OK.code                      reason = OK.message                  }                    header {                      "My-Header" to "Ohh nice"                      SERVER to "Firefly kotlin DSL server"                      +HttpField("Add-My-Header", "test add")                  }                    trailer {                      "You-are-trailer" to "Crane ....."                  }                    val type = getRouterParameter("type")                  val id = getRouterParameter("id")                  log.info { "req type: $type, id: $id" }                    writeJson(Response("ok", 200, Product(id, type))).end()              }          }      }      server.listen("localhost", 8080)  }

在这个例子中,我们使用Kotlin DSL风格的API来设置HTTP method, path, header等,它能更清晰的表达HTTP协议的结构。

Firefly HTTP Server/Client (Kotlin版本)在协程(Coroutine)中执行业务逻辑,这意味着我们可以以同步风格的代码编写异步应用程序。编写协程同步风格的代码相比异步回调更简单,更易于阅读,并且不会影响程序的性能和伸缩性。

由于Java语言并没有原生协程(Coroutine),在未来的版本中我们考虑增加Quasar(一个JVM第三方协程库)适配器来使得Java版本的HTTP Server/Client运行在协程中。

为了让Java版本的框架更容易编写异步应用程序,增加了Spring Reactor适配器来帮助我们更流畅的构建异步应用。例如:

@Override  public Mono<Boolean> buy(ProductBuyRequest request) {      if (request == null) {          return Mono.error(new IllegalArgumentException("The product request is required"));      }        if (request.getUserId() == null || request.getUserId().equals(0L)) {          return Mono.error(new IllegalArgumentException("The user id is required"));      }        if (CollectionUtils.isEmpty(request.getProducts())) {          return Mono.error(new IllegalArgumentException("The products must bu not empty"));      }        return db.newTransaction(c -> buy(request, c));  }    private Mono<Boolean> buy(ProductBuyRequest request, ReactiveSQLConnection c) {      return inventoryDAO.updateBatch(request.getProducts(), InventoryOperator.SUB, c)                         .doOnSuccess(this::verifyInventory)                         .flatMap(arr -> productDAO.list(toProductIdList(request), c))                         .map(products -> toOrders(request, products))                         .flatMap(orders -> orderDAO.insertBatch(orders, c))                         .map(orderIdList -> true);  }

更多的例子请参考:

更新日志:

  1. 增加Kotlin DSL风格的API

  2. 增加异步SQL客户端

  3. 增加Reactive stream适配器

  4. 增加Coroutine dispatcher

  5. HTTP客户端增自动识别gzip编码

  6. 增加slf4j MDC实现

  7. 增加日志formatter

  8. 增加AffinityThreadPool



历史版本 :
Firefly 4.7.0 正式版发布,增加新的 WebSocket APIs
Firefly 4.6.0 正式版发布,新增OpenSSL引擎支持
Firefly 4.5.0 正式版发布
Firefly 4.0.21 发布,Java 的 Web 框架
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务