Skip to content

Commit

Permalink
更新配置
Browse files Browse the repository at this point in the history
  • Loading branch information
hlf513 committed Dec 28, 2019
1 parent 153a9f7 commit fb9f315
Show file tree
Hide file tree
Showing 22 changed files with 360 additions and 162 deletions.
30 changes: 18 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
var (
// c 初始化配置器
c = &configurator{}
// initialize micro config 是否初始化
initialize bool
)

// Configurator 配置器接口
type Configurator interface {
// Init 初始化 micro config,并监控改动
Init(source ...source.Source) error
Init(configs []func(config []byte) error, source ...source.Source) error
// Get 实时读取配置信息并解析
Get(name []string, config interface{}) error
}
Expand All @@ -30,12 +28,7 @@ type configurator struct {
}

// Init 初始化 micro config,并监控改动
func (c *configurator) Init(source ...source.Source) error {
// 防止重复初始化
if initialize {
log.Info("[initialise config]: initialised")
return nil
}
func (c *configurator) Init(configs []func(config []byte) error, source ...source.Source) error {
c.conf = config.NewConfig()
// 加载配置
if err := c.conf.Load(source...); err != nil {
Expand All @@ -48,15 +41,28 @@ func (c *configurator) Init(source ...source.Source) error {
log.Fatal("[initialise config] watch error:", err.Error())
}
for {
if _, err := w.Next(); err != nil {
v, err := w.Next()
if err != nil {
log.Fatalf("[initialise config] watch next error,%s", err)
return
}
// log.Printf("config was changed, %s", string(v.Bytes()))
// log.Infof("config was changed, %s", string(v.Bytes()))
// 更新配置
modify := v.Bytes()
for _, f := range configs {
if err := f(modify); err != nil {
log.Warn("[update config] ", err.Error())
}
}
}
}()

initialize = true
// 读取配置
for _, f := range configs {
if err := f(nil); err != nil {
log.Fatal("[initialise config] ", err.Error())
}
}

return nil
}
Expand Down
44 changes: 36 additions & 8 deletions config/etcd/config.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
package etcd

import (
"encoding/json"
"sync"
"time"

"github.com/hlf513/go-micro-pkg/config"
)

type Etcd struct {
// etcd 定义配置项
type etcd struct {
Host []string
Timeout time.Duration
}

// etcd 初始化
var etcd = &Etcd{}
// conf 定义更新配置
type conf struct {
Etcd *etcd
}

var (
etcdConf = &conf{}
s sync.RWMutex
)

// GetConf 读取配置
func GetConf() *etcd {
s.RLock()
defer s.RUnlock()

// GetEtcdConf 读取配置
func GetEtcdConf() (*Etcd, error) {
if err := config.GetConfigurator().Get([]string{"etcd"}, etcd); err != nil {
return nil, err
return etcdConf.Etcd
}

// SetConf 更新配置
func SetConf(c []byte) error {
s.Lock()
defer s.Unlock()

if c == nil {
if err := config.GetConfigurator().Get([]string{"etcd"}, &etcdConf.Etcd); err != nil {
return err
}
} else {
if err := json.Unmarshal(c, etcdConf); err != nil {
return err
}
}
return etcd, nil

return nil
}
6 changes: 1 addition & 5 deletions config/etcd/connect.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package etcd

import (
"github.com/micro/go-micro/util/log"
"go.etcd.io/etcd/clientv3"
)

// Client 创建 etcd client
func Connect() (*clientv3.Client, error) {
etcdConf, err := GetEtcdConf()
if err != nil {
log.Fatal("[etcd connect] ", err.Error())
}
etcdConf := GetConf()
client, err := clientv3.New(clientv3.Config{
Endpoints: etcdConf.Host,
})
Expand Down
7 changes: 1 addition & 6 deletions config/etcd/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@ import (
"time"

"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/util/log"
"github.com/micro/go-plugins/registry/etcdv3"
)

// DefaultRegistry 注册 etcd
func DefaultRegistry() registry.Registry {
conf, err := GetEtcdConf()
if err != nil {
log.Fatal("[micro registry etcd]", err.Error())
}

conf := GetConf()
return etcdv3.NewRegistry(func(op *registry.Options) {
op.Addrs = conf.Host
op.Timeout = conf.Timeout * time.Second
Expand Down
49 changes: 40 additions & 9 deletions config/jaeger/config.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
package jaeger

import "github.com/hlf513/go-micro-pkg/config"
import (
"encoding/json"
"sync"

type Jaeger struct {
"github.com/hlf513/go-micro-pkg/config"
)

// jaeger 定义配置项
type jaeger struct {
Name string `json:"name"`
Address string `json:"address"`
}

// j 初始化
var j = &Jaeger{}
// conf 定义更新配置
type conf struct {
Jaeger *jaeger
}

var (
jaegerConf = &conf{}
s sync.RWMutex
)

// GetConf 读取配置
func GetConf() *jaeger {
s.RLock()
defer s.RUnlock()

return jaegerConf.Jaeger
}

// SetConf 更新配置
func SetConf(c []byte) error {
s.Lock()
defer s.Unlock()

// GetJaegerConf 读取配置
func GetJaegerConf() (*Jaeger, error) {
if err := config.GetConfigurator().Get([]string{"jaeger"}, j); err != nil {
return nil, err
if c == nil {
if err := config.GetConfigurator().Get([]string{"jaeger"}, &jaegerConf.Jaeger); err != nil {
return err
}
} else {
if err := json.Unmarshal(c, jaegerConf); err != nil {
return err
}
}
return j, nil

return nil
}
14 changes: 5 additions & 9 deletions config/jaeger/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@ import (

"github.com/micro/go-micro/util/log"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
client "github.com/uber/jaeger-client-go"
)

// Connect 创建一个jaeger Tracer
func Connect() (opentracing.Tracer, io.Closer) {
conf, err := GetJaegerConf()
if err != nil {
log.Fatal("[jaeger connect]", err.Error())
}

sender, err := jaeger.NewUDPTransport(conf.Address, 0)
conf := GetConf()
sender, err := client.NewUDPTransport(conf.Address, 0)
if err != nil {
log.Fatal("[jaeger connect] connect was failed", err.Error())
}

var tracer, closer = jaeger.NewTracer(
var tracer, closer = client.NewTracer(
conf.Name,
GetSampler(),
jaeger.NewRemoteReporter(sender),
client.NewRemoteReporter(sender),
)

opentracing.SetGlobalTracer(tracer)
Expand Down
10 changes: 5 additions & 5 deletions config/jaeger/sampler.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package jaeger

import "github.com/uber/jaeger-client-go"
import client "github.com/uber/jaeger-client-go"

// sampler 采样率
var sampler jaeger.Sampler
var sampler client.Sampler

// SetSampler 设置采样率
func SetSampler(s jaeger.Sampler) {
func SetSampler(s client.Sampler) {
sampler = s
}

// GetSampler 获取采样率
func GetSampler() jaeger.Sampler {
func GetSampler() client.Sampler {
if sampler == nil {
return jaeger.NewConstSampler(true) // 全量追踪
return client.NewConstSampler(true) // 全量追踪
}

return sampler
Expand Down
4 changes: 2 additions & 2 deletions config/jaeger/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"context"

"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
client "github.com/uber/jaeger-client-go"
)

// GetTraceId 获取 trace id
func GetTraceId(ctx context.Context) string {
span := opentracing.SpanFromContext(ctx)
if span != nil {
return span.Context().(jaeger.SpanContext).TraceID().String()
return span.Context().(client.SpanContext).TraceID().String()
}

return ""
Expand Down
45 changes: 36 additions & 9 deletions config/mysql/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package mysql

import (
"encoding/json"
"fmt"
"sync"
"time"

"github.com/hlf513/go-micro-pkg/config"
)

type DB struct {
// DB 定义配置项
type db struct {
Type string
Host string
Username string
Expand All @@ -18,15 +22,38 @@ type DB struct {
Debug bool
}

// dbs 初始化
var dbs = make(map[string]DB)
// conf 定义更新配置
type conf struct {
DB map[string]db `json:"db"`
}

var (
dbConf = &conf{}
s sync.RWMutex
)

// GetConf 读取配置
func GetConf() map[string]db {
s.RLock()
defer s.RUnlock()

return dbConf.DB
}

// SetConf 更新配置
func SetConf(c []byte) error {
s.Lock()
defer s.Unlock()

// GetDBs 读取配置
func GetDBConf() (map[string]DB, error) {
err := config.GetConfigurator().Get([]string{"db"}, &dbs)
if err != nil {
return nil, err
if c == nil {
if err := config.GetConfigurator().Get([]string{"db"}, &dbConf.DB); err != nil {
return err
}
} else {
if err := json.Unmarshal(c, dbConf); err != nil {
return err
}
}

return dbs, nil
return nil
}
6 changes: 1 addition & 5 deletions config/mysql/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ var dbInstance = make(map[string]*gorm.DB)

// Connect 连接 MySQL
func Connect() {
configs, err := GetDBConf()
if err != nil {
log.Fatal("[mysql connect] ", err.Error())
}

configs := GetConf()
for name, dbCfg := range configs {
db, err := gorm.Open(
"mysql",
Expand Down
Loading

0 comments on commit fb9f315

Please sign in to comment.