Firefly FrameworkJava異步Web框架
什么是Firefly?
Firefly是一個(gè)Java異步Web框架,它能幫助您方便和快速的創(chuàng)建web應(yīng)用。其主要功能包括:異步HTTP服務(wù)器/客戶端,異步TCP服務(wù)器/客戶端,數(shù)據(jù)庫(kù)訪問(wèn),IOC框架等。部署Firefly不需要任何額外的web容器。Firefly使用高度可伸縮的SEDA架構(gòu)能充分發(fā)揮硬件的性能。
事件驅(qū)動(dòng)
傳統(tǒng)的阻塞模型會(huì)消耗大量的線程,從而導(dǎo)致占用的大量?jī)?nèi)存和上下文切換開(kāi)銷。Firefly的API使用事件驅(qū)動(dòng)模型,用很少的線程去處理很高的并發(fā)請(qǐng)求。
函數(shù)編程
Firefly提供了函數(shù)風(fēng)格和鏈?zhǔn)秸{(diào)用API來(lái)編寫網(wǎng)絡(luò)應(yīng)用程序,它可以讓您使用極簡(jiǎn)主義的代碼,流暢的開(kāi)發(fā)網(wǎng)絡(luò)應(yīng)用程序。例如:
public class HelloHTTPServerAndClient {
public static void main(String[] args) {
Phaser phaser = new Phaser(2);
HTTP2ServerBuilder httpServer = $.httpServer();
httpServer.router().get("/").handler(ctx -> ctx.write("hello world! ").next())
.router().get("/").handler(ctx -> ctx.end("end message"))
.listen("localhost", 8080);
$.httpClient().get("http://localhost:8080/").submit()
.thenAccept(res -> System.out.println(res.getStringBody()))
.thenAccept(res -> phaser.arrive());
phaser.arriveAndAwaitAdvance();
httpServer.stop();
$.httpClient().stop();
}
}
Kotlin支持
Firefly同樣提供了Kotlin DSL風(fēng)格的API,Kotlin DSL以半聲明的方式構(gòu)造程序,能清晰的表達(dá)程序的結(jié)構(gòu)和意圖。例如:
fun main(args: Array) {
HttpServer {
router {
httpMethod = HttpMethod.GET
path = "/"
asyncHandler {
end("hello world!")
}
}
}.listen("localhost", 8080)
}
fun main(args: Array): Unit = runBlocking {
val msg = firefly.httpClient().get("http://localhost:8080").asyncSubmit().stringBody
println(msg)
}
Firefly Kotlin HTTP 服務(wù)器和客戶端使用協(xié)程(coroutine)消除回調(diào)風(fēng)格的代碼,能讓程序變得更簡(jiǎn)單清晰,并保留了異步IO的性能與伸縮性。
更多詳細(xì)的用例可以在Firefly的文檔中找到。
評(píng)論
圖片
表情
