-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Name: jzero |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.