Skip to content

Commit 257d5b8

Browse files
authored
Add Apollo data-source extension (#444)
* feat(pkg/datasource):add apollo datasource * fix(pkg/datasource):fix ci problem * feat(.github/workflow):add apollo datasource ci test command * feat(datasource/apollo):change go.mod version to 1.13
1 parent 9eabe9f commit 257d5b8

File tree

6 files changed

+1155
-0
lines changed

6 files changed

+1155
-0
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ jobs:
6363
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
6464
cd ../nacos
6565
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
66+
cd ../apollo
67+
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
6668
cd ../../adapters/echo
6769
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
6870
cd ../gear

pkg/datasource/apollo/apollo.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package apollo
2+
3+
import (
4+
"github.com/alibaba/sentinel-golang/ext/datasource"
5+
"github.com/apolloconfig/agollo/v4"
6+
"github.com/apolloconfig/agollo/v4/component/log"
7+
"github.com/apolloconfig/agollo/v4/env/config"
8+
"github.com/pkg/errors"
9+
)
10+
11+
var (
12+
ErrEmptyKey = errors.New("property key is empty")
13+
ErrMissConfig = errors.New("miss config")
14+
)
15+
16+
type Option func(o *options)
17+
18+
type options struct {
19+
handlers []datasource.PropertyHandler
20+
logger log.LoggerInterface
21+
client *agollo.Client
22+
}
23+
24+
// WithPropertyHandlers set property handlers
25+
func WithPropertyHandlers(handlers ...datasource.PropertyHandler) Option {
26+
return func(o *options) {
27+
o.handlers = handlers
28+
}
29+
}
30+
31+
// WithLogger set apollo logger
32+
func WithLogger(logger log.LoggerInterface) Option {
33+
return func(o *options) {
34+
o.logger = logger
35+
}
36+
}
37+
38+
// apolloDatasource implements datasource.Datasource
39+
type apolloDatasource struct {
40+
datasource.Base
41+
client *agollo.Client
42+
propertyKey string
43+
}
44+
45+
// NewDatasource create apollo datasource
46+
func NewDatasource(conf *config.AppConfig, propertyKey string, opts ...Option) (datasource.DataSource, error) {
47+
if conf == nil {
48+
return nil, ErrMissConfig
49+
}
50+
if propertyKey == "" {
51+
return nil, ErrEmptyKey
52+
}
53+
option := &options{
54+
logger: &log.DefaultLogger{},
55+
}
56+
for _, opt := range opts {
57+
opt(option)
58+
}
59+
agollo.SetLogger(option.logger)
60+
apolloClient, err := agollo.StartWithConfig(func() (*config.AppConfig, error) {
61+
return conf, nil
62+
})
63+
if err != nil {
64+
return nil, err
65+
}
66+
ds := &apolloDatasource{
67+
client: apolloClient,
68+
propertyKey: propertyKey,
69+
}
70+
for _, handler := range option.handlers {
71+
ds.AddPropertyHandler(handler)
72+
}
73+
return ds, nil
74+
}
75+
76+
func (a *apolloDatasource) ReadSource() ([]byte, error) {
77+
value := a.client.GetValue(a.propertyKey)
78+
return []byte(value), nil
79+
}
80+
81+
func (a *apolloDatasource) Initialize() error {
82+
source, err := a.ReadSource()
83+
if err != nil {
84+
return err
85+
}
86+
a.handle(source)
87+
listener := &customChangeListener{
88+
ds: a,
89+
}
90+
a.client.AddChangeListener(listener)
91+
return nil
92+
}
93+
94+
func (a *apolloDatasource) Close() error {
95+
return nil
96+
}
97+
98+
func (a *apolloDatasource) handle(source []byte) {
99+
err := a.Handle(source)
100+
if err != nil {
101+
log.Errorf("update config err: %s", err.Error())
102+
}
103+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package apollo
2+
3+
import (
4+
"testing"
5+
6+
"github.com/apolloconfig/agollo/v4/env/config"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// must run an apollo server before test
11+
// this would be failed for CICD test. so it's disabled for now
12+
//func TestConfig(t *testing.T) {
13+
//
14+
// var resultConfig string
15+
//
16+
// propertyHandler := datasource2.NewDefaultPropertyHandler(
17+
// func(src []byte) (interface{}, error) {
18+
// return string(src), nil
19+
// },
20+
// func(data interface{}) error {
21+
// s := data.(string)
22+
// fmt.Println(s)
23+
// resultConfig = s
24+
// return nil
25+
// },
26+
// )
27+
// c := &config.AppConfig{
28+
// AppID: "SampleApp",
29+
// Cluster: "DEV",
30+
// IP: "http://localhost:8080",
31+
// NamespaceName: "application",
32+
// IsBackupConfig: true,
33+
// Secret: "1dc9532d02cd47f0bb26ee39d614ef8d",
34+
// }
35+
// datasource, err := NewDatasource(
36+
// c, "timeout",
37+
// WithLogger(&log.DefaultLogger{}),
38+
// WithPropertyHandlers(propertyHandler),
39+
// )
40+
// assert.Nil(t, err)
41+
// err = datasource.Initialize()
42+
// assert.Nil(t, err)
43+
// assert.Equal(t, "123", resultConfig)
44+
// select {}
45+
//}
46+
47+
func TestEmptyKey(t *testing.T) {
48+
c := &config.AppConfig{
49+
AppID: "SampleApp",
50+
Cluster: "DEV",
51+
IP: "http://localhost:8080",
52+
NamespaceName: "application",
53+
IsBackupConfig: true,
54+
Secret: "1dc9532d02cd47f0bb26ee39d614ef8d",
55+
}
56+
_, err := NewDatasource(c, "")
57+
assert.Equal(t, ErrEmptyKey, err)
58+
}
59+
60+
func TestMissConfig(t *testing.T) {
61+
_, err := NewDatasource(nil, "test")
62+
assert.Equal(t, ErrMissConfig, err)
63+
}

pkg/datasource/apollo/go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/alibaba/sentinel-golang/pkg/datasource/apollo
2+
3+
go 1.13
4+
5+
replace github.com/alibaba/sentinel-golang => ../../../
6+
7+
require (
8+
github.com/alibaba/sentinel-golang v1.0.3
9+
github.com/apolloconfig/agollo/v4 v4.0.9
10+
github.com/pkg/errors v0.9.1
11+
github.com/stretchr/testify v1.7.0
12+
)
13+
14+
require (
15+
github.com/spf13/viper v1.9.0 // indirect
16+
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
17+
golang.org/x/text v0.3.7 // indirect
18+
gopkg.in/ini.v1 v1.64.0 // indirect
19+
)

0 commit comments

Comments
 (0)