Skip to content

Commit eb19fcf

Browse files
committed
做配置文件的热更
1 parent 40a374d commit eb19fcf

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

micro_v2/client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func main() {
2222
var opss client.CallOption = func(o *client.CallOptions) {
2323
o.RequestTimeout = time.Second * 30
2424
o.DialTimeout = time.Second * 30
25+
o.Retries = 3
2526
}
26-
2727
info, err := agent.RpcUserInfo(context.TODO(), &test_agent.ReqMsg{
2828
UserName: "test user",
2929
}, opss)

micro_v2/config/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/micro/go-micro/v2/config"
6+
"github.com/micro/go-micro/v2/config/encoder/toml"
7+
"github.com/micro/go-micro/v2/config/source"
8+
"github.com/micro/go-micro/v2/config/source/etcd"
9+
)
10+
11+
func main() {
12+
conf, err := config.NewConfig()
13+
if err != nil {
14+
panic(err)
15+
}
16+
t := toml.NewEncoder()
17+
sou := etcd.NewSource(
18+
etcd.WithAddress("192.168.1.86:2379"),
19+
etcd.WithPrefix("/conf/"),
20+
etcd.StripPrefix(true),
21+
source.WithEncoder(t),
22+
)
23+
if err = conf.Load(sou); err != nil {
24+
panic(err)
25+
}
26+
fmt.Println("读取到配置", string(conf.Get("test", "server").Bytes()))
27+
wath, err := conf.Watch("test", "server")
28+
if err != nil {
29+
panic(err)
30+
}
31+
defer wath.Stop()
32+
for {
33+
val, err := wath.Next()
34+
if err != nil {
35+
panic(err)
36+
}
37+
fmt.Println(string(val.Bytes()))
38+
}
39+
}

0 commit comments

Comments
 (0)