Skip to content

配置中心

starjiang edited this page Jul 20, 2020 · 7 revisions

配置中心采用etcd 存储配置,服务启动时,读取远程配置,并持久化到本地

package main

import (
	"flag"
	"fmt"
	"time"

	"github.com/starjiang/easycall"
)

type TConfig struct {
	ThreadNum int
	Name      string
	Port      int
	IsExsit   bool
	FailRate  float32
	UserList  []int64
	UserMap   map[string]int64
	UserMap1  map[string]bool
	UserMap2  map[string]float64
	UserMap3  map[string]string
}

func init() {

}

func main() {

	flag.Parse()
        //连接到配置中心的etcd endpoints,配置中心的配置名为profile
	config := easycall.NewRemoteEasyConfig([]string{"127.0.0.1:2379"}, "profile")

	fmt.Println(config.GetInt64("service.port", 10))//获取配置名

	tconfig := &TConfig{}
	config.GetConfig("easycall.", tconfig) //跟进前缀获取配置结构体
	fmt.Println(config)
	time.Sleep(time.Hour)
}

配置加载,持久化机制

  • 1.系统检查比较远程配置版本,从./conf/${config.name}/version 文件读取本地版本,从etcd /easycall/configs/${config.name}/version 读取远程版本,本地版本小于远程版本,进入下一步,否则进入到第3步
  • 2.从etcd /easycall/configs/${config.name}/data,读取配置,并持久化到本地,命名为./conf/${config.name}/remote
  • 3.读取持久化到本地的配置./conf/${config.name}/remote 如果存在的话
  • 4.读取./conf/${config.name}/local 配置,如果存在的话,local 配置可以覆盖远程配置
  • 5.系统通过定时检查版本变化(60s检查一次),只要本地版本小于远程版本 系统会reload 配置。

配置解析规则

配置样例

#service config
config.name=profile
easycall.userList=10001,1002,10033
easycall.name="xxxxx" #service name
easycall.isExsit=true
easycall.service=
easycall.port=8004
service.port=1991
#service threadNum
#easycall.threadNum=32
easycall.failRate=1.56
easycall.userMap1=starjiang:false,starjiang1:true
easycall.userMap2=starjiang:1.55,starjiang1:1.2222
easycall.userMap3=starjiang:hello,starjiang1:world
easycall.userMap=starjiang:10001,starjiang1:1002,starjiang3:10004

Clone this wiki locally