Skip to content

Commit

Permalink
add consul config center
Browse files Browse the repository at this point in the history
  • Loading branch information
dobyte committed Nov 6, 2023
1 parent b9b9e35 commit b375da6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
17 changes: 11 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package config_test

import (
"context"
"github.com/dobyte/due/config/etcd/v2"
"github.com/dobyte/due/v2/config"
"github.com/dobyte/due/v2/config/file"
"testing"
"time"
)

func init() {
source := file.NewSource(file.WithMode(config.ReadWrite))
config.SetConfigurator(config.NewConfigurator(config.WithSources(source)))
}

func TestWatch(t *testing.T) {
ticker1 := time.NewTicker(2 * time.Second)
ticker2 := time.After(time.Minute)
Expand All @@ -26,8 +31,8 @@ func TestWatch(t *testing.T) {

func TestLoad(t *testing.T) {
ctx := context.Background()
file := "config.json"
c, err := config.Load(ctx, etcd.Name, file)
filename := "config.json"
c, err := config.Load(ctx, file.Name, filename)
if err != nil {
t.Fatal(err)
}
Expand All @@ -40,7 +45,7 @@ func TestLoad(t *testing.T) {

func TestStore(t *testing.T) {
ctx := context.Background()
file := "config.json"
filename := "config.json"
content1 := map[string]interface{}{
"timezone": "Local",
}
Expand All @@ -50,14 +55,14 @@ func TestStore(t *testing.T) {
"pid": "./run/gate.pid",
}

err := config.Store(ctx, etcd.Name, file, content1, true)
err := config.Store(ctx, file.Name, filename, content1, true)
if err != nil {
t.Fatal(err)
}

time.Sleep(5 * time.Second)

err = config.Store(ctx, etcd.Name, file, content2)
err = config.Store(ctx, file.Name, filename, content2)
if err != nil {
t.Fatal(err)
}
Expand Down
75 changes: 75 additions & 0 deletions config/etcd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package etcd_test

import (
"context"
"github.com/dobyte/due/config/etcd/v2"
"github.com/dobyte/due/v2/config"
"testing"
"time"
)

func init() {
source := etcd.NewSource(etcd.WithMode(config.ReadWrite))
config.SetConfigurator(config.NewConfigurator(config.WithSources(source)))
}

func TestWatch(t *testing.T) {
ticker1 := time.NewTicker(2 * time.Second)
ticker2 := time.After(time.Minute)

for {
select {
case <-ticker1.C:
t.Log(config.Get("config.timezone").String())
t.Log(config.Get("config.pid").String())
case <-ticker2:
config.Close()
return
}
}
}

func TestLoad(t *testing.T) {
ctx := context.Background()
file := "config.json"
c, err := config.Load(ctx, etcd.Name, file)
if err != nil {
t.Fatal(err)
}

t.Log(c[0].Name)
t.Log(c[0].Path)
t.Log(c[0].Format)
t.Log(c[0].Content)
}

func TestStore(t *testing.T) {
ctx := context.Background()
file := "config.json"
content1 := map[string]interface{}{
"timezone": "Local",
}

content2 := map[string]interface{}{
"timezone": "UTC",
"pid": "./run/gate.pid",
}

err := config.Store(ctx, etcd.Name, file, content1, true)
if err != nil {
t.Fatal(err)
}

time.Sleep(5 * time.Second)

err = config.Store(ctx, etcd.Name, file, content2)
if err != nil {
t.Fatal(err)
}
}

func BenchmarkGet(b *testing.B) {
for i := 0; i < b.N; i++ {
config.Get("config").Value()
}
}
8 changes: 8 additions & 0 deletions testdata/etc/etc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ timezone = "Local"
path = "/config"
# 读写模式。可选:read-only | read-write,默认为read-only
mode = "read-only"
# consul配置中心
[config.consul]
# 客户端连接地址
addr = "127.0.0.1:8500"
# 路径。默认为/config
path = "/config"
# 读写模式。可选:read-only | read-write,默认为read-only
mode = "read-only"

[cluster]
# 集群网关配置
Expand Down

0 comments on commit b375da6

Please sign in to comment.