Skip to content

Commit

Permalink
feat(contrib): add dynamic_conf
Browse files Browse the repository at this point in the history
  • Loading branch information
jaronnie committed Dec 11, 2024
1 parent e027bd3 commit 55d9577
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 44 deletions.
59 changes: 59 additions & 0 deletions dynamic_conf/fsnotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package dynamic_conf

import (
"os"

"github.com/fsnotify/fsnotify"
"github.com/zeromicro/go-zero/core/configcenter/subscriber"
"github.com/zeromicro/go-zero/core/logx"
)

var _ subscriber.Subscriber = (*FsNotify)(nil)

type FsNotify struct {
Path string `json:"path"`
*fsnotify.Watcher
}

func NewFsNotify(path string) (*FsNotify, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}

return &FsNotify{
Path: path,
Watcher: watcher,
}, nil
}

func (lf *FsNotify) AddListener(listener func()) error {
go func() {
for {
select {
case event, ok := <-lf.Watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
listener()
}
case err, ok := <-lf.Watcher.Errors:
if !ok {
return
}
logx.Errorf("error: %v", err)
}
}
}()

if err := lf.Watcher.Add(lf.Path); err != nil {
return err
}
return nil
}

func (lf *FsNotify) Value() (string, error) {
file, err := os.ReadFile(lf.Path)
return string(file), err
}
43 changes: 43 additions & 0 deletions dynamic_conf/fsnotify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dynamic_conf

import (
"testing"

configurator "github.com/zeromicro/go-zero/core/configcenter"
"github.com/zeromicro/go-zero/core/logx"
)

type TestSt struct {
Name string `json:","`
}

func TestLocalFsNotify(t *testing.T) {
logx.MustSetup(logx.LogConf{
Level: "info",
Encoding: "plain",
})
ss, err := NewFsNotify("testdata/etc.yaml")
if err != nil {
panic(err)
}
cc := configurator.MustNewConfigCenter[TestSt](configurator.Config{
Type: "yaml", // 配置值类型:json,yaml,toml
}, ss)

v, err := cc.GetConfig()
if err != nil {
panic(err)
}
println(v.Name)

// 如果想监听配置变化,可以添加 listener
cc.AddListener(func() {
v, err := cc.GetConfig()
if err != nil {
panic(err)
}
println(v.Name)
})

select {}
}
1 change: 1 addition & 0 deletions dynamic_conf/testdata/etc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Name: jzero
44 changes: 29 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ toolchain go1.22.3
require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/eddieowens/opts v0.1.0
github.com/fsnotify/fsnotify v1.8.0
github.com/guregu/null/v5 v5.0.0
github.com/huandu/go-assert v1.1.6
github.com/huandu/go-sqlbuilder v1.28.0
github.com/pkg/errors v0.9.1
github.com/spf13/cast v1.3.1
github.com/zeromicro/go-zero v1.7.0
github.com/spf13/cast v1.5.1
github.com/zeromicro/go-zero v1.7.4
)

require (
Expand All @@ -22,30 +23,40 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
go.etcd.io/etcd/api/v3 v3.5.15 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.15 // indirect
go.etcd.io/etcd/client/v3 v3.5.15 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.24.0 // indirect
Expand All @@ -57,14 +68,17 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
google.golang.org/protobuf v1.35.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 55d9577

Please sign in to comment.