Use of Etcd in Go language log collection project

Log collection item (II) use of Etcd

ETCD introduction

  • Concept: highly available distributed key value storage for configuration sharing and service discovery
  • Similar projects: zookeeper and consumer
  • Development language: Go
  • Interface: provide restful http interface, easy to use
  • Implementation algorithm: a highly consistent, highly available service storage directory based on the raft algorithm

Application scenarios of ETCD

  • Service discovery and registration
  • Configuration center
  • Distributed lock
  • master election

ETCD environment construction

[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 & & start etcd
  • etcdctl use
[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
  • go to realize watch function
install v3 plug-in unit
go get go.etcd.io/etcd/clientv3

code

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))
         }
      }
   }
}
  • go realizes put function
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)
   }
}
  • kafka consumption code
package main

import (
   "fmt"
   "github.com/Shopify/sarama"
   "sync"
)

var wg sync.WaitGroup

func main() {
   //Connection configuration
   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()
}

Tags: Go github Zookeeper Linux kafka

Posted on Fri, 15 May 2020 11:06:41 -0400 by brian183