模擬寫(xiě)個(gè)日志監(jiān)控程序:
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有商水免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
package main
import (
"fmt"
"time"
"strings"
)
type LogProcess struct {
rc chan string //讀chan
wc chan string //寫(xiě)chan
path string //讀取文件的路徑
influxDBDsn string //influx data source
}
func (l *LogProcess) ReadFromFile() {
//讀取模塊
line := "message"
l.rc <- line
}
func (l *LogProcess) Process() {
//解析模塊
data := <- l.rc
l.wc <- strings.ToUpper(data)
}
func (l *LogProcess) WriteToInfluxDB() {
//寫(xiě)入模塊
fmt.Println("寫(xiě)入成功:", <-l.wc)
}
func main() {
lp := &LogProcess{
rc: make(chan string),
wc: make(chan string),
path: "D:/go_work_dir/logs/access.log",
influxDBDsn: "username&password..",
}
go lp.ReadFromFile()
go lp.Process()
go lp.WriteToInfluxDB()
time.Sleep(time.Second)
}
但是程序?qū)懰懒?,ReadFromFile只能從文件里讀??;而WriteToInfluxDB只能寫(xiě)入InfluxDB... 如果是從隊(duì)列,從標(biāo)準(zhǔn)輸出讀取呢?難道要定義更多模塊?下面用到接口的設(shè)計(jì)方式解決
package main
import (
"fmt"
"time"
)
//接口做抽象優(yōu)化
type Reader interface {
Read(rc chan string)
}
//接口做抽象優(yōu)化
type Writer interface {
Write(wc chan string)
}
type LogProcess struct {
rc chan string //讀chan
wc chan string //寫(xiě)chan
read Reader
write Writer
}
type ReadFromFile struct {
path string //讀取文件的路徑
}
type WriteToInfluxDB struct {
influxDBDsn string //influx data source
}
func (r *ReadFromFile) Read(rc chan string) {
//讀取模塊
line := "message"
rc <- line
}
func (l *LogProcess) Process() {
//解析模塊
data := <- l.rc
l.wc <- data
}
func (w *WriteToInfluxDB) Write(wc chan string) {
//寫(xiě)入模塊
fmt.Println("寫(xiě)入成功:", <-wc)
}
func main() {
r := &ReadFromFile{
path: "D:/go_work_dir/logs/access.log",
}
w := &WriteToInfluxDB{
influxDBDsn: "username&password..",
}
lp := &LogProcess{
rc: make(chan string),
wc: make(chan string),
read: r,
write: w,
}
go lp.read.Read(lp.rc)
go lp.Process()
go lp.write.Write(lp.wc)
time.Sleep(time.Second)
}
分享文章:日志監(jiān)控程序1(抽象接口進(jìn)行優(yōu)化)
文章位置:http://jinyejixie.com/article14/ijjjge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站內(nèi)鏈、網(wǎng)站維護(hù)、、定制網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)