资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

go语言读取配置文件,go 读取配置文件

如何用Go语言打造一个高性能MySQL Proxy

读取配置文件并启动,在配置文件中设置的监听端口监听客户端请求。

站在用户的角度思考问题,与客户深入沟通,找到丰县网站设计与丰县网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站制作、网站建设、企业官网、英文网站、手机端网站、网站推广、域名注册网站空间、企业邮箱。业务覆盖丰县地区。

收到客户端连接请求后,启动一个goroutine单独处理该请求。

首选进行登录验证,验证过程完全兼容MySQL认证协议,由于用户名和密码在配置文件中已经设置好,所以可以利用该信息验证连接请求是否合法。

当用户名和密码都正确时,转入下面的步骤,否则返回出错信息给客户端。

认证通过后,客户端发送SQL语句。

kingshard对客户端发送过来的SQL语句,进行词法和语义分析,识别出SQL的类型和生成SQL的路由计划。如果有必要还会改写SQL,然后转发到相应的DB。也有可能不做词法和语义分析直接转发到相应的后端DB。如果转发SQL是分表且跨多个DB,则每个DB对应启动一个goroutine发送SQL和接收该DB返回的结果。

接收并合并结果,然后转发给客户端。

Go语言-一个简单的MockServer实现

用于在开发阶段,服务端接口没有正式完成之前,模拟接口请求,便于客户端测试。

建立一个文件夹(Config),放置所有的接口文件

将接口按如下json格式存储到成文件,一个接口对应一个文件

如登录接口:./Config/login.json

定义接口

读取所有的接口配置文件

C语言操作yaml配置文件通用操作工具

在go语言中使用viper之类的库很方便的处理yaml配置文件,但是在c语言中就比较麻烦,经过一番思索和借助强大的github,发现了一个libyaml c库,但是网上的例子都比较麻烦,而且比较繁琐,就想法作了一个相对比较容易配置的解析应用,可以简单地类似viper 的模式进行配置实现不同的配置文件读取。如你的配置文件很复杂请按格式修改KeyValue 全局变量,欢迎大家一起完善

库请自行下载 GitHub - yaml/libyaml: Canonical source repository for LibYAML

直接上代码

yaml示例文件

%YAML 1.1

---

mqtt:

subtopic: "Control/#"

pubtopic: "bbt"

qos: 1

serveraddress: "tcp://192.168.0.25:1883"

clientid: "kvm_test"

writelog: false

writetodisk: false

outputfile: "./receivedMessages.txt"

hearttime: 30

#ifndef __CONFIG_H__

#define __CONFIG_H__

#ifdef __cplusplus

extern "C" {

#endif

/************************/

/* Minimum YAML version */

/************************/

#define YAML_VERSION_MAJOR 1

#define YAML_VERSION_MINOR 1

#define STRUCT_TYPE_NAME 100

#define INT_TYPE_NAME 101

#define STRING_TYPE_NAME 102

#define BOOL_TYPE_NAME 103

#define FLOAT_TYPE_NAME 104

#define MAP_TYPE_NAME 105

#define LIST_TYPE_NAME 106

typedef struct{

char *key;

void *value;

int valuetype;

char *parent;

}KeyValue,*pKeyValue;

#ifdef __cplusplus

}

#endif

#endif

#include

#include

#include

#include

#include

#include

#include

#include "config.h"

typedef struct {

char *SUBTOPIC; //string `yaml:"subtopic" mapstructure:"subtopic"` //"topic1"

char *PUBTOPIC; //string `yaml:"pubtopic" mapstructure:"pubtopic"`

int QOS; //byte `yaml:"qos" mapstructure:"qos"` //1

char *SERVERADDRESS; //string `yaml:"serveraddress" mapstructure:"serveraddress"` //= "tcp://mosquitto:1883"

char *CLIENTID; //string `yaml:"clientid" mapstructure:"clientid"` //= "mqtt_subscriber"

int HEARTTIME; //int `yaml:"hearttime" mapstructure:"hearttime"`

// CommandLocalPath string `yam:"commanlocalpath"`

}mqttSection,*pmqttSection;

typedef struct {

mqttSection Mqtt;// `yaml:"mqtt" mapstructure:"mqtt"`

// KVM kvmSection `yaml:"kvm" mapstructure:"kvm"`

}ConfigT;

ConfigT config;

static KeyValue webrtcconfig[]={

{"mqtt",config,STRUCT_TYPE_NAME,NULL},

{"subtopic",(config.Mqtt.SUBTOPIC),STRING_TYPE_NAME,"mqtt"},

{"pubtopic",(config.Mqtt.PUBTOPIC),STRING_TYPE_NAME,"mqtt"},

{"qos",(config.Mqtt.QOS),INT_TYPE_NAME,"mqtt"},

{"serveraddress",(config.Mqtt.SERVERADDRESS),STRING_TYPE_NAME,"mqtt"},

{"clientid",(config.Mqtt.CLIENTID),STRING_TYPE_NAME,"mqtt"},

{"hearttime",(config.Mqtt.HEARTTIME),INT_TYPE_NAME,"mqtt"},

{NULL,NULL,0,NULL},

};

int printConfig(ConfigT * pconfig){

if(pconfig==NULL) return -1;

printf("mqtt:r ");

if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf("subtopic: %sr ",pconfig-Mqtt.SUBTOPIC); }

if(pconfig-Mqtt.SUBTOPIC!=NULL) {printf("pubtopic: %sr ",pconfig-Mqtt.PUBTOPIC); }

printf("qos: %dr ",config.Mqtt.QOS);

if(pconfig-Mqtt.SERVERADDRESS!=NULL) {printf("serveraddress: %sr ",pconfig-Mqtt.SERVERADDRESS); }

if(pconfig-Mqtt.CLIENTID!=NULL) {printf("clientid: %sr ",pconfig-Mqtt.CLIENTID); }

printf("hearttime: %dr ",config.Mqtt.HEARTTIME);

}

int freeConfig(ConfigT * pconfig){

if(pconfig==NULL) return -1;

if(pconfig-Mqtt.SERVERADDRESS!=NULL) {free(pconfig-Mqtt.SERVERADDRESS); }

if(pconfig-Mqtt.CLIENTID!=NULL) {free(pconfig-Mqtt.CLIENTID); }

if(pconfig-Mqtt.SUBTOPIC!=NULL) {free(pconfig-Mqtt.SUBTOPIC); }

}

char currentkey[100];

void getvalue(yaml_event_t event,pKeyValue *ppconfigs){

char *value = (char *)event.data.scalar.value;

pKeyValue pconfig=*ppconfigs;

char *pstringname;

while(pconfig-key!=NULL){

if(currentkey[0]!=0){

if(!strcmp(currentkey,pconfig-key))

{

switch(pconfig-valuetype){

case STRING_TYPE_NAME:

pstringname=strdup(value);

printf("get string value %sr ",pstringname);

*((char**)pconfig-value)=pstringname;

memset(currentkey, 0, sizeof(currentkey));

break;

case INT_TYPE_NAME:

*((int*)(pconfig-value))=atoi(value);

memset(currentkey, 0, sizeof(currentkey));

break;

case BOOL_TYPE_NAME:

if(!strcmp(value,"true")) *((bool*)(pconfig-value))=true;

else *((bool*)(pconfig-value))=false;

memset(currentkey, 0, sizeof(currentkey));

break;

case FLOAT_TYPE_NAME:

*((float*)(pconfig-value))=atof(value);

memset(currentkey, 0, sizeof(currentkey));

break;

case STRUCT_TYPE_NAME:

case MAP_TYPE_NAME:

case LIST_TYPE_NAME:

memset(currentkey, 0, sizeof(currentkey));

strncpy(currentkey,value,strlen(value));

break;

default:

break;

}

break;

}

//continue;

}else{

if(!strcmp(value,pconfig-key)){

strncpy(currentkey,pconfig-key,strlen(pconfig-key));

break;

}

}

pconfig++;

}

}

int Load_YAML_Config( char *yaml_file, KeyValue *(configs[]) )

{

struct stat filecheck;

yaml_parser_t parser;

yaml_event_t event;

bool done = 0;

unsigned char type = 0;

unsigned char sub_type = 0;

if (stat(yaml_file, filecheck) != false )

{

printf("[%s, line %d] Cannot open configuration file '%s'! %s", __FILE__, __LINE__, yaml_file, strerror(errno) );

return -1;

}

FILE *fh = fopen(yaml_file, "r");

if (!yaml_parser_initialize(parser))

{

printf("[%s, line %d] Failed to initialize the libyaml parser. Abort!", __FILE__, __LINE__);

return -1;

}

if (fh == NULL)

{

printf("[%s, line %d] Failed to open the configuration file '%s' Abort!", __FILE__, __LINE__, yaml_file);

return -1;

}

memset(currentkey, 0, sizeof(currentkey));

/* Set input file */

yaml_parser_set_input_file(parser, fh);

while(!done)

{

if (!yaml_parser_parse(parser, event))

{

/* Useful YAML vars: parser.context_mark.line+1, parser.context_mark.column+1, parser.problem, parser.problem_mark.line+1, parser.problem_mark.column+1 */

printf( "[%s, line %d] libyam parse error at line %ld in '%s'", __FILE__, __LINE__, parser.problem_mark.line+1, yaml_file);

}

if ( event.type == YAML_DOCUMENT_START_EVENT )

{

//yaml file first line is version

//%YAML 1.1

//---

yaml_version_directive_t *ver = event.data.document_start.version_directive;

if ( ver == NULL )

{

printf( "[%s, line %d] Invalid configuration file. Configuration must start with "%%YAML 1.1"", __FILE__, __LINE__);

}

int major = ver-major;

int minor = ver-minor;

if (! (major == YAML_VERSION_MAJOR minor == YAML_VERSION_MINOR) )

{

printf( "[%s, line %d] Configuration has a invalid YAML version. Must be 1.1 or above", __FILE__, __LINE__);

return -1;

}

}

else if ( event.type == YAML_STREAM_END_EVENT )

{

done = true;

}

else if ( event.type == YAML_MAPPING_END_EVENT )

{

sub_type = 0;

}

else if ( event.type == YAML_SCALAR_EVENT )

{

getvalue(event,configs);

}

}

return 0;

}

int main(int argc, char *argv[]){

pKeyValue pconfig=webrtcconfig[0];

Load_YAML_Config("../../etc/kvmagent.yml",pconfig);

printConfig(config);

freeConfig(config);

}

go 怎么写入conf配置文件

为了快速声明配置文件中的全局变量而写的封装包,大家笑纳,废话少说,直接上方法。

1.首先,下载包:

1

2

go get "github.com/luckykris/go-utilbox/Env"

go get "github.com/luckykris/go-utilbox/Conf/ReadConf"

2.书写配置文件(例):

vim test.cfg,写入如下配置内容:

1

2

3

4

5

6

7

8

[gms]

port = 2016

[db]

db = mysql

user = root

password = redhat

port = 3306

host = 127.0.0.1

3.写一个test.go来获取配置文件里的配置,并且将所有配置文件内的信息声明全局变量,注意,你需要在go代码里提前定义各项配置的数据类型(string,int目前只支持两类)以及默认值。如下列go代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

package main

import(

"github.com/luckykris/go-utilbox/Env"

"github.com/luckykris/go-utilbox/Conf/ReadConf"

"fmt"

)

func main(){

Init()

}

func Init(){

//定义gms配置区域

gms:=ReadConf.CONFIG{

AREA:"gms",//区域名

CONF:map[string]ReadConf.CONFIGROW{

"port":{TYPE:"int",EXIT:false,DEFAULT:1},//具体配置,

//此条为port配置默认值为1

//类型为int

//注意:如果标明类型是int默认值

//不要加双引号“”不然会被认为 //是字符串.

//false的意思是,当配置文件里读 //取不到这个配置的时候程序退出

},

}

//定义db配置区域

db:=ReadConf.CONFIG{

AREA:"db",//区域名

CONF:map[string]ReadConf.CONFIGROW{

"port":{TYPE:"int",EXIT:false,DEFAULT:3306},

"db":{TYPE:"string",EXIT:false,DEFAULT:"mongo"},

"user":{TYPE:"string",EXIT:false,DEFAULT:"root"},

"password":{TYPE:"string",EXIT:false,DEFAULT:"root"},

"host":{TYPE:"string",EXIT:false,DEFAULT:"localhost"},

},

}

//让程序读取配合文件,并且把需要提取的配置区域变量传入LodConf函数

//这个函数执行之后,所有实现定义的配置就都生成全局变量了。

ReadConf.LoadConf("test.cfg",gms,db)

//以下是对配置文件的全局变量的调用方式,用Env.ENV["配置区域/配置条目"].(类型)

//因为返回的值是interface{}类型的,所以需要自己转换下类型。

fmt.Printf("gms/port:%d\n",Env.ENV["gms/port"].(int))

fmt.Printf("db/port:%d\n",Env.ENV["db/port"].(int))

fmt.Printf("db/db:%s\n",Env.ENV["db/db"].(string))

fmt.Printf("db/user:%s\n",Env.ENV["db/user"].(string))

fmt.Printf("db/password:%s\n",Env.ENV["db/password"].(string))

fmt.Printf("db/host:%s\n",Env.ENV["db/host"].(string))

}

4.以下就是执行此GO程序后的输出结果啦:

1

2

3

4

5

6

7

[root@gbz.test.org GoMonitorServer]# go run test.go

gms/port:2016

db/port:3306

db/db:mysql

db/user:root

db/password:redhat

db/host:127.0.0.1

Go 各种方式加载配置文件

go run 或者 go build后在配置目录的相对路径上执行

假设当前目录如下:

├─config

│ │ main.go

│ │

│ └─file // 配置文件目录

│ config.ini

也就是说无论你是go run或者build后的执行程序,都应该在你执行目录下

有该配置文件路径如file/config.ini

否则就会发生以下错误, 无法读取配置文件

panic: Error:can not read file "./file/config.ini"

使用绝对路径读取配置文件

如果配置文件动态的话, 具体怎么传入 配置文件路径

go-bindata 把配置文件打包进去

5.1 使用远程配置中心去读取配置


本文标题:go语言读取配置文件,go 读取配置文件
标题网址:http://cdkjz.cn/article/dsspgch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220