-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
9eabe9f
commit 257d5b8
Showing
6 changed files
with
1,155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package apollo | ||
|
||
import ( | ||
"github.com/alibaba/sentinel-golang/ext/datasource" | ||
"github.com/apolloconfig/agollo/v4" | ||
"github.com/apolloconfig/agollo/v4/component/log" | ||
"github.com/apolloconfig/agollo/v4/env/config" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
ErrEmptyKey = errors.New("property key is empty") | ||
ErrMissConfig = errors.New("miss config") | ||
) | ||
|
||
type Option func(o *options) | ||
|
||
type options struct { | ||
handlers []datasource.PropertyHandler | ||
logger log.LoggerInterface | ||
client *agollo.Client | ||
} | ||
|
||
// WithPropertyHandlers set property handlers | ||
func WithPropertyHandlers(handlers ...datasource.PropertyHandler) Option { | ||
return func(o *options) { | ||
o.handlers = handlers | ||
} | ||
} | ||
|
||
// WithLogger set apollo logger | ||
func WithLogger(logger log.LoggerInterface) Option { | ||
return func(o *options) { | ||
o.logger = logger | ||
} | ||
} | ||
|
||
// apolloDatasource implements datasource.Datasource | ||
type apolloDatasource struct { | ||
datasource.Base | ||
client *agollo.Client | ||
propertyKey string | ||
} | ||
|
||
// NewDatasource create apollo datasource | ||
func NewDatasource(conf *config.AppConfig, propertyKey string, opts ...Option) (datasource.DataSource, error) { | ||
if conf == nil { | ||
return nil, ErrMissConfig | ||
} | ||
if propertyKey == "" { | ||
return nil, ErrEmptyKey | ||
} | ||
option := &options{ | ||
logger: &log.DefaultLogger{}, | ||
} | ||
for _, opt := range opts { | ||
opt(option) | ||
} | ||
agollo.SetLogger(option.logger) | ||
apolloClient, err := agollo.StartWithConfig(func() (*config.AppConfig, error) { | ||
return conf, nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ds := &apolloDatasource{ | ||
client: apolloClient, | ||
propertyKey: propertyKey, | ||
} | ||
for _, handler := range option.handlers { | ||
ds.AddPropertyHandler(handler) | ||
} | ||
return ds, nil | ||
} | ||
|
||
func (a *apolloDatasource) ReadSource() ([]byte, error) { | ||
value := a.client.GetValue(a.propertyKey) | ||
return []byte(value), nil | ||
} | ||
|
||
func (a *apolloDatasource) Initialize() error { | ||
source, err := a.ReadSource() | ||
if err != nil { | ||
return err | ||
} | ||
a.handle(source) | ||
listener := &customChangeListener{ | ||
ds: a, | ||
} | ||
a.client.AddChangeListener(listener) | ||
return nil | ||
} | ||
|
||
func (a *apolloDatasource) Close() error { | ||
return nil | ||
} | ||
|
||
func (a *apolloDatasource) handle(source []byte) { | ||
err := a.Handle(source) | ||
if err != nil { | ||
log.Errorf("update config err: %s", err.Error()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package apollo | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/apolloconfig/agollo/v4/env/config" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// must run an apollo server before test | ||
// this would be failed for CICD test. so it's disabled for now | ||
//func TestConfig(t *testing.T) { | ||
// | ||
// var resultConfig string | ||
// | ||
// propertyHandler := datasource2.NewDefaultPropertyHandler( | ||
// func(src []byte) (interface{}, error) { | ||
// return string(src), nil | ||
// }, | ||
// func(data interface{}) error { | ||
// s := data.(string) | ||
// fmt.Println(s) | ||
// resultConfig = s | ||
// return nil | ||
// }, | ||
// ) | ||
// c := &config.AppConfig{ | ||
// AppID: "SampleApp", | ||
// Cluster: "DEV", | ||
// IP: "http://localhost:8080", | ||
// NamespaceName: "application", | ||
// IsBackupConfig: true, | ||
// Secret: "1dc9532d02cd47f0bb26ee39d614ef8d", | ||
// } | ||
// datasource, err := NewDatasource( | ||
// c, "timeout", | ||
// WithLogger(&log.DefaultLogger{}), | ||
// WithPropertyHandlers(propertyHandler), | ||
// ) | ||
// assert.Nil(t, err) | ||
// err = datasource.Initialize() | ||
// assert.Nil(t, err) | ||
// assert.Equal(t, "123", resultConfig) | ||
// select {} | ||
//} | ||
|
||
func TestEmptyKey(t *testing.T) { | ||
c := &config.AppConfig{ | ||
AppID: "SampleApp", | ||
Cluster: "DEV", | ||
IP: "http://localhost:8080", | ||
NamespaceName: "application", | ||
IsBackupConfig: true, | ||
Secret: "1dc9532d02cd47f0bb26ee39d614ef8d", | ||
} | ||
_, err := NewDatasource(c, "") | ||
assert.Equal(t, ErrEmptyKey, err) | ||
} | ||
|
||
func TestMissConfig(t *testing.T) { | ||
_, err := NewDatasource(nil, "test") | ||
assert.Equal(t, ErrMissConfig, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module github.com/alibaba/sentinel-golang/pkg/datasource/apollo | ||
|
||
go 1.13 | ||
|
||
replace github.com/alibaba/sentinel-golang => ../../../ | ||
|
||
require ( | ||
github.com/alibaba/sentinel-golang v1.0.3 | ||
github.com/apolloconfig/agollo/v4 v4.0.9 | ||
github.com/pkg/errors v0.9.1 | ||
github.com/stretchr/testify v1.7.0 | ||
) | ||
|
||
require ( | ||
github.com/spf13/viper v1.9.0 // indirect | ||
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
gopkg.in/ini.v1 v1.64.0 // indirect | ||
) |
Oops, something went wrong.