Skip to content
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 5 commits into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ jobs:
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
cd ../nacos
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
cd ../apollo
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
cd ../../adapters/echo
go test -race -count=1 ./... -coverprofile=coverage.txt -covermode=atomic
cd ../gear
Expand Down
103 changes: 103 additions & 0 deletions pkg/datasource/apollo/apollo.go
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())
}
}
63 changes: 63 additions & 0 deletions pkg/datasource/apollo/apollo_test.go
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)
}
51 changes: 51 additions & 0 deletions pkg/datasource/apollo/go.mod
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
Copy link
Member

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?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK


replace github.com/alibaba/sentinel-golang => ../../../

require (
github.com/alibaba/sentinel-golang v1.0.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1.0.3 is available here

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
)
Loading