操作系统 : 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. day28 面向对象:反射,内置函数,类的内置方法

    面向对象进阶博客地址链接: http://www.cnblogs.com/Eva-J/articles/7351812.html 复习昨日内容: # 包 # 开发规范 # # hashlib # 登录 ...

  2. Create-react-app+Antd-mobile+Less配置(学习中的记录)

    (参考别人结合自己的整理得出,若有错误请大神指出) Facebook 官方推出Create-React-App脚手架,基本可以零配置搭建基于webpack的React开发环境,内置了热更新等功能. 详 ...

  3. es6的分析总结

    1,var let const对比 1,箭头函数的总结 /** * 1,箭头函数没有this,箭头函数this没有被箭头的函数,所以不能使用call,apply,bind改变this指向 * 2,箭头 ...

  4. HDU 3594 Cactus (强连通+仙人掌图)

    <题目链接> <转载于 >>> > 题目大意: 给你一个图,让你判断他是不是仙人掌图. 仙人掌图的条件是: 1.是强连通图. 2.每条边在仙人掌图中只属于一个 ...

  5. c#一步一步实现ORM

    本篇适合新手了解学习orm.欢迎指正,交流学习. 现有的优秀的orm有很多. EF:特点是高度自动化,缺点是有点重. Nhibnate:缺点是要写很多的配置. drapper:最快的orm.但是自动化 ...

  6. 浅谈solr

    Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口.用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引:也可以通过Http Get操 ...

  7. mybatis查询语句的背后之封装数据

    转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...

  8. JS实现缓动动画效果

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

  9. Visual Assist X 助手下载

    链接:http://pan.baidu.com/s/1pLUErhT 密码:c6in

  10. SSM项目搭建

    1.新建包 com.javen.controller com.javen.service com.javen.dao com.javen.domain com.javen.mapper 2.log4j ...