Go语言(二十)日志采集项目(二)Etcd的使用
2021-01-21 11:14
标签:erro one done 存储目录 nil err 应用 连接 node 代码 Go语言(二十)日志采集项目(二)Etcd的使用 标签:erro one done 存储目录 nil err 应用 连接 node 原文地址:https://blog.51cto.com/13812615/2495685ETCD 介绍
ETCD的应用场景
ETCD环境搭建
[root@centos7-node1 etcd]# nohup ./etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-advertise-peer-urls http://0.0.0.0:2380 & #启动etcd
[root@centos7-node1 ~]# cd /opt/application/etcd/
[root@centos7-node1 etcd]# ./etcdctl --endpoints "http://localhost:2379" put /logagent/conf 333333
[root@centos7-node1 etcd]# ./etcdctl --endpoints "http://localhost:2379" watch /logagent/conf
[root@centos7-node1 etcd]# ./etcdctl --endpoints "http://localhost:2379" del /logagent/conf
安装v3插件
go get go.etcd.io/etcd/clientv3
package main
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"time"
)
func main() {
client,err := clientv3.New(clientv3.Config{
Endpoints: []string{"192.168.56.11:2379"},
DialTimeout: time.Second*3,
})
defer client.Close()
fmt.Printf("conn succ\n")
for {
resultChan := client.Watch(context.Background(),"/logagent/conf")
for v := range resultChan{
if v.Err() != nil {
fmt.Printf("watch faild,err:%v\n",err)
continue
}
for _,e := range v.Events {
fmt.Printf("event_type:%v,key:%v,val:%v\n",e.Type,e.Kv.Key,string(e.Kv.Value))
}
}
}
}
package main
import (
"context"
"fmt"
"go.etcd.io/etcd/clientv3"
"time"
)
func main() {
client,err := clientv3.New(clientv3.Config{
Endpoints: []string{"192.168.56.11:2379"},
DialTimeout: time.Second*3,
})
defer client.Close()
fmt.Printf("conn succ\n")
_,err = client.Put(context.Background(),"/logagent/conf","sddadas")
if err != nil {
fmt.Printf("Put faild,err:%v\n",err)
}
}
package main
import (
"fmt"
"github.com/Shopify/sarama"
"sync"
)
var wg sync.WaitGroup
func main() {
//连接配置
consumer,err := sarama.NewConsumer([]string{"192.168.56.11:9092"},nil)
if err != nil {
fmt.Printf("consumer message faild,error:%v\n",err)
return
}
fmt.Printf("conn succ\n")
pt,err := consumer.Partitions("nginx_log")
if err != nil {
fmt.Printf("get partions aild,err:%v\n",err)
return
}
for _,p := range pt {
pc, err := consumer.ConsumePartition("nginx_log",p,sarama.OffsetNewest)
if err != nil {
fmt.Printf("consumer faild,error:%v\n",err)
continue
}
wg.Add(1)
go func() {
for m := range pc.Messages() {
fmt.Printf("topic:%v,value:%v\n",m.Topic,string(m.Value))
}
wg.Done()
}()
}
wg.Wait()
}
上一篇:java后台大数据量下的分批入库
下一篇:java 6 -数组
文章标题:Go语言(二十)日志采集项目(二)Etcd的使用
文章链接:http://soscw.com/index.php/essay/44964.html