CentOS7.3下InfluxDB添加新服務

來源:互聯網
上載者:User

CentOS7.3下InfluxDB添加新服務

作業系統 : CentOS7.3.1611_x64

go語言版本:1.8.3 linux/amd64

InfluxDB版本:1.1.0

這裡以添加 syncd 服務為例記錄下InfluxDB添加新服務的流程。

添加主服務代碼

在 influxdata/influxdb/services 目錄建立 syncd 檔案夾,用於存放 syncd 服務相關代碼。

1、添加服務配置相關內容

添加 config.go 檔案,樣本內容如下:

package syncd// E-Mail : Mike_Zhang@live.comtype Config struct {        Enabled        bool   `toml:"enabled"`        LogEnabled     bool   `toml:"log-enabled"`        RemoteTSDBHost string `toml:"remote-host"`        DefaultDB      string `toml:"defaultDB"`}func NewConfig() Config {        return Config{                Enabled:        true,                LogEnabled:     true,                RemoteTSDBHost: "http://127.0.0.1:8086",                DefaultDB:      "Monitor",        }}

解釋如下:

  • 定義 Config 用於存放具體配置;
  • 添加 NewConfig 函數,用於建立 Config 對象;

2、添加 syncd 服務的具體內容

添加 syncd.go 檔案,樣本內容如下:

package syncd// E-Mail : Mike_Zhang@live.comimport (        "log"        "os"        "time")type Service struct {        Logger      *log.Logger        remote_host string        DefaultDB   string        username    string        password    string}func NewService(c Config) *Service {        return &Service{                remote_host: c.RemoteTSDBHost,                DefaultDB:   c.DefaultDB,                username:    "root",                password:    "root",                Logger:      log.New(os.Stderr, "[syncd] ", log.LstdFlags),        }}func (s *Service) Run() {    for {            cur_time := time.Now().Unix()            s.Logger.Printf("current timestamp : %d\n", cur_time)            time.Sleep(1 * time.Second)    }}
 

解釋如下: * 定義Service結構;

  • 添加 NewService 函數,用於建立具體服務;
  • 添加 Run 函數,實現具體服務

該函數作為入口實現具體的服務,這裡以在日誌中定時列印時間戳記作為具體的服務內容。

在伺服器主程式中啟動服務

1、添加配置相關代碼

進入 influxdata/influxdb/cmd/influxd 目錄,修改 run/config.go 檔案。

  • 引入 syncd 服務

在 import 中加入如下代碼:

"github.com/influxdata/influxdb/services/syncd"
  • 定義配置

在 Config 結構中加入如下變數:

SyncdConfig    syncd.Config      `toml:"syncd"`
  • 初始化配置

在 NewConfig 函數中加入如下代碼:

c.SyncdConfig = syncd.NewConfig()

使用 git diff 命令查看如下 :

[root@localhost run]# git diff config.godiff --git a/cmd/influxd/run/config.go b/cmd/influxd/run/config.goindex 36e4f14..01df0cc 100644--- a/cmd/influxd/run/config.go+++ b/cmd/influxd/run/config.go@@ -27,6 +27,7 @@ import (        "github.com/influxdata/influxdb/services/precreator"        "github.com/influxdata/influxdb/services/retention"        "github.com/influxdata/influxdb/services/subscriber"+       "github.com/influxdata/influxdb/services/syncd"        "github.com/influxdata/influxdb/services/udp"        "github.com/influxdata/influxdb/tsdb" )@@ -48,6 +49,7 @@ type Config struct {        Monitor        monitor.Config    `toml:"monitor"`        Subscriber     subscriber.Config `toml:"subscriber"`        HTTPD          httpd.Config      `toml:"http"`+       SyncdConfig    syncd.Config      `toml:"syncd"`        GraphiteInputs []graphite.Config `toml:"graphite"`        CollectdInputs []collectd.Config `toml:"collectd"`        OpenTSDBInputs []opentsdb.Config `toml:"opentsdb"`@@ -84,6 +86,7 @@ func NewConfig() *Config {        c.Retention = retention.NewConfig()        c.BindAddress = DefaultBindAddress+       c.SyncdConfig = syncd.NewConfig()        return c }[root@localhost run]#

2、添加啟動服務代碼

進入 influxdata/influxdb/cmd/influxd 目錄,修改 run/command.go 檔案

  • 引入 syncd 服務

在 import 中加入如下代碼:

"github.com/influxdata/influxdb/services/syncd"
  • 添加啟動代碼

在 Command->Run 函數中加入如下代碼(go cmd.monitorServerErrors() 之前):

// start syncdsyncdInstance := syncd.NewService(config.SyncdConfig)go syncdInstance.Run()

在 Config 結構中加入如下變數:

使用 git diff 命令查看如下 :

[root@localhost run]# git diff command.godiff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.goindex 51036f1..8743f04 100644--- a/cmd/influxd/run/command.go+++ b/cmd/influxd/run/command.go@@ -1,6 +1,7 @@ package run import (+    "github.com/influxdata/influxdb/services/syncd"        "flag"        "fmt"        "io"@@ -120,6 +121,11 @@ func (cmd *Command) Run(args ...string) error {        }        cmd.Server = s+    // start syncd+       syncdInstance := syncd.NewService(config.SyncdConfig)+       go syncdInstance.Run()++        // Begin monitoring the server's error channel.        go cmd.monitorServerErrors()[root@localhost run]#
測試服務

進入 influxdata/influxdb/cmd/influxd 目錄,執行 go build 命令,並將編譯好的二進位檔案copy到bin目錄,具體如下:

[root@localhost influxd]# go build[root@localhost influxd]# cp influxd /usr/bin/cp: overwrite ‘/usr/bin/influxd’? y[root@localhost influxd]#

啟動InfluxDB伺服器,在控制台可以看到如下內容:

[root@localhost influxdb]# influxd 8888888           .d888 888                   8888888b.  888888b.   888            d88P"  888                   888  "Y88b 888  "88b   888            888    888                   888    888 888  .88P   888   88888b.  888888 888 888  888 888  888 888    888 8888888K.   888   888 "88b 888    888 888  888  Y8bd8P' 888    888 888  "Y88b   888   888  888 888    888 888  888   X88K   888    888 888    888   888   888  888 888    888 Y88b 888 .d8""8b. 888  .d88P 888   d88P 8888888 888  888 888    888  "Y88888 888  888 8888888P"  8888888P"[run] 2018/02/07 04:24:28 InfluxDB starting, version unknown, branch unknown, commit unknown[run] 2018/02/07 04:24:28 Go version go1.8.3, GOMAXPROCS set to 2[run] 2018/02/07 04:24:28 Using configuration at: /etc/influxdb/influxdb.conf[store] 2018/02/07 04:24:29 Using data dir: /var/lib/influxdb/data...[syncd] 2018/02/07 21:56:11 current timestamp : 1518058571[syncd] 2018/02/07 21:56:12 current timestamp : 1518058572[syncd] 2018/02/07 21:56:13 current timestamp : 1518058573

產生新的設定檔:

influxd config > new.conf

可以看到 syncd 服務預設配置如下:

[syncd]  enabled = true  log-enabled = true  remote-host = "http://127.0.0.1:8086"  defaultDB = "Monitor"

好,就這些了,希望對你有協助。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.