go get gopkg.in/ini.v1
ini一个非常方便的能够配置配置文件的包
官方手册

go get github.com/sirupsen/logrus
logrus是一个日志库

https://zhuanlan.zhihu.com/p/105759117

简单示例

读取配置文件

package main

import (
    "fmt"

    "github.com/sirupsen/logrus"
    "gopkg.in/ini.v1"
)

func main() {
    cfg, err := ini.Load("./configure.ini")
    if err != nil {
        logrus.Error("failed to load configure:", err)
        return
    }
    kafka_addr := cfg.Section("kafka").Key("addr").String()
    kafka_port := cfg.Section("kafka").Key("port").String()
    fmt.Println("kafka addr:", kafka_addr)
    fmt.Println("kafka_port:", kafka_port)
}

//configure.ini
//[kafka]
//addr = 127.0.0.1
//port = 9092

结构体映射

package main

import (
    "fmt"

    "github.com/sirupsen/logrus"
    "gopkg.in/ini.v1"
)

type Kafka struct {
    Address string `ini:"addr"`
    Port    string `ini:"port"`
}

type Tsdb struct {
    Address string `ini:"address"`
}

type Configure struct {
    Kafka `ini:"kafka"`
    Tsdb  `ini:"tsdb"`
}

func main() {
    var configOBJ = new(Configure) //创建一个configure类型的指针并分配空间
    cfg, err := ini.Load("./configure.ini")
    if err != nil {
        logrus.Error("failed to load configure:", err)
        return
    }
    // err = ini.MapTo(configOBJ,cfg)
    // err := ini.MapTo(configOBJ, "./configure.ini")
    err = cfg.MapTo(configOBJ)
    if err != nil {
        logrus.Error("failed to reflect:", err)
        return
    }
    fmt.Printf("%#v\n", configOBJ)
}

//&main.Configure{Kafka:main.Kafka{Address:"127.0.0.1", Port:"9092"}, Tsdb:main.Tsdb{Address:"127.0.0.1"}}

格式化输出

//kafka.go
package kafka

import (
    "github.com/Shopify/sarama"
    "github.com/sirupsen/logrus"
)

var (
    Kafka_client sarama.SyncProducer
)

func Kafkainit(address []string) (err error) {
    config := sarama.NewConfig()                              //配置
    config.Producer.RequiredAcks = sarama.WaitForAll          //ack应答模式为all
    config.Producer.Partitioner = sarama.NewRandomPartitioner //partition的选择方式为随机
    config.Producer.Return.Successes = true                   //Successes channel 返回值

    Kafka_client, err = sarama.NewSyncProducer(address, config) //使用参数创建一个SyncProducer
    //    defer kafka_client.Close()
    if err != nil {
        logrus.Error("broker connection has something wrong happened:", err)
        return err
    }
    return err
}

//tailread.go
package tailfile

import (
    "fmt"

    "github.com/nxadm/tail"
)

var (
    tail_context *tail.Tail
)

func Tailinit(filename string) (err error) {
    config := tail.Config{
        ReOpen:    true, //当出现轮转切割的时候,这两个参数会去追踪新的文件
        Follow:    true,
        Location:  &tail.SeekInfo{Offset: 0, Whence: 2}, //从哪开始
        MustExist: false,                                //允许文件不存在
        Poll:      true,                                 //轮询
    }

    tail_context, err = tail.TailFile(filename, config)
    if err != nil {
        fmt.Println("error happens:", err)
    }
    return err
}


//example.go
package main

import (
    "fmt"

    "example.go/kafka"
    "example.go/tailfile"
    "github.com/sirupsen/logrus"
    "gopkg.in/ini.v1"
)

type Kafka struct {
    Address string `ini:"addr"`
    Topic   string `ini:"topic"`
}

type Tsdb struct {
    Address    string `ini:"address"`
    Configpath string `ini:"confpath"`
}

type Configure struct {
    Kafka `ini:"kafka"`
    Tsdb  `ini:"tsdb"`
}

func main() {
    logrus.SetLevel(logrus.DebugLevel)
    var configOBJ = new(Configure) //创建一个configure类型的指针并分配空间
    cfg, err := ini.Load("./configure.ini")
    if err != nil {
        logrus.Error("failed to load configure:", err)
        return
    }
    // err = ini.MapTo(configOBJ,cfg)
    // err := ini.MapTo(configOBJ, "./configure.ini")
    err = cfg.MapTo(configOBJ)
    if err != nil {
        logrus.Error("failed to reflect:", err)
        return
    }
    fmt.Printf("%#v\n", configOBJ)

    err = kafka.Kafkainit([]string{configOBJ.Kafka.Address}) //类型转换

    if err != nil {
        logrus.Error("kafka init failed:", err)
        return
    }

    logrus.Debug("kafka init success")

    err = tailfile.Tailinit(configOBJ.Configpath)

    if err != nil {
        logrus.WithFields(logrus.Fields{"filename": configOBJ.Configpath, "err": err}).Error("file loaded failed")
        return
    }

    logrus.WithFields(logrus.Fields{"filename": configOBJ.Tsdb.Configpath}).Debug("file loaded successfully")
}


//go run ./example.go
//&main.Configure{Kafka:main.Kafka{Address:"127.0.0.1:9092", Topic:"test1"}, Tsdb:main.Tsdb{Address:"127.0.0.1", Configpath:"./configure.ini"}}
//DEBU[0000] kafka init success                           
//DEBU[0000] file loaded successfully                      filename=./configure.ini

标签: none

评论已关闭