Skip to content

Commit 4f8aab3

Browse files
liuxingwangliuxingwang
authored andcommitted
[feat] 1. 优化了DefaultNamespace的使用体验,注释和测试用例
1 parent af2fc41 commit 4f8aab3

4 files changed

Lines changed: 153 additions & 26 deletions

File tree

agollo.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,7 @@ func (a *agollo) reloadNamespace(namespace string) (status int, conf Configurati
229229
}
230230

231231
func (a *agollo) Get(key string, opts ...GetOption) string {
232-
getOpts := newGetOptions(
233-
append(
234-
[]GetOption{
235-
WithNamespace(a.opts.DefaultNamespace),
236-
},
237-
opts...,
238-
)...,
239-
)
232+
getOpts := a.opts.newGetOptions(opts...)
240233

241234
val, found := a.GetNameSpace(getOpts.Namespace)[key]
242235
if !found {

options.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,26 @@ var (
1616
)
1717

1818
type Options struct {
19-
AppID string // appid
20-
Cluster string // 默认的集群名称,默认:default
21-
DefaultNamespace string // 默认的命名空间,默认:application
22-
PreloadNamespaces []string // 预加载命名空间,默认:application
23-
ApolloClient ApolloClient // apollo HTTP api实现
24-
Logger Logger // 需要日志需要设置实现,或者注入有效的io.Writer,默认: ioutil.Discard
25-
AutoFetchOnCacheMiss bool // 自动获取非预设以外的namespace的配置,默认:false
26-
LongPollerInterval time.Duration // 轮训间隔时间,默认:1s
27-
BackupFile string // 备份文件存放地址,默认:.agollo
28-
FailTolerantOnBackupExists bool // 服务器连接失败时允许读取备份,默认:false
29-
Balancer Balancer // ConfigServer负载均衡
30-
EnableSLB bool // 启用ConfigServer负载均衡
31-
RefreshIntervalInSecond time.Duration // ConfigServer刷新间隔
32-
ClientOptions []ApolloClientOption
19+
AppID string // appid
20+
Cluster string // 默认的集群名称,默认:default
21+
DefaultNamespace string // Get时默认使用的命名空间,如果设置了该值,而不在PreloadNamespaces中,默认也会加入初始化逻辑中
22+
PreloadNamespaces []string // 预加载命名空间,默认:为空
23+
ApolloClient ApolloClient // apollo HTTP api实现
24+
Logger Logger // 日志实现类,可以设置自定义实现或者通过NewLogger()创建并设置有效的io.Writer,默认: ioutil.Discard
25+
AutoFetchOnCacheMiss bool // 自动获取非预设以外的Namespace的配置,默认:false
26+
LongPollerInterval time.Duration // 轮训间隔时间,默认:1s
27+
BackupFile string // 备份文件存放地址,默认:.agollo
28+
FailTolerantOnBackupExists bool // 服务器连接失败时允许读取备份,默认:false
29+
Balancer Balancer // ConfigServer负载均衡
30+
EnableSLB bool // 启用ConfigServer负载均衡
31+
RefreshIntervalInSecond time.Duration // ConfigServer刷新间隔
32+
ClientOptions []ApolloClientOption // 设置apollo HTTP api的配置项
3333
}
3434

3535
func newOptions(configServerURL, appID string, opts ...Option) (Options, error) {
3636
var options = Options{
3737
AppID: appID,
3838
Cluster: defaultCluster,
39-
DefaultNamespace: defaultNamespace,
4039
ApolloClient: NewApolloClient(),
4140
Logger: NewLogger(),
4241
AutoFetchOnCacheMiss: defaultAutoFetchOnCacheMiss,
@@ -68,6 +67,13 @@ func newOptions(configServerURL, appID string, opts ...Option) (Options, error)
6867
options.Balancer = b
6968
}
7069

70+
if options.DefaultNamespace != "" &&
71+
!stringInSlice(options.DefaultNamespace, options.PreloadNamespaces) {
72+
73+
options.PreloadNamespaces = append(
74+
options.PreloadNamespaces, options.DefaultNamespace)
75+
}
76+
7177
return options, nil
7278
}
7379

@@ -177,22 +183,31 @@ func AccessKey(accessKey string) Option {
177183
}
178184
}
179185

180-
func WithClientOptinons(opts ...ApolloClientOption) Option {
186+
func WithClientOptions(opts ...ApolloClientOption) Option {
181187
return func(o *Options) {
182188
o.ClientOptions = append(o.ClientOptions, opts...)
183189
}
184190
}
185191

186192
type GetOptions struct {
193+
// Get时,如果key不存在将返回此值
187194
DefaultValue string
188-
Namespace string
195+
196+
// Get时,显示的指定需要获取那个Namespace中的key。非空情况下,优先级顺序为:
197+
// GetOptions.Namespace > Options.DefaultNamespace > "application"
198+
Namespace string
189199
}
190200

191-
func newGetOptions(opts ...GetOption) GetOptions {
201+
func (o Options) newGetOptions(opts ...GetOption) GetOptions {
192202
var getOpts GetOptions
193203
for _, opt := range opts {
194204
opt(&getOpts)
195205
}
206+
207+
if getOpts.Namespace == "" {
208+
getOpts.Namespace = nonEmptyString(defaultNamespace, o.DefaultNamespace)
209+
}
210+
196211
return getOpts
197212
}
198213

options_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package agollo
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestOptions(t *testing.T) {
11+
var (
12+
configServerURL = "localhost:8080"
13+
appID = "SampleApp"
14+
)
15+
var tests = []struct {
16+
Options []Option
17+
Check func(Options)
18+
}{
19+
{
20+
[]Option{},
21+
func(opts Options) {
22+
assert.Equal(t, appID, opts.AppID)
23+
assert.Equal(t, defaultCluster, opts.Cluster)
24+
assert.Equal(t, defaultAutoFetchOnCacheMiss, opts.AutoFetchOnCacheMiss)
25+
assert.Equal(t, defaultLongPollInterval, opts.LongPollerInterval)
26+
assert.Equal(t, defaultBackupFile, opts.BackupFile)
27+
assert.Equal(t, defaultFailTolerantOnBackupExists, opts.FailTolerantOnBackupExists)
28+
assert.Equal(t, defaultEnableSLB, opts.EnableSLB)
29+
assert.NotNil(t, opts.Logger)
30+
assert.NotNil(t, opts.ApolloClient)
31+
assert.NotNil(t, opts.Balancer)
32+
assert.Empty(t, opts.PreloadNamespaces)
33+
assert.Empty(t, opts.DefaultNamespace)
34+
getOpts := opts.newGetOptions()
35+
assert.Equal(t, "application", getOpts.Namespace)
36+
getOpts = opts.newGetOptions(WithNamespace("customize_namespace"))
37+
assert.Equal(t, "customize_namespace", getOpts.Namespace)
38+
ac := &apolloClient{}
39+
ac.Apply(opts.ClientOptions...)
40+
assert.Empty(t, ac.AccessKey)
41+
},
42+
},
43+
{
44+
[]Option{
45+
Cluster("test_cluster"),
46+
DefaultNamespace("default_namespace"),
47+
PreloadNamespaces("preload_namespace"),
48+
AutoFetchOnCacheMiss(),
49+
LongPollerInterval(time.Second * 30),
50+
BackupFile("test_backup"),
51+
FailTolerantOnBackupExists(),
52+
AccessKey("test_access_key"),
53+
},
54+
func(opts Options) {
55+
assert.Equal(t, "test_cluster", opts.Cluster)
56+
assert.Equal(t, []string{"preload_namespace", "default_namespace"}, opts.PreloadNamespaces)
57+
assert.Equal(t, "default_namespace", opts.DefaultNamespace)
58+
getOpts := opts.newGetOptions()
59+
assert.Equal(t, "default_namespace", getOpts.Namespace)
60+
getOpts = opts.newGetOptions(WithNamespace("customize_namespace"))
61+
assert.Equal(t, "customize_namespace", getOpts.Namespace)
62+
assert.Equal(t, true, opts.AutoFetchOnCacheMiss)
63+
assert.Equal(t, time.Second*30, opts.LongPollerInterval)
64+
assert.Equal(t, "test_backup", opts.BackupFile)
65+
assert.Equal(t, true, opts.FailTolerantOnBackupExists)
66+
ac := &apolloClient{}
67+
ac.Apply(opts.ClientOptions...)
68+
assert.Equal(t, "test_access_key", ac.AccessKey)
69+
},
70+
},
71+
{
72+
[]Option{
73+
EnableSLB(true),
74+
WithApolloClient(&mockApolloClient{
75+
getConfigServers: func(metaServerURL, appID string) (i int, servers []ConfigServer, err error) {
76+
return 200,
77+
[]ConfigServer{
78+
ConfigServer{
79+
AppName: "test",
80+
InstanceID: "test",
81+
HomePageURL: "http://localhost:8080",
82+
},
83+
},
84+
nil
85+
},
86+
}),
87+
},
88+
func(opts Options) {
89+
assert.Equal(t, true, opts.EnableSLB)
90+
},
91+
},
92+
}
93+
94+
for _, test := range tests {
95+
opts, err := newOptions(configServerURL, appID, test.Options...)
96+
if err != nil {
97+
assert.Nil(t, err)
98+
}
99+
test.Check(opts)
100+
}
101+
}

util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,21 @@ func splitCommaSeparatedURL(s string) []string {
1818

1919
return urls
2020
}
21+
22+
func stringInSlice(t string, ss []string) bool {
23+
for _, s := range ss {
24+
if s == t {
25+
return true
26+
}
27+
}
28+
return false
29+
}
30+
31+
func nonEmptyString(def string, ss ...string) string {
32+
for _, s := range ss {
33+
if s != "" {
34+
return s
35+
}
36+
}
37+
return def
38+
}

0 commit comments

Comments
 (0)