操作系统 : 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.com
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
// E-Mail : Mike_Zhang@live.com
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( * 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
--- a/cmd/influxd/run/config.go
+++ b/cmd/influxd/run/config.go
@@ -, +, @@ 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"
)
@@ -, +, @@ 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"`
@@ -, +, @@ 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
--- a/cmd/influxd/run/command.go
+++ b/cmd/influxd/run/command.go
@@ -, +, @@
package run import (
+ "github.com/influxdata/influxdb/services/syncd"
"flag"
"fmt"
"io"
@@ -, +, @@ 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

            .d888                    8888888b.  888888b.
d88P" 888 888 "Y88b "88b
.88P
88888b. 8888888K.
"88b 888 888 888 888 Y8bd8P' 888 888 888 "Y88b
X88K
Y88b .d8""8b. .d88P d88P
"Y88888 888 888 8888888P" 8888888P" [run] // :: InfluxDB starting, version unknown, branch unknown, commit unknown
[run] // :: Go version go1.8.3, GOMAXPROCS set to
[run] // :: Using configuration at: /etc/influxdb/influxdb.conf
[store] // :: Using data dir: /var/lib/influxdb/data ... [syncd] // :: current timestamp :
[syncd] // :: current timestamp :
[syncd] // :: current timestamp :

生成新的配置文件:

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

欢迎补充

InfluxDB添加新服务的更多相关文章

  1. Ambari中添加新服务

    官网: https://cwiki.apache.org/confluence/pages/viewpage.action?pageId=38571133 一.背景 栈的定义可以在源代码树中找到/am ...

  2. Dos命令删除添加新服务

    安装服务sc create Svnservice binpath= "d:\subversion\bin\svnserve.exe --service -r E:\projectversio ...

  3. 添加ssh服务构建新镜像-docker commit 方式01

    添加ssh服务构建新镜像-docker commit 方式 1:docker commit构建自定义镜像---一般不推荐使用这种方式构建新镜像 1:ubuntu基础镜像下载 ubuntu@ubuntu ...

  4. 【PostgreSQL】PostgreSQL添加新服务器连接时,报错“Server doesn't listen ”,已解决。

    PostgreSQL添加新的服务器连接时,报错:

  5. 如何手动添加Windows服务和如何把一个服务删除

    windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...

  6. 向CDH5集群中添加新的主机节点

    向CDH5集群中添加新的主机节点 步骤一:首先得在新的主机环境中安装JDK,关闭防火墙.修改selinux.NTP时钟与主机同步.修改hosts.与主机配置ssh免密码登录.保证安装好了perl和py ...

  7. WebKit JavaScript Binding添加新DOM对象的三种方式

    一.基础知识 首先WebKit IDL并非完全遵循Web IDL,只是借鉴使用.WebKit官网提供了一份说明(WebKitIDL),比如Web IDL称"operation”(操作), 而 ...

  8. TIA Portal V12不能添加新的CPU

    4核AMD 740,10G内存,Win7 X64,打开TIA Portal V12,依旧慢如牛,鼠标指针转啊转,TIA窗口写着 无响应... 真没志气,STM32要是玩转了,坚决不用这老牛. 上图为正 ...

  9. 为镜像添加SSH服务

    操作Docker容器介绍了一些进入容器的办法,比如attach.exec等命令,但是这些命令都无法解决远程管理容器的问题.因此,当需要远程登录到容器内进行一些操作的时候,就需要SSH的支持了. 如何自 ...

随机推荐

  1. MySQL高级02

    索引简介 索引(Index)是帮助MySQL高效获取数据的数据结构.可以得到索引的本质:索引是数据结构.你可以简单理解为“排好序的快速查找数据结构”. 在数据之外,数据库系统还维护着满足特定查找算法的 ...

  2. tensorflow基础架构 - 处理结构+创建一个线性回归模型+session+Variable+Placeholder

    以下仅为自己的整理记录,绝大部分参考来源:莫烦Python,建议去看原博客 一.处理结构 因为TensorFlow是采用数据流图(data flow graphs)来计算, 所以首先我们得创建一个数据 ...

  3. 满血复活--来自世一大的WAR

    最需要复习的清单 1.二分 2.图论 3.数论 4.dp

  4. css position相对定位与绝对定位彻底搞懂

    定位position position本身就有给...定位的意思 position属性的值: static ---默认值 relative ---相对定位 absolute ---绝对定位 fixed ...

  5. IIS PHP

    Name:PHP_FCGI_MAX_REQUESTS Value:10000

  6. JS实现缓动动画效果

    原理如下: 假设要从数值A变化到数值B,如果是线性运动,则每次移动距离是一样:如果是缓动,每次移动距离不一样.那如何才能不一样呢?很简单,按比例移动就可以. 例如:每次移动剩余距离的一半. 对吧,超容 ...

  7. Android应用源码 概览

    之前我讲过关于Android应用源码的使用,不要走弯路,没有用的源码不要深究. 记住目录就好. 这里还有很多源码, Javaapk这个网站里的.还有很多. 这些源码 有可能有用. 但是不必故意用它. ...

  8. 在addroutes后,$router.options.routes没有更新的问题(手摸手,带你用vue撸后台 读后感)

    参照<着手摸手,带你用vue撸后台>一文,本人做了前端的权限判断 https://segmentfault.com/a/1190000009275424 首先就是在addroutes后,$ ...

  9. LOJ.6068.[2017山东一轮集训Day4]棋盘(费用流zkw)

    题目链接 考虑两个\(\#\)之间产生的花费是怎样的.设这之间放了\(k\)个棋子,花费是\(\frac{k(k-1)}{2}\). 在\((r,c)\)处放棋子,行和列会同时产生花费,且花费和该行该 ...

  10. Java并发编程(七)-- ThreadLocal

    提到ThreadLocal,有些Android或者Java程序员可能有所陌生,可能会提出种种问题,它是做什么的,是不是和线程有关,怎么使用呢?等等问题,本文将总结一下我对ThreadLocal的理解和 ...