InfluxDB添加新服務

来源:https://www.cnblogs.com/MikeZhang/archive/2018/02/10/InfluxdbAddService20180210.html
-Advertisement-
Play Games

操作系統 : CentOS7.3.1611_x64 go語言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 這裡以添加 syncd 服務為例記錄下InfluxDB添加新服務的流程。 添加主服務代碼 在 influxdata/influxdb/services 目錄建立 s ...


操作系統 : 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

type 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

import (
        "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.go
diff --git a/cmd/influxd/run/config.go b/cmd/influxd/run/config.go
index 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 syncd
syncdInstance := syncd.NewService(config.SyncdConfig)
go syncdInstance.Run()

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

使用 git diff 命令查看如下 :

[root@localhost run]# git diff command.go
diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go
index 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"

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

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180210_InfluxDB添加新服務.rst

歡迎補充


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • "回到目錄" 環境 環境,對於開發來說就是部署的一種場景,你可以是調試場景,測試場景,生產場景,當然還可以有很多其它的場景,只要你的項目需要就可以自定義,微軟幫我們定義了三種標準的環境變數,下麵來說一下. 預設定義三種場景 1. Development 開發環境 appsetting.Develop ...
  • 1、Crond定義 crond是Linux系統中用來定期執行命令或指定程式的一種服務或軟體。 (1)linux系統自身定期執行的任務(輪詢系統日誌、備份數據等) (2)用戶執行的任務(定時更新同步時間、網站數據備份等) 2、Crond命令語法 定時任務的命令是crontab,其守護進程是crond( ...
  • NAME vim - Vi IMproved, a programmers text editor #vi的改進,一個程式文本編輯器 1、移動游標的方法 2、查找和替換 /word 向游標下麵尋找一個名為word的字元串,配合n,向下查找,N向上查找 3、刪除、複製和粘貼 dd 刪除游標所在的行 y ...
  • 介紹 Zabbix是一款能夠監控各種網路參數以及伺服器健康性和完整性的軟體。Zabbix使用靈活的通知機制,允許用戶為幾乎任何事件配置基於郵件的告警。這樣可以快速反饋伺服器的問題。基於已存儲的數據,Zabbix提供了出色的報告和數據可視化功能。這些功能使得Zabbix成為容量規劃的理想方案。 新版已 ...
  • Nagios->check_openmanage[Dell R7*] Nagios->check_openmanage[Dell R7*] 2014年11月13日 下午 07:44 2014年11月13日 下午 07:44 需求介紹: 透過Nagios監控Dell R7系列伺服器硬體狀態 環境信息: ...
  • 大概分兩步,先自定義字元串規則如去除空格特殊符號等等,再使用轉義,後面的語句一定要加上單引號。 ...
  • [1]介紹 [2]用戶模型 [3]加密 [4]驗證 [5]測試 ...
  • 介紹 對於任何人而言,用T-SQL語句來寫聚會查詢都是工作中重要的一環。我們大家也都很熟悉GROUP BY子句來實現聚合表達式,但是如果打算在一個結果集中包含多種不同的彙總結果,可能會比較麻煩。我將舉例展示給大家使用GROUPING SETS操作符來完成這個“混合的結果集”。 或許當我們在打算分析較 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...