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

Config center feature implement for setting the zookeeper as config center to refresh consumerConfig & providerConfig when dubbogo starting. #99

Merged
merged 16 commits into from
Jun 29, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
104 changes: 104 additions & 0 deletions common/config/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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 config

import (
"container/list"
"strings"
"sync"
)

// There is dubbo.properties file and application level config center configuration which higner than normal config center in java. So in java the
// configuration sequence will be config center > application level config center > dubbo.properties > spring bean configuration.
// But in go, neither the dubbo.properties file or application level config center configuration will not support for the time being.
// We just have config center configuration which can override configuration in consumer.yaml & provider.yaml.
// But for add these features in future ,I finish the environment struct following Environment class in java.
type Environment struct {
configCenterFirst bool
externalConfigs sync.Map
externalConfigMap sync.Map
}

var (
instance *Environment
once sync.Once
)

hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
func GetEnvInstance() *Environment {
once.Do(func() {
instance = &Environment{configCenterFirst: true}
})
return instance
}

//func (env *Environment) SetConfigCenterFirst() {

Choose a reason for hiding this comment

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

Why not remove comment?

// env.configCenterFirst = true
//}

//func (env *Environment) ConfigCenterFirst() bool {

Choose a reason for hiding this comment

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

As above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里我估计后面添加新的配置级别,就会要加上,所以没有直接去掉,先注释掉了。先不删吧

// return env.configCenterFirst
//}

func (env *Environment) UpdateExternalConfigMap(externalMap map[string]string) {
for k, v := range externalMap {
env.externalConfigMap.Store(k, v)
}
}

func (env *Environment) Configuration() *list.List {
list := list.New()
memConf := newInmemoryConfiguration()
memConf.setProperties(env.externalConfigMap)
list.PushBack(memConf)
return list
}

type InmemoryConfiguration struct {
store sync.Map
}

func newInmemoryConfiguration() *InmemoryConfiguration {
return &InmemoryConfiguration{}
}
func (conf *InmemoryConfiguration) setProperties(p sync.Map) {
conf.store = p
}

func (conf *InmemoryConfiguration) GetProperty(key string) (bool, string) {
v, ok := conf.store.Load(key)
if ok {
return true, v.(string)
}
hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
return false, ""

}

func (conf *InmemoryConfiguration) GetSubProperty(subKey string) map[string]struct{} {
properties := make(map[string]struct{})
conf.store.Range(func(key, value interface{}) bool {
if idx := strings.Index(key.(string), subKey); idx >= 0 {
after := key.(string)[idx+len(subKey):]
if i := strings.Index(after, "."); i >= 0 {
properties[after[0:strings.Index(after, ".")]] = struct{}{}
}

}
return true
})
return properties
}
52 changes: 52 additions & 0 deletions common/config/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 config

import (
"testing"
)
import (
"github.com/stretchr/testify/assert"
)

func TestGetEnvInstance(t *testing.T) {
GetEnvInstance()
assert.NotNil(t, instance)
}

func TestEnvironment_UpdateExternalConfigMap(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"1": "2"})
v, ok := GetEnvInstance().externalConfigMap.Load("1")
assert.True(t, ok)
assert.Equal(t, "2", v)
}

func TestEnvironment_ConfigurationAndGetProperty(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"1": "2"})
list := GetEnvInstance().Configuration()
ok, v := list.Front().Value.(*InmemoryConfiguration).GetProperty("1")
assert.True(t, ok)
assert.Equal(t, "2", v)
}

func TestInmemoryConfiguration_GetSubProperty(t *testing.T) {
GetEnvInstance().UpdateExternalConfigMap(map[string]string{"123": "2"})
list := GetEnvInstance().Configuration()
m := list.Front().Value.(*InmemoryConfiguration).GetSubProperty("1")

assert.Equal(t, struct{}{}, m["123"])
}
3 changes: 3 additions & 0 deletions common/constant/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package constant

const (
DUBBO = "dubbo"
)
const (
DEFAULT_WEIGHT = 100 //
DEFAULT_WARMUP = 10 * 60 // in java here is 10*60*1000 because of System.currentTimeMillis() is measured in milliseconds & in go time.Unix() is second
Expand Down
8 changes: 8 additions & 0 deletions common/constant/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ const (
CONFIG_NAMESPACE_KEY = "config.namespace"
CONFIG_TIMEOUT_KET = "config.timeout"
)
const (
RegistryConfigPrefix = "dubbo.registries."
ReferenceConfigPrefix = "dubbo.reference."
ServiceConfigPrefix = "dubbo.service."
ProtocolConfigPrefix = "dubbo.protocols."
ProviderConfigPrefix = "dubbo.provider."
ConsumerConfigPrefix = "dubbo.consumer."
)
37 changes: 37 additions & 0 deletions common/extension/config_center_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 extension

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

var (
configCenterFactories = make(map[string]func() config_center.DynamicConfigurationFactory)
)

func SetConfigCenterFactory(name string, v func() config_center.DynamicConfigurationFactory) {
configCenterFactories[name] = v
}

func GetConfigCenterFactory(name string) config_center.DynamicConfigurationFactory {
if configCenterFactories[name] == nil {
panic("config center for " + name + " is not existing, make sure you have import the package.")
}
return configCenterFactories[name]()
}
9 changes: 8 additions & 1 deletion common/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error)
}

//rawUrlString = "//" + rawUrlString
if strings.Index(rawUrlString, "//") < 0 {
t := URL{baseUrl: baseUrl{ctx: ctx}}
for _, opt := range opts {
opt(&t)
}
rawUrlString = t.Protocol + "://" + rawUrlString
}
serviceUrl, err = url.Parse(rawUrlString)
if err != nil {
return s, perrors.Errorf("url.Parse(url string{%s}), error{%v}", rawUrlString, err)
Expand Down Expand Up @@ -197,10 +204,10 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error)
// s.Timeout = time.Duration(timeout * 1e6) // timeout unit is millisecond
// }
//}
//fmt.Println(s.String())
hxmhlt marked this conversation as resolved.
Show resolved Hide resolved
for _, opt := range opts {
opt(&s)
}
//fmt.Println(s.String())
return s, nil
}

Expand Down
27 changes: 27 additions & 0 deletions common/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ func TestURL(t *testing.T) {
"ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245", u.String())
}

func TestURLWithoutSchema(t *testing.T) {
u, err := NewURL(context.TODO(), "@127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+
"side=provider&timeout=3000&timestamp=1556509797245", WithProtocol("dubbo"))
assert.NoError(t, err)

assert.Equal(t, "/com.ikurento.user.UserProvider", u.Path)
assert.Equal(t, "127.0.0.1:20000", u.Location)
assert.Equal(t, "dubbo", u.Protocol)
assert.Equal(t, "127.0.0.1", u.Ip)
assert.Equal(t, "20000", u.Port)
assert.Equal(t, URL{}.Methods, u.Methods)
assert.Equal(t, "", u.Username)
assert.Equal(t, "", u.Password)
assert.Equal(t, "anyhost=true&application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-"+
"provider-golang-1.0.0&environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%"+
"2C&module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&t"+
"imestamp=1556509797245", u.Params.Encode())

assert.Equal(t, "dubbo://:@127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&application=BDTServi"+
"ce&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&environment=dev&interface=com.ikure"+
"nto.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&module=dubbogo+user-info+server&org=ikurento.com&owner="+
"ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245", u.String())
}

func TestURL_URLEqual(t *testing.T) {
u1, err := NewURL(context.TODO(), "dubbo://:@127.0.0.1:20000/com.ikurento.user.UserProvider?interface=com.ikurento.user.UserProvider&group=&version=2.6.0")
assert.NoError(t, err)
Expand Down
24 changes: 18 additions & 6 deletions config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@

package config

import "github.com/apache/dubbo-go/common/constant"

type ApplicationConfig struct {
Organization string `yaml:"organization" json:"organization,omitempty"`
Name string `yaml:"name" json:"name,omitempty"`
Module string `yaml:"module" json:"module,omitempty"`
Version string `yaml:"version" json:"version,omitempty"`
Owner string `yaml:"owner" json:"owner,omitempty"`
Environment string `yaml:"environment" json:"environment,omitempty"`
Organization string `yaml:"organization" json:"organization,omitempty" property:"organization"`
Name string `yaml:"name" json:"name,omitempty" property:"name"`
Module string `yaml:"module" json:"module,omitempty" property:"module"`
Version string `yaml:"version" json:"version,omitempty" property:"version"`
Owner string `yaml:"owner" json:"owner,omitempty" property:"owner"`
Environment string `yaml:"environment" json:"environment,omitempty" property:"environment"`
}

func (*ApplicationConfig) Prefix() string {
return constant.DUBBO + ".application."
}
func (c *ApplicationConfig) Id() string {
return ""
}
func (c *ApplicationConfig) SetId(id string) {

}
Loading