Skip to content

Commit b79e3b6

Browse files
committed
插拔型配置中心支持ETCD,K8S,Apollo
1 parent 2aa1002 commit b79e3b6

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,60 @@
107107
{"level":"debug","ts":1639453664.7402549,"caller":"example/main.go:33","msg":"test","traceId":"68867b89-c949-45a4-b325-86866c9f869a"}
108108
```
109109

110+
### [hconfig 插拔式配置读取工具可动态加载](https://github.com/hwholiday/learning_tools/tree/master/hconfig)
110111

112+
- 支持 etcd
113+
- 支持 kubernetes
114+
- 支持 apollo
115+
116+
#### hconfig 配置不同的源
117+
118+
```base
119+
//etcd
120+
cli, err := clientv3.New(clientv3.Config{
121+
Endpoints: []string{"127.0.0.1:2379"},})
122+
123+
c, err := etcd.NewEtcdConfig(cli,
124+
etcd.WithRoot("/hconf"),
125+
etcd.WithPaths("app", "mysql"))
126+
127+
//kubernetes
128+
cli, err := kubernetes.NewK8sClientset(
129+
kubernetes.KubeConfigPath("/home/app/conf/kube_config/local_kube.yaml"))
130+
131+
c, err := kubernetes.NewKubernetesConfig(cli,
132+
kubernetes.WithNamespace("im"),
133+
kubernetes.WithPaths("im-test-conf", "im-test-conf2"))
134+
135+
//apollo
136+
c, err := apollo.NewApolloConfig(
137+
apollo.WithAppid("test"),
138+
apollo.WithNamespace("test.yaml"),
139+
apollo.WithAddr("http://127.0.0.1:32001"),
140+
apollo.WithCluster("dev"),
141+
)
142+
```
143+
144+
145+
#### hconfig 使用
146+
147+
```base
148+
conf, err := NewHConfig(
149+
WithDataSource(c),//c 不同的源
150+
)
151+
152+
// 加载配置
153+
conf.Load()
154+
155+
//读取配置
156+
val, err := conf.GetPath("test.yaml")
157+
t.Logf("val %+v\n", val.String())
158+
159+
//监听配置变化
160+
conf.WatchPaths(func(path string, v HVal) {
161+
t.Logf("path %s val %+v\n", path, v.String())
162+
})
163+
```
111164

112165
# go_push 一个实用的消息推送服务
113166

hconfig/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
### [hconfig 插拔式配置读取工具可动态加载](https://github.com/hwholiday/learning_tools/tree/master/hconfig)
2+
3+
- 支持 etcd
4+
- 支持 kubernetes
5+
- 支持 apollo
6+
7+
#### hconfig 配置不同的源
8+
9+
```base
10+
//etcd
11+
cli, err := clientv3.New(clientv3.Config{
12+
Endpoints: []string{"127.0.0.1:2379"},})
13+
14+
c, err := etcd.NewEtcdConfig(cli,
15+
etcd.WithRoot("/hconf"),
16+
etcd.WithPaths("app", "mysql"))
17+
18+
//kubernetes
19+
cli, err := kubernetes.NewK8sClientset(
20+
kubernetes.KubeConfigPath("/home/app/conf/kube_config/local_kube.yaml"))
21+
22+
c, err := kubernetes.NewKubernetesConfig(cli,
23+
kubernetes.WithNamespace("im"),
24+
kubernetes.WithPaths("im-test-conf", "im-test-conf2"))
25+
26+
//apollo
27+
c, err := apollo.NewApolloConfig(
28+
apollo.WithAppid("test"),
29+
apollo.WithNamespace("test.yaml"),
30+
apollo.WithAddr("http://127.0.0.1:32001"),
31+
apollo.WithCluster("dev"),
32+
)
33+
```
34+
35+
36+
#### hconfig 使用
37+
38+
```base
39+
conf, err := NewHConfig(
40+
WithDataSource(c),//c 不同的源
41+
)
42+
43+
// 加载配置
44+
conf.Load()
45+
46+
//读取配置
47+
val, err := conf.GetPath("test.yaml")
48+
t.Logf("val %+v\n", val.String())
49+
50+
//监听配置变化
51+
conf.WatchPaths(func(path string, v HVal) {
52+
t.Logf("path %s val %+v\n", path, v.String())
53+
})
54+
```

hconfig/etcd/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import clientv3 "go.etcd.io/etcd/client/v3"
77

88
func TestNewEtcdConfig(t *testing.T) {
99
cli, err := clientv3.New(clientv3.Config{
10-
Endpoints: []string{"172.12.12.165:2379"},
10+
Endpoints: []string{"127.0.0.1:2379"},
1111
})
1212
if err != nil {
1313
t.Error(err)

hconfig/hconfig_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestNewHConfig_ETCD(t *testing.T) {
1212
cli, err := clientv3.New(clientv3.Config{
13-
Endpoints: []string{"172.12.12.165:2379"},
13+
Endpoints: []string{"127.0.0.1:2379"},
1414
})
1515
if err != nil {
1616
t.Error(err)

0 commit comments

Comments
 (0)