这篇文章主要讲解了“Golang怎么监听日志文件并发送到kafka中”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Golang怎么监听日志文件并发送到kafka中”吧!
涉及的golang库和可视化工具:
go-inisaramatail
go-inisaramatail
可视化工具:
offsetexplorer
工作的流程
saramakafkatailtail
环境准备
zookeeperkafkasaramaoffsetexplorer
代码分层
tailsarama
main函数为程序入口
关键的代码
main.go
tailsrama
package main import ( "fmt" "sarama/serve" "github.com/go-ini/ini" ) type KafkaConfig struct { Address string `ini:"address"` ChannelSize int `ini:"chan_size"` } type TailConfig struct { Path string `ini:"path"` Filename string `ini:"fileName"` // 如果是结构体,则指明分区名 Children `ini:"tailfile.children"` } type Config struct { KafkaConfig `ini:"kafka"` TailConfig `ini:"tailfile"` } type Children struct { Name string `ini:"name"` } func main() { // 加载配置 var cfg = new(Config) err := ini.MapTo(cfg, "./conf/go-conf.ini") if err != nil { fmt.Print(err) } // 初始化kafka ks := &serve.KafukaServe{} // 启动kafka消息监听。异步 ks.InitKafka([]string{cfg.KafkaConfig.Address}, int64(cfg.KafkaConfig.ChannelSize)) // 关闭主协程时,关闭channel defer ks.Destruct() // 初始化tail ts := &serve.TailServe{} ts.TailInit(cfg.TailConfig.Path + "/" + cfg.TailConfig.Filename) // 阻塞 ts.Listener(ks.MsgChan) }
kafka.go
有3个方法 :
InitKafkaListenerDestruct
package serve import ( "fmt" "github.com/Shopify/sarama" ) type KafukaServe struct { MsgChan chan string //err error } func (ks *KafukaServe) InitKafka(addr []string, chanSize int64) { // 读取配置 config := sarama.NewConfig() // 1. 初始化生产者配置 config.Producer.RequiredAcks = sarama.WaitForAll // 选择分区 config.Producer.Partitioner = sarama.NewRandomPartitioner // 成功交付的信息 config.Producer.Return.Successes = true ks.MsgChan = make(chan string, chanSize) go ks.Listener(addr, chanSize, config) } func (ks *KafukaServe) Listener(addr []string, chanSize int64, config *sarama.Config) { // 连接kafka var kafkaClient, _ = sarama.NewSyncProducer(addr, config) defer kafkaClient.Close() for { select { case content := <-ks.MsgChan: // msg := &sarama.ProducerMessage{ Topic: "weblog", Value: sarama.StringEncoder(content), } partition, offset, err := kafkaClient.SendMessage(msg) if err != nil { fmt.Println(err) } fmt.Println("分区,偏移量:") fmt.Println(partition, offset) fmt.Println("___") } } } func (ks *KafukaServe) Destruct() { close(ks.MsgChan) }
tail.go
主要包括了两个方法:
TailInittailListenerListenerkafka
package serve import ( "fmt" "github.com/hpcloud/tail" ) type TailServe struct { tails *tail.Tail } func (ts *TailServe) TailInit(filenName string) { config := tail.Config{ ReOpen: true, Follow: true, Location: &tail.SeekInfo{Offset: 0, Whence: 2}, MustExist: false, Poll: true, } // 打开文件开始读取数据 ts.tails, _ = tail.TailFile(filenName, config) // if err != nil { // fmt.Println("tails %s failed,err:%v\n", filenName, err) // return nil, err // } fmt.Println("启动," + filenName + "监听") } func (ts *TailServe) Listener(MsgChan chan string) { for { msg, ok := <-ts.tails.Lines if !ok { // todo fmt.Println("数据接收失败") return } fmt.Println(msg.Text) MsgChan <- msg.Text } } // 测试案例 func Demo() { filename := `E:\xx.log` config := tail.Config{ ReOpen: true, Follow: true, Location: &tail.SeekInfo{Offset: 0, Whence: 2}, MustExist: false, Poll: true, } // 打开文件开始读取数据 tails, err := tail.TailFile(filename, config) if err != nil { fmt.Println("tails %s failed,err:%v\n", filename, err) return } var ( msg *tail.Line ok bool ) fmt.Println("启动") for { msg, ok = <-tails.Lines if !ok { fmt.Println("tails file close reopen,filename:$s\n", tails.Filename) } fmt.Println("msg:", msg.Text) } }
感谢各位的阅读,以上就是“Golang怎么监听日志文件并发送到kafka中”的内容了,经过本文的学习后,相信大家对Golang怎么监听日志文件并发送到kafka中这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!