這篇文章主要介紹“go語言中beehive源碼分析”,在日常操作中,相信很多人在go語言中beehive源碼分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”go語言中beehive源碼分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
鋼城網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,鋼城網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為鋼城上千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的鋼城做網(wǎng)站的公司定做!
beehive 非常有趣的在于各邏輯的解耦設(shè)計,這不僅讓本身功能操作簡單,也讓擴展變得關(guān)注點少了很多,只需要一點學(xué)習(xí)成本就可以擴展自己的 beehive
首先解釋一下 bee hive 中 的概念
bee 代表的是我們常見的 Worker 也就是說,實際的行為是由這些 小蜜蜂執(zhí)行的。他們就類似于采蜜的工人,采集到了之后統(tǒng)一放回來處理
hive 是蜂房,也就是我們常見的 WorkerPool 不同的是,她更像一個 Facotry ,什么意思呢?她可以創(chuàng)建專屬的 bee 。在極少的配置下,比如只需要配置上一個 token 即可。就可以生成一只 bee 專門針對某一種蜜工作了。
chain 又是什么? chain 就是鏈接事件與處理的工具,我們認為 bee 采回蜜是一個事件,總不可能采回來啥都不干吧。針對不同的 蜜 我們就有不同的反應(yīng),就有不同的 action
比如某人的 blog 更新了 ,rss bee 接收到了之后飛回來,我們就可以再要求 email bee 把這其中的信息通過郵件發(fā)給我們或者我們想發(fā)給的人。
這就需要 chain 來聯(lián)系 event 和 action 了
cdn.nlark.com/yuque/0/2019/svg/176280/1564626091652-31230c7c-0534-4b43-a1af-39e06c3cc34d.svg">
成組的 API 實現(xiàn)了 Resource 接口注冊到 Container 的路由 Route 中。
API 只是供調(diào)用,邏輯重點在 bees 這個包里的實現(xiàn)。
首先是有的接口
// BeeInterface is an interface all bees implement.
type BeeInterface interface {
// Name of the bee
Name() string
// Namespace of the bee
Namespace() string
// Description of the bee
Description() string
// SetDescription sets a description
SetDescription(s string)
// Config returns this bees config
Config() BeeConfig
// Options of the bee
Options() BeeOptions
// SetOptions to configure the bee
SetOptions(options BeeOptions)
// ReloadOptions gets called after a bee's options get updated
ReloadOptions(options BeeOptions)
// Activates the bee
Run(eventChannel chan Event)
// Running returns the current state of the bee
IsRunning() bool
// Start the bee
Start()
// Stop the bee
Stop()
LastEvent() time.Time
LogEvent()
LastAction() time.Time
LogAction()
Logln(args ...interface{})
Logf(format string, args ...interface{})
LogErrorf(format string, args ...interface{})
LogFatal(args ...interface{})
SetSigChan(c chan bool)
WaitGroup() *sync.WaitGroup
// Handles an action
Action(action Action) []Placeholder
}和他的基礎(chǔ)實現(xiàn)
// Bee is the base-struct to be embedded by bee implementations.
type Bee struct {
config BeeConfig
lastEvent time.Time
lastAction time.Time
Running bool
SigChan chan bool
waitGroup *sync.WaitGroup
}這里需要注意的是 Run 接口,在 Base-Struct Bee 中該方法 是空的實現(xiàn),因為 Run 是 Bee 的生命周期開始處,是自動開始的。
簡單的看某一個實現(xiàn)即可
// WebBee is a Bee that starts an HTTP server and fires events for incoming
// requests.
type WebBee struct {
bees.Bee
addr string
eventChan chan bees.Event
}可以很清楚的指導(dǎo),這個 WebBee 中的 eventChan 正是通知的地方,也就是上文所說的 Chain 的開始處。注意的是由于松耦合的設(shè)計,任何 Bee 都可以成為 Chain 上的一環(huán),只要它能觸發(fā)事件。或者監(jiān)聽事件。
func (mod *WebBee) Run(cin chan bees.Event)
// Run executes the Bee's event loop.
func (mod *WebBee) Run(cin chan bees.Event) {
mod.eventChan = cin
srv := &http.Server{Addr: mod.addr, Handler: mod}
l, err := net.Listen("tcp", mod.addr)
if err != nil {
mod.LogErrorf("Can't listen on %s", mod.addr)
return
}
defer l.Close()
go func() {
err := srv.Serve(l)
if err != nil {
mod.LogErrorf("Server error: %v", err)
}
// Go 1.8+: srv.Close()
}()
select {
case <-mod.SigChan:
return
}
}同時 WebBee 也有一個方法 ServeHTTP 來實現(xiàn) http.Handle 來處理請求。
這里也就是前文所說的 注冊的那些 API 的部分來源,每一個 bee 自身實現(xiàn)的自動注冊暴露給外界調(diào)用。
func (mod *WebBee) ServeHTTP(w http.ResponseWriter, req *http.Request)
package:beehive/bees/event.go
剛才講到了 觸發(fā)事件 event 的 WebBee 實現(xiàn),現(xiàn)在我們來看 event 的實現(xiàn)
實際上是通過 這個函數(shù)實現(xiàn)的
// handleEvents handles incoming events and executes matching Chains.
func handleEvents() {
for {
event, ok := <-eventsIn
···
bee := GetBee(event.Bee)
(*bee).LogEvent()
···
go func() {
defer func() {
if e := recover(); e != nil {
log.Printf("Fatal chain event: %s %s", e, debug.Stack())
}
}()
execChains(&event)
}()
}
}省略了 日志部分。可以看到 handleEvents 通過接受通道里的 event,并檢查 event 中的 Bee 作為 標(biāo)志找到對應(yīng)的 Bee 喚醒。
這里我們可以看到 最后進入了 Chains 中執(zhí)行,即上文所說的 Chain 將 Event 和 Action 鏈接了起來,讓 Bee 之間能夠協(xié)作。
chain
package:beehive/bees/chains.go
chain 中實際上是調(diào)用 Actions 通過下面的 execActions 函數(shù)
for _, el := range c.Actions {
action := GetAction(el)
if action == nil {
log.Println("\t\tERROR: Unknown action referenced!")
continue
}
execAction(*action, m)
}我們來看看 Action 的執(zhí)行。
package: beehive/bees/actions.go
actions 既可以運行設(shè)置中的 options 也可以直接在 運行函數(shù)中傳入需要運行的 options
func execAction(action Action, opts map[string]interface{}) bool
整個執(zhí)行邏輯是如此了,其他還有一些
日志處理:用于跟蹤 bee 和 hive 的情況
Config:保存配置文件,每一次啟動可以重新放飛以前的 Bee 們
Signal:beehive 攔截了一些 Signal 的 Kill 等信號來執(zhí)行優(yōu)雅退出,避免了 Config 等的丟失。
Run Flag:執(zhí)行的附帶參數(shù),設(shè)定 Beehive 整個應(yīng)用的監(jiān)聽端口和版本配置。
到此,關(guān)于“go語言中beehive源碼分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
文章名稱:go語言中beehive源碼分析
網(wǎng)站鏈接:http://chinadenli.net/article40/ppcoho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、網(wǎng)頁設(shè)計公司、用戶體驗、定制網(wǎng)站、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)