-
Notifications
You must be signed in to change notification settings - Fork 426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pkg/datasource):Add apollo support #444
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37d4777
feat(pkg/datasource):add apollo datasource
Casper-Mars 596a3be
feat(pkg/datasource):add apollo datasource
Casper-Mars 38dd2fc
fix(pkg/datasource):fix ci problem
Casper-Mars 76676c0
feat(.github/workflow):add apollo datasource ci test command
Casper-Mars 3765d5f
feat(datasource/apollo):change go.mod version to 1.13
Casper-Mars File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,51 @@ | ||
module github.com/alibaba/sentinel-golang/pkg/datasource/apollo | ||
|
||
go 1.17 | ||
|
||
replace github.com/alibaba/sentinel-golang => ../../../ | ||
|
||
require ( | ||
github.com/alibaba/sentinel-golang v1.0.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v1.0.3 is available here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
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/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.1 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/fsnotify/fsnotify v1.5.1 // indirect | ||
github.com/go-ole/go-ole v1.2.4 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/google/uuid v1.1.2 // indirect | ||
github.com/hashicorp/hcl v1.0.0 // indirect | ||
github.com/magiconair/properties v1.8.5 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect | ||
github.com/mitchellh/mapstructure v1.4.2 // indirect | ||
github.com/pelletier/go-toml v1.9.4 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_golang v1.9.0 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/common v0.15.0 // indirect | ||
github.com/prometheus/procfs v0.2.0 // indirect | ||
github.com/shirou/gopsutil/v3 v3.21.6 // indirect | ||
github.com/spf13/afero v1.6.0 // indirect | ||
github.com/spf13/cast v1.4.1 // indirect | ||
github.com/spf13/jwalterweatherman v1.1.0 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
github.com/spf13/viper v1.9.0 // indirect | ||
github.com/stretchr/objx v0.1.1 // indirect | ||
github.com/subosito/gotenv v1.2.0 // indirect | ||
github.com/tklauser/go-sysconf v0.3.6 // indirect | ||
github.com/tklauser/numcpus v0.2.2 // indirect | ||
go.uber.org/atomic v1.7.0 // indirect | ||
go.uber.org/multierr v1.6.0 // indirect | ||
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
google.golang.org/protobuf v1.27.1 // indirect | ||
gopkg.in/ini.v1 v1.64.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1.17 might be too new for some legacy users. How about keeping 1.13 (if possible?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK