Golang最好用的Actor框架

最好用的Actor框架

今天給大家推薦是由瑞士的團(tuán)隊(duì)Asynkron出品的Actor框架protoactor-go,Actor模型是一種適用于高并發(fā)的編程模型。

早在1973 年 Carl Hewitt 發(fā)表的論文中定義了Actor,但一直流行于Erlang 語言中。

Erlang 被愛立信公司應(yīng)用于建立高并發(fā)、可靠通信系統(tǒng),取得了巨大成功,著名的rabbitMQ 就是Erlang的代表作。

Java 語言的 Akka 庫里面角色的API 跟Scala 框架里面角色相似,后者一些語法模仿Erlang語言。

Golang中 始終缺乏一個(gè)代表性的Actor 框架,Golang自身的協(xié)程處理被廣大Golang推崇,但從生產(chǎn)實(shí)踐來看,在大規(guī)模并發(fā)的情況下,協(xié)程并不是最佳的方案,使用Actor框架編寫大規(guī)模并發(fā)服務(wù)通常被認(rèn)為是更好的選擇。

推薦理由

從目前gitHub 上的Star數(shù)量觀察, protoactor-go 是唯一個(gè)star 過千的Actor框架,由于背后有專業(yè)團(tuán)隊(duì)持續(xù)維護(hù),因此穩(wěn)定性有保證,同時(shí)protoactor-go有.Net/Golang/Java/Kotlin 的跨語言平臺(tái)的實(shí)現(xiàn),給開發(fā)者提供了更多系統(tǒng)集成的方案, 特別是在互聯(lián)網(wǎng)公司,多語言共同開發(fā)環(huán)境下,優(yōu)勢明顯。

Actor模型概述

Actor模型為并行而生,為解決高并發(fā)場景的一種編程思路。
在Actor模型中,主角是Actor,類似一種worker,Actor彼此之間直接發(fā)送消息,不需要經(jīng)過什么中介,消息是異步發(fā)送和處理的; 所謂 "一切皆是Actor",所有邏輯或者模塊均別看做Actor,通過不同Actor之間的消息傳遞實(shí)現(xiàn)模塊之間的通信和交互。
Actor模型描述了一組為了避免并發(fā)編程的常見問題的公理:

  1. 所有Actor狀態(tài)是Actor本地的,外部無法訪問。
  2. Actor必須只有通過消息傳遞進(jìn)行通信。
  3. 一個(gè)Actor可以響應(yīng)消息:推出新Actor,改變其內(nèi)部狀態(tài),或?qū)⑾l(fā)送到一個(gè)或多個(gè)其他參與者。
  4. Actor可能會(huì)堵塞自己,但Actor不應(yīng)該堵塞它運(yùn)行的線程。

介紹

The Actor Model provides a higher level of abstraction for writing concurrent and distributed systems. It alleviates the developer from having to deal with explicit locking and thread management, making it easier to write correct concurrent and parallel systems.

特性

Minimalistic API - The API should be small and easy to use. Avoid enterprisey JVM like containers and configurations.

Build on existing technologies - There are already a lot of great tech for e.g. networking and clustering, build on those. e.g. gRPC streams for networking, Consul.IO for clustering.

Pass data, not objects - Serialization is an explicit concern, don't try to hide it. Protobuf all the way.

Be fast - Do not trade performance for magic API trickery.

Ultra fast remoting, Proto Actor currently manages to pass over two million messages per second between nodes using only two actors, while still preserving message order! This is six times more the new super advanced UDP based Artery transport for Scala Akka, and 30 times faster than Akka.NET.

重點(diǎn)

該框架目前仍然是Beta版本狀態(tài),但已經(jīng)被很多團(tuán)隊(duì)使用在生產(chǎn)環(huán)境,官網(wǎng)提示當(dāng)該版本到達(dá)1.0后,API接口可能會(huì)發(fā)生改變,因此如果考慮迭代中版本兼容的問題,使用protoactor-go上生產(chǎn)確實(shí)不是一個(gè)最佳選擇。

后記

在構(gòu)建分布式高并發(fā)、容錯(cuò)通信平臺(tái),對(duì)比Java Akka,Erlang,Rust actix的光環(huán),protoactor-go確實(shí)不算很耀眼,但是作為Golang開發(fā)者,protoactor-go 幾乎是目前最好的選擇。
官方網(wǎng)站 http://proto.actor
GitHub https://github.com/AsynkronIT/protoactor-go
希望大家能從protoactor-go學(xué)到有價(jià)值的東西。
愿我們在Go 語言的學(xué)習(xí)之路上 從此結(jié)伴而行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容