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

Ftr: config center apollo #250

Merged
merged 30 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ const (

const (
CONFIG_NAMESPACE_KEY = "config.namespace"
CONFIG_GROUP_KEY = "config.group"
CONFIG_CLUSTER_KEY = "config.cluster"
CONFIG_CHECK_KEY = "config.check"
CONFIG_TIMEOUT_KET = "config.timeout"
CONFIG_VERSION_KEY = "configVersion"
COMPATIBLE_CONFIG_KEY = "compatible_config"
Expand Down
2 changes: 1 addition & 1 deletion config/base_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type BaseConfig struct {
}

func (c *BaseConfig) startConfigCenter(ctx context.Context) error {
url, err := common.NewURL(ctx, c.ConfigCenterConfig.Address, common.WithProtocol(c.ConfigCenterConfig.Protocol))
url, err := common.NewURL(ctx, c.ConfigCenterConfig.Address, common.WithProtocol(c.ConfigCenterConfig.Protocol), common.WithParams(c.ConfigCenterConfig.GetUrlMap()))
if err != nil {
return err
}
Expand Down
16 changes: 16 additions & 0 deletions config/base_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
_ "github.com/apache/dubbo-go/config_center/apollo"
)

func Test_refresh(t *testing.T) {
Expand Down Expand Up @@ -490,6 +491,21 @@ func Test_startConfigCenter(t *testing.T) {
assert.Equal(t, "ikurento.com", v)
}

//func Test_startApolloConfigCenter(t *testing.T) {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
// c := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{
// Protocol: "apollo",
// Address: "106.12.25.204:8080",
// Group: "testApplication_yang",
// Cluster: "dev",
// ConfigFile: "mockDubbog.properties",
// }}
// err := c.startConfigCenter(context.Background())
// assert.NoError(t, err)
// b, v := config.GetEnvInstance().Configuration().Back().Value.(*config.InmemoryConfiguration).GetProperty("application.organization")
// assert.True(t, b)
// assert.Equal(t, "ikurento.com", v)
//}

func Test_initializeStruct(t *testing.T) {
consumerConfig := &ConsumerConfig{}
tp := reflect.TypeOf(ConsumerConfig{})
Expand Down
10 changes: 10 additions & 0 deletions config/config_center_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package config

import (
"context"
"github.com/apache/dubbo-go/common/constant"
zouyx marked this conversation as resolved.
Show resolved Hide resolved
"net/url"
"time"
)

Expand Down Expand Up @@ -50,3 +52,11 @@ func (c *ConfigCenterConfig) UnmarshalYAML(unmarshal func(interface{}) error) er
}
return nil
}

func (c *ConfigCenterConfig) GetUrlMap() url.Values {
urlMap := url.Values{}
urlMap.Set(constant.CONFIG_NAMESPACE_KEY, c.ConfigFile)
urlMap.Set(constant.CONFIG_GROUP_KEY, c.Group)
zouyx marked this conversation as resolved.
Show resolved Hide resolved
urlMap.Set(constant.CONFIG_CLUSTER_KEY, c.Cluster)
return urlMap
}
57 changes: 57 additions & 0 deletions config_center/apollo/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apollo

import (
"sync"
)

import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/extension"
. "github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/config_center/parser"
)

func init() {
extension.SetConfigCenterFactory("apollo", createDynamicConfigurationFactory)
}

func createDynamicConfigurationFactory() DynamicConfigurationFactory {
return &apolloConfigurationFactory{}
}

type apolloConfigurationFactory struct{}

var (
once sync.Once
dynamicConfiguration *apolloConfiguration
)

func (f *apolloConfigurationFactory) GetDynamicConfiguration(url *common.URL) (DynamicConfiguration, error) {
var err error
once.Do(func() {
dynamicConfiguration, err = newApolloConfiguration(url)
})
if err != nil {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
dynamicConfiguration.SetParser(&parser.DefaultConfigurationParser{})
return dynamicConfiguration, err

}
157 changes: 157 additions & 0 deletions config_center/apollo/impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apollo

import (
"fmt"
"github.com/go-errors/errors"
"strconv"
"strings"
"sync"

"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
. "github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/config_center/parser"
"github.com/apache/dubbo-go/remoting"

"github.com/zouyx/agollo"
zouyx marked this conversation as resolved.
Show resolved Hide resolved
zouyx marked this conversation as resolved.
Show resolved Hide resolved
)

const (
apolloProtocolPrefix = "http://"
apolloConfigFormat = "%s.%s"
)

type apolloConfiguration struct {
url *common.URL

listeners sync.Map
appConf *agollo.AppConfig
parser parser.ConfigurationParser
}

func newApolloConfiguration(url *common.URL) (*apolloConfiguration, error) {
c := &apolloConfiguration{
url: url,
}
configAddr := c.getAddressWithProtocolPrefix(url)
configCluster := url.GetParam(constant.CONFIG_CLUSTER_KEY, "")

appId := url.GetParam(constant.CONFIG_GROUP_KEY, DEFAULT_GROUP)
namespaces := url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP))
readyConfig := &agollo.AppConfig{
AppId: appId,
Cluster: configCluster,
NamespaceName: namespaces,
Ip: configAddr,
}

agollo.InitCustomConfig(func() (*agollo.AppConfig, error) {
return readyConfig, nil
})

return c, agollo.Start()
}

func getChangeType(change agollo.ConfigChangeType) remoting.EventType {
switch change {
case agollo.ADDED:
return remoting.EventTypeAdd
case agollo.DELETED:
return remoting.EventTypeDel
case agollo.MODIFIED:
return remoting.EventTypeUpdate
default:
panic("unknown type: " + strconv.Itoa(int(change)))
zouyx marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (c *apolloConfiguration) AddListener(key string, listener ConfigurationListener, opts ...Option) {
k := &Options{}
for _, opt := range opts {
opt(k)
}

key = k.Group + key
l, _ := c.listeners.LoadOrStore(key, NewApolloListener())
l.(*apolloListener).AddListener(listener)
}

func (c *apolloConfiguration) RemoveListener(key string, listener ConfigurationListener, opts ...Option) {
k := &Options{}
for _, opt := range opts {
opt(k)
}

key = k.Group + key
l, ok := c.listeners.Load(key)
if ok {
l.(*apolloListener).RemoveListener(listener)
}
}

func getProperties(namespace string) string {
return getNamespaceName(namespace, agollo.Properties)
}

func getNamespaceName(namespace string, configFileFormat agollo.ConfigFileFormat) string {
return fmt.Sprintf(apolloConfigFormat, namespace, configFileFormat)
}

func (c *apolloConfiguration) GetConfig(key string, opts ...Option) (string, error) {
k := &Options{}
for _, opt := range opts {
opt(k)
}
namespace := c.url.GetParam(constant.CONFIG_NAMESPACE_KEY, getProperties(DEFAULT_GROUP))
config := agollo.GetConfig(namespace)
if config == nil {
zouyx marked this conversation as resolved.
Show resolved Hide resolved
return "", errors.New(fmt.Sprintf("nothiing in namespace:%s ", namespace))
}
return config.GetContent(agollo.Properties), nil
}

func (c *apolloConfiguration) getAddressWithProtocolPrefix(url *common.URL) string {
address := url.Location
converted := address
if len(address) != 0 {
parts := strings.Split(address, ",")
zouyx marked this conversation as resolved.
Show resolved Hide resolved
addrs := make([]string, 0)
for _, part := range parts {
addr := part
if !strings.HasPrefix(part, apolloProtocolPrefix) {
addr = apolloProtocolPrefix + part
}
addrs = append(addrs, addr)
}
converted = strings.Join(addrs, ",")
}
return converted
}

func (c *apolloConfiguration) Parser() parser.ConfigurationParser {
return c.parser
}
func (c *apolloConfiguration) SetParser(p parser.ConfigurationParser) {
c.parser = p
}

func (c *apolloConfiguration) GetConfigs(key string, opts ...Option) (string, error) {
return c.GetConfig(key, opts...)
}
Loading