操作系统 : 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. day76 auth模块 用户验证,

    概要: form组件回顾: (1) 创建form组件对应的类,比如LoginForm (2) views.login: if get请求: form_obj=LoginForm() return re ...

  2. POJ 3122 Pie【二分答案】

    <题目链接> 题目大意: 将n个半径不一但是高度为1的蛋糕分给 F+1个人,每个人分得蛋糕的体积应当相同,并且需要注意的是,每个人分得的整块蛋糕都只能从一个蛋糕上切下来,而不是从几个蛋糕上 ...

  3. input模拟输入下拉框

       功能点: 输入.下拉选择.根据输入内容模糊检索.键盘上下键选择 实现思路: 显示隐藏: input获取焦点显示,失去焦点隐藏 下拉选择: 以父元素为基准,通过绝对定位定位至input输入下方 模 ...

  4. Codeforces Round #530 (Div. 2)

    RANK :2252 题数 :3 补题: D - Sum in the tree 思路:贪心 把权值放在祖先节点上 ,预处理 每个节点保存 他与他儿子中 权值最小值即可. 最后会有一些叶子节点依旧为 ...

  5. asp.net core Session的测试使用心得及注意事项

    sp.net-core中Session是以中间件的形式注册使用的.不比asp.net中的使用,直接使用Session就行. 首先在.net-core框架中注入Session中间件,首先在Configu ...

  6. Android-自定义View前传-View的三大流程-Layout

    Android-自定义View前传-View的三大流程-Layout 参考 <Android开发艺术探索> https://github.com/hongyangAndroid/FlowL ...

  7. XamarinAndroid组件教程设置动画的时长参数

    XamarinAndroid组件教程设置动画的时长参数 在添加动画的时候,开发者还可以动画参数进行设置,如动画持续的时长.插值器等.下面依次讲解动画参数的设置方法. 1.设置动画时长 设置动画持续的时 ...

  8. c# Array、ArrayList、List

    1.Array:在内存中连续存储.索引速度快.赋值,修改元素简单. 不足:(1)插入数据麻烦 (2)声明时必须指定长度 2.ArrayList:解决了Array的不足 不足:(1)类型不安全 (2)存 ...

  9. [JOISC2014]たのしい家庭菜園

    [JOISC2014]たのしい家庭菜園 题目大意: 给定一个长度为\(n(n\le3\times10^5)\)的序列\(A(A_i\le10^9)\).只能交换相邻两个数,问最少需要几步可以将它变成一 ...

  10. CentOs中玩docker

    1.启动: systemctl start docker.service 2.停止: systemctl stop docker 3.从usts上拉取仓库 编辑文件 vi /etc/docker/da ...