Skip to content

Commit

Permalink
Add Apollo data-source extension (#444)
Browse files Browse the repository at this point in the history
* 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
Casper-Mars committed Dec 11, 2021
1 parent 9eabe9f commit 257d5b8
Show file tree
Hide file tree
Showing 6 changed files with 1,155 additions and 0 deletions.
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)
}
19 changes: 19 additions & 0 deletions pkg/datasource/apollo/go.mod
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
)
Loading

0 comments on commit 257d5b8

Please sign in to comment.