From 39f5bfeebb1f2179de0129985eb219a0ff3a7efa Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Tue, 26 Jan 2021 16:51:16 +0800 Subject: [PATCH 1/7] feat: add config api --- config/config_api.go | 471 ++++++++++++++++++++++++++++++++++++++ config/config_api_test.go | 158 +++++++++++++ 2 files changed, 629 insertions(+) create mode 100644 config/config_api.go create mode 100644 config/config_api_test.go diff --git a/config/config_api.go b/config/config_api.go new file mode 100644 index 0000000000..15db1d80dd --- /dev/null +++ b/config/config_api.go @@ -0,0 +1,471 @@ +/* + * 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 ( + "context" + "time" +) + +//////////////////////////////////// default registry config +const defaultZKAddr = "127.0.0.1:2181" +const defaultConsulAddr = "127.0.0.1:8500" +const defaultNacosAddr = "127.0.0.1:8848" +const defaultRegistryTimeout = "3s" + +func NewDefaultRegistryConfig(protocol string) *RegistryConfig { + switch protocol { + case "zookeeper": + return &RegistryConfig{ + Protocol: protocol, + Address: defaultZKAddr, + TimeoutStr: defaultRegistryTimeout, + } + case "consul": + return &RegistryConfig{ + Protocol: protocol, + Address: defaultConsulAddr, + TimeoutStr: defaultRegistryTimeout, + } + case "nacos": + return &RegistryConfig{ + Protocol: protocol, + Address: defaultNacosAddr, + TimeoutStr: defaultRegistryTimeout, + } + default: + return &RegistryConfig{ + Protocol: protocol, + } + } +} + +///////////////////////////////////// registry config api +type RegistryConfigOpt func(config *RegistryConfig) *RegistryConfig + +func NewRegistryConfig(opts ...RegistryConfigOpt) *RegistryConfig { + newRegistryConfig := NewDefaultRegistryConfig("none") + for _, v := range opts { + newRegistryConfig = v(newRegistryConfig) + } + return newRegistryConfig +} + +func WithRegistryProtocol(regProtocol string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Protocol = regProtocol + return config + } +} + +func WithRegistryAddress(addr string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Address = addr + return config + } +} + +func WithRegistryTimeOut(timeout string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.TimeoutStr = timeout + return config + } +} + +func WithRegistryGroup(group string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Group = group + return config + } +} + +func WithRegistryTTL(ttl string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.TTL = ttl + return config + } +} + +func WithRegistryUserName(userName string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Username = userName + return config + } +} + +func WithRegistryPassword(psw string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Password = psw + return config + } +} + +func WithRegistrySimplified(simplified bool) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Simplified = simplified + return config + } +} + +func WithRegistryPreferred(preferred bool) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Preferred = preferred + return config + } +} + +func WithRegistryWeight(weight int64) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Weight = weight + return config + } +} + +func WithRegistryParams(params map[string]string) RegistryConfigOpt { + return func(config *RegistryConfig) *RegistryConfig { + config.Params = params + return config + } +} + +///////////////////////////////////// consumer config api +type ConsumerConfigOpt func(config *ConsumerConfig) *ConsumerConfig + +func NewDefaultConsumerConfig() *ConsumerConfig { + check := true + newConsumerConfig := &ConsumerConfig{ + BaseConfig: BaseConfig{}, + Registries: make(map[string]*RegistryConfig, 8), + References: make(map[string]*ReferenceConfig, 8), + ConnectTimeout: 3 * time.Second, + RequestTimeout: 3 * time.Second, + Check: &check, + } + return newConsumerConfig +} + +func NewConsumerConfig(opts ...ConsumerConfigOpt) *ConsumerConfig { + newConfig := NewDefaultConsumerConfig() + for _, v := range opts { + v(newConfig) + } + return newConfig +} +func WithConsumerAppConfig(appConfig *ApplicationConfig) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.ApplicationConfig = appConfig + return config + } +} + +func WithConsumerRegistryConfig(registryKey string, regConfig *RegistryConfig) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.Registries[registryKey] = regConfig + return config + } +} + +func WithConsumerReferenceConfig(referenceKey string, refConfig *ReferenceConfig) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.References[referenceKey] = refConfig + return config + } +} + +func WithConsumerConnTimeout(timeout time.Duration) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.ConnectTimeout = timeout + return config + } +} + +func WithConsumerRequestTimeout(timeout time.Duration) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.RequestTimeout = timeout + return config + } +} + +func WithConsumerConfigCenterConfig(configCenterConfig *ConfigCenterConfig) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + config.ConfigCenterConfig = configCenterConfig + return config + } +} + +func WithConsumerConfigCheck(check bool) ConsumerConfigOpt { + return func(config *ConsumerConfig) *ConsumerConfig { + *config.Check = check + return config + } +} + +//////////////////////////////////// reference config api +type ReferenceConfigOpt func(config *ReferenceConfig) *ReferenceConfig + +func NewDefaultReferenceConfig() *ReferenceConfig { + newReferenceConfig := NewReferenceConfig("", context.Background()) + newReferenceConfig.Methods = make([]*MethodConfig, 0, 8) + newReferenceConfig.Params = make(map[string]string, 8) + return newReferenceConfig +} + +func NewReferenceConfigByAPI(opts ...ReferenceConfigOpt) *ReferenceConfig { + newreferenceConfig := NewDefaultReferenceConfig() + for _, v := range opts { + v(newreferenceConfig) + } + return newreferenceConfig +} + +func WithReferenceRegistry(registry string) ReferenceConfigOpt { + return func(config *ReferenceConfig) *ReferenceConfig { + config.Registry = registry + return config + } +} +func WithReferenceProtocol(protocol string) ReferenceConfigOpt { + return func(config *ReferenceConfig) *ReferenceConfig { + config.Protocol = protocol + return config + } +} +func WithReferenceInterface(interfaceName string) ReferenceConfigOpt { + return func(config *ReferenceConfig) *ReferenceConfig { + config.InterfaceName = interfaceName + return config + } +} +func WithReferenceCluster(cluster string) ReferenceConfigOpt { + return func(config *ReferenceConfig) *ReferenceConfig { + config.Cluster = cluster + return config + } +} +func WithReferenceMethod(methodName, retries, lb string) ReferenceConfigOpt { + return func(config *ReferenceConfig) *ReferenceConfig { + config.Methods = append(config.Methods, &MethodConfig{ + Name: methodName, + Retries: retries, + LoadBalance: lb, + }) + return config + } +} + +///////////////////////////////////// provider config api +type ProviderConfigOpt func(config *ProviderConfig) *ProviderConfig + +func NewDefaultProviderConfig() *ProviderConfig { + newConsumerConfig := &ProviderConfig{ + BaseConfig: BaseConfig{ + ApplicationConfig: &ApplicationConfig{ + Name: "dubbo", + Module: "module", + Organization: "dubbo_org", + Owner: "dubbo", + }, + }, + Services: make(map[string]*ServiceConfig), + Registries: make(map[string]*RegistryConfig, 8), + Protocols: make(map[string]*ProtocolConfig, 8), + } + return newConsumerConfig +} + +func NewProviderConfig(opts ...ProviderConfigOpt) *ProviderConfig { + newConfig := NewDefaultProviderConfig() + for _, v := range opts { + v(newConfig) + } + return newConfig +} + +func WithPrividerRegistryConfig(regConfig *RegistryConfig) ProviderConfigOpt { + return func(config *ProviderConfig) *ProviderConfig { + config.Registries[regConfig.Protocol] = regConfig + return config + } +} + +func WithProviderAppConfig(appConfig *ApplicationConfig) ProviderConfigOpt { + return func(config *ProviderConfig) *ProviderConfig { + config.ApplicationConfig = appConfig + return config + } +} + +func WithProviderServices(serviceName string, serviceConfig *ServiceConfig) ProviderConfigOpt { + return func(config *ProviderConfig) *ProviderConfig { + config.Services[serviceName] = serviceConfig + return config + } +} + +func WithProviderProtocol(protocolKey, protocol, port string) ProviderConfigOpt { + return func(config *ProviderConfig) *ProviderConfig { + config.Protocols[protocolKey] = &ProtocolConfig{ + Name: protocol, + Port: port, + } + return config + } +} + +func WithProviderRegistry(registryKey string, registryConfig *RegistryConfig) ProviderConfigOpt { + return func(config *ProviderConfig) *ProviderConfig { + config.Registries[registryKey] = registryConfig + return config + } +} + +/////////////////////////////////////// service config api +type ServiceConfigOpt func(config *ServiceConfig) *ServiceConfig + +func NewDefaultServiceConfig() *ServiceConfig { + newServiceConfig := NewServiceConfig("", context.Background()) + newServiceConfig.Params = make(map[string]string) + newServiceConfig.Methods = make([]*MethodConfig, 0, 8) + return newServiceConfig +} + +// NewServiceConfigByAPI is named as api, because there is NewServiceConfig func already declared +func NewServiceConfigByAPI(opts ...ServiceConfigOpt) *ServiceConfig { + defaultServiceConfig := NewDefaultServiceConfig() + for _, v := range opts { + v(defaultServiceConfig) + } + return defaultServiceConfig +} + +func WithServiceRegistry(registry string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Registry = registry + return config + } +} + +func WithServiceProtocol(protocol string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Protocol = protocol + return config + } +} +func WithServiceInterface(interfaceName string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.InterfaceName = interfaceName + return config + } +} +func WithServiceLoadBalance(lb string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Loadbalance = lb + return config + } +} + +func WithServiceWarmUpTime(warmUp string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Warmup = warmUp + return config + } +} + +func WithServiceCluster(cluster string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Cluster = cluster + return config + } +} + +func WithServiceMethod(name, retries, lb string) ServiceConfigOpt { + return func(config *ServiceConfig) *ServiceConfig { + config.Methods = append(config.Methods, &MethodConfig{ + Name: name, + Retries: retries, + LoadBalance: lb, + }) + return config + } +} + +///////////////////////////////////////// Application config api +type ApplicationConfigOpt func(config *ApplicationConfig) *ApplicationConfig + +func NewDefaultApplicationConfig() *ApplicationConfig { + newAppConfig := &ApplicationConfig{ + Name: "dubbo.io", + Module: "sample", + Organization: "dubbo.io", + Owner: "dubbogo", + Version: "0.0.1", + Environment: "dev", + } + return newAppConfig +} + +// NewApplicationConfig is named as api, because there is NewServiceConfig func already declared +func NewApplicationConfig(opts ...ApplicationConfigOpt) *ApplicationConfig { + defaultServiceConfig := NewDefaultApplicationConfig() + for _, v := range opts { + v(defaultServiceConfig) + } + return defaultServiceConfig +} + +func WithAppName(name string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Name = name + return config + } +} + +func WithAppModule(module string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Module = module + return config + } +} + +func WithAppOrganization(org string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Organization = org + return config + } +} + +func WithAppOwner(owner string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Owner = owner + return config + } +} + +func WithAppVersion(version string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Version = version + return config + } +} + +func WithAppEnvironment(env string) ApplicationConfigOpt { + return func(config *ApplicationConfig) *ApplicationConfig { + config.Environment = env + return config + } +} diff --git a/config/config_api_test.go b/config/config_api_test.go new file mode 100644 index 0000000000..5e6d5f529b --- /dev/null +++ b/config/config_api_test.go @@ -0,0 +1,158 @@ +package config + +import ( + "github.com/stretchr/testify/assert" + "strconv" + "testing" + "time" +) + +func TestNewDefaultServiceConfig(t *testing.T) { + serviceConfig := NewServiceConfigByAPI( + WithServiceCluster("test-cluster"), + WithServiceInterface("test-interface"), + WithServiceLoadBalance("test-loadbalance"), + WithServiceMethod("test-method1", "test-retries1", "test-lb1"), + WithServiceMethod("test-method2", "test-retries2", "test-lb2"), + WithServiceMethod("test-method3", "test-retries3", "test-lb3"), + WithServiceProtocol("test-protocol"), + WithServiceRegistry("test-registry"), + WithServiceWarmUpTime("test-warmup"), + ) + assert.Equal(t, serviceConfig.Cluster, "test-cluster") + assert.Equal(t, serviceConfig.InterfaceName, "test-interface") + assert.Equal(t, serviceConfig.Loadbalance, "test-loadbalance") + for i, v := range serviceConfig.Methods { + backFix := strconv.Itoa(i + 1) + assert.Equal(t, v.Name, "test-method"+backFix) + assert.Equal(t, v.Retries, "test-retries"+backFix) + assert.Equal(t, v.LoadBalance, "test-lb"+backFix) + } + assert.Equal(t, serviceConfig.Protocol, "test-protocol") + assert.Equal(t, serviceConfig.Registry, "test-registry") + assert.Equal(t, serviceConfig.Warmup, "test-warmup") +} + +func TestNewReferenceConfigByAPI(t *testing.T) { + refConfig := NewReferenceConfigByAPI( + WithReferenceCluster("test-cluster"), + WithReferenceInterface("test-interface"), + WithReferenceMethod("test-method1", "test-retries1", "test-lb1"), + WithReferenceMethod("test-method2", "test-retries2", "test-lb2"), + WithReferenceMethod("test-method3", "test-retries3", "test-lb3"), + WithReferenceProtocol("test-protocol"), + WithReferenceRegistry("test-registry"), + ) + assert.Equal(t, refConfig.Cluster, "test-cluster") + assert.Equal(t, refConfig.InterfaceName, "test-interface") + for i, v := range refConfig.Methods { + backFix := strconv.Itoa(i + 1) + assert.Equal(t, v.Name, "test-method"+backFix) + assert.Equal(t, v.Retries, "test-retries"+backFix) + assert.Equal(t, v.LoadBalance, "test-lb"+backFix) + } + assert.Equal(t, refConfig.Protocol, "test-protocol") + assert.Equal(t, refConfig.Registry, "test-registry") +} + +func TestNewRegistryConfig(t *testing.T) { + regConfig := NewRegistryConfig( + WithRegistryTimeOut("test-timeout"), + WithRegistryProtocol("test-protocol"), + WithRegistryGroup("test-group"), + WithRegistryAddress("test-address"), + WithRegistrySimplified(true), + WithRegistryUserName("test-username"), + WithRegistryPassword("test-password"), + ) + assert.Equal(t, regConfig.TimeoutStr, "test-timeout") + assert.Equal(t, regConfig.Protocol, "test-protocol") + assert.Equal(t, regConfig.Group, "test-group") + assert.Equal(t, regConfig.Address, "test-address") + assert.Equal(t, regConfig.Simplified, true) + assert.Equal(t, regConfig.Username, "test-username") + assert.Equal(t, regConfig.Password, "test-password") +} + +func TestNewConsumerConfig(t *testing.T) { + referConfig := NewReferenceConfigByAPI( + WithReferenceCluster("test-cluster"), + WithReferenceInterface("test-interface"), + WithReferenceMethod("test-method1", "test-retries1", "test-lb1"), + WithReferenceMethod("test-method2", "test-retries2", "test-lb2"), + WithReferenceMethod("test-method3", "test-retries3", "test-lb3"), + WithReferenceProtocol("test-protocol"), + WithReferenceRegistry("test-registry"), + ) + defaultZKRegistry := NewDefaultRegistryConfig("zookeeper") + assert.Equal(t, defaultZKRegistry.Address, defaultZKAddr) + assert.Equal(t, defaultZKRegistry.Protocol, "zookeeper") + assert.Equal(t, defaultZKRegistry.TimeoutStr, defaultRegistryTimeout) + + consumerConfig := NewConsumerConfig( + WithConsumerConfigCheck(true), + WithConsumerConnTimeout(time.Minute), + WithConsumerRequestTimeout(time.Hour), + WithConsumerReferenceConfig("UserProvider", referConfig), + WithConsumerRegistryConfig("demoZK", defaultZKRegistry), + ) + + assert.Equal(t, *consumerConfig.Check, true) + assert.Equal(t, consumerConfig.ConnectTimeout, time.Minute) + assert.Equal(t, consumerConfig.RequestTimeout, time.Hour) + assert.Equal(t, consumerConfig.Registries["demoZK"], defaultZKRegistry) + assert.Equal(t, consumerConfig.References["UserProvider"], referConfig) +} + +func TestNewProviderConfig(t *testing.T) { + serviceConfig := NewServiceConfigByAPI( + WithServiceCluster("test-cluster"), + WithServiceInterface("test-interface"), + WithServiceLoadBalance("test-loadbalance"), + WithServiceMethod("test-method1", "test-retries1", "test-lb1"), + WithServiceMethod("test-method2", "test-retries2", "test-lb2"), + WithServiceMethod("test-method3", "test-retries3", "test-lb3"), + WithServiceProtocol("test-protocol"), + WithServiceRegistry("test-registry"), + WithServiceWarmUpTime("test-warmup"), + ) + + defaultConsulRegistry := NewDefaultRegistryConfig("consul") + assert.Equal(t, defaultConsulRegistry.Address, defaultConsulAddr) + assert.Equal(t, defaultConsulRegistry.Protocol, "consul") + assert.Equal(t, defaultConsulRegistry.TimeoutStr, defaultRegistryTimeout) + + defaultNacosRegistry := NewDefaultRegistryConfig("nacos") + assert.Equal(t, defaultNacosRegistry.Address, defaultNacosAddr) + assert.Equal(t, defaultNacosRegistry.Protocol, "nacos") + assert.Equal(t, defaultNacosRegistry.TimeoutStr, defaultRegistryTimeout) + + providerConfig := NewProviderConfig( + WithProviderServices("UserProvider", serviceConfig), + WithProviderProtocol("dubbo", "dubbo", "20000"), + WithProviderRegistry("demoConsul", defaultConsulRegistry), + WithProviderRegistry("demoNacos", defaultNacosRegistry), + ) + + assert.NotNil(t, providerConfig.Services) + for k, v := range providerConfig.Services { + assert.Equal(t, k, "UserProvider") + assert.Equal(t, v, serviceConfig) + } + assert.NotNil(t, providerConfig.Registries) + i := 0 + for k, v := range providerConfig.Registries { + if i == 0 { + assert.Equal(t, k, "demoConsul") + assert.Equal(t, v, defaultConsulRegistry) + i++ + } else { + assert.Equal(t, k, "demoNacos") + assert.Equal(t, v, defaultNacosRegistry) + } + } + + assert.NotNil(t, providerConfig.Protocols) + assert.Equal(t, providerConfig.Protocols["dubbo"].Name, "dubbo") + assert.Equal(t, providerConfig.Protocols["dubbo"].Port, "20000") +} From 0135e95b92d52d66d273200e585d6342cd4ab66f Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Tue, 26 Jan 2021 17:03:50 +0800 Subject: [PATCH 2/7] fix: split import block --- config/config_api_test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config/config_api_test.go b/config/config_api_test.go index 5e6d5f529b..ca002e24fa 100644 --- a/config/config_api_test.go +++ b/config/config_api_test.go @@ -1,12 +1,15 @@ package config import ( - "github.com/stretchr/testify/assert" "strconv" "testing" "time" ) +import ( + "github.com/stretchr/testify/assert" +) + func TestNewDefaultServiceConfig(t *testing.T) { serviceConfig := NewServiceConfigByAPI( WithServiceCluster("test-cluster"), From f671d4b4ce32407dc010e6a312bbc7c3ccd5adbd Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Tue, 26 Jan 2021 17:55:37 +0800 Subject: [PATCH 3/7] fix: fmt --- config/config_api.go | 10 ++++++---- config/config_api_test.go | 16 ++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/config/config_api.go b/config/config_api.go index 15db1d80dd..a3815b04e1 100644 --- a/config/config_api.go +++ b/config/config_api.go @@ -23,10 +23,12 @@ import ( ) //////////////////////////////////// default registry config -const defaultZKAddr = "127.0.0.1:2181" -const defaultConsulAddr = "127.0.0.1:8500" -const defaultNacosAddr = "127.0.0.1:8848" -const defaultRegistryTimeout = "3s" +const ( + defaultZKAddr = "127.0.0.1:2181" + defaultConsulAddr = "127.0.0.1:8500" + defaultNacosAddr = "127.0.0.1:8848" + defaultRegistryTimeout = "3s" +) func NewDefaultRegistryConfig(protocol string) *RegistryConfig { switch protocol { diff --git a/config/config_api_test.go b/config/config_api_test.go index ca002e24fa..7d0b714464 100644 --- a/config/config_api_test.go +++ b/config/config_api_test.go @@ -130,21 +130,21 @@ func TestNewProviderConfig(t *testing.T) { assert.Equal(t, defaultNacosRegistry.Protocol, "nacos") assert.Equal(t, defaultNacosRegistry.TimeoutStr, defaultRegistryTimeout) - providerConfig := NewProviderConfig( + testProviderConfig := NewProviderConfig( WithProviderServices("UserProvider", serviceConfig), WithProviderProtocol("dubbo", "dubbo", "20000"), WithProviderRegistry("demoConsul", defaultConsulRegistry), WithProviderRegistry("demoNacos", defaultNacosRegistry), ) - assert.NotNil(t, providerConfig.Services) - for k, v := range providerConfig.Services { + assert.NotNil(t, testProviderConfig.Services) + for k, v := range testProviderConfig.Services { assert.Equal(t, k, "UserProvider") assert.Equal(t, v, serviceConfig) } - assert.NotNil(t, providerConfig.Registries) + assert.NotNil(t, testProviderConfig.Registries) i := 0 - for k, v := range providerConfig.Registries { + for k, v := range testProviderConfig.Registries { if i == 0 { assert.Equal(t, k, "demoConsul") assert.Equal(t, v, defaultConsulRegistry) @@ -155,7 +155,7 @@ func TestNewProviderConfig(t *testing.T) { } } - assert.NotNil(t, providerConfig.Protocols) - assert.Equal(t, providerConfig.Protocols["dubbo"].Name, "dubbo") - assert.Equal(t, providerConfig.Protocols["dubbo"].Port, "20000") + assert.NotNil(t, testProviderConfig.Protocols) + assert.Equal(t, testProviderConfig.Protocols["dubbo"].Name, "dubbo") + assert.Equal(t, testProviderConfig.Protocols["dubbo"].Port, "20000") } From cea0a55f3920353884f3827cfef1edb2a4ee419a Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Tue, 26 Jan 2021 18:05:13 +0800 Subject: [PATCH 4/7] fix: add api test --- config/config_api_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/config_api_test.go b/config/config_api_test.go index 7d0b714464..46a441ba4e 100644 --- a/config/config_api_test.go +++ b/config/config_api_test.go @@ -92,7 +92,7 @@ func TestNewConsumerConfig(t *testing.T) { assert.Equal(t, defaultZKRegistry.Protocol, "zookeeper") assert.Equal(t, defaultZKRegistry.TimeoutStr, defaultRegistryTimeout) - consumerConfig := NewConsumerConfig( + testConsumerConfig := NewConsumerConfig( WithConsumerConfigCheck(true), WithConsumerConnTimeout(time.Minute), WithConsumerRequestTimeout(time.Hour), @@ -100,11 +100,11 @@ func TestNewConsumerConfig(t *testing.T) { WithConsumerRegistryConfig("demoZK", defaultZKRegistry), ) - assert.Equal(t, *consumerConfig.Check, true) - assert.Equal(t, consumerConfig.ConnectTimeout, time.Minute) - assert.Equal(t, consumerConfig.RequestTimeout, time.Hour) - assert.Equal(t, consumerConfig.Registries["demoZK"], defaultZKRegistry) - assert.Equal(t, consumerConfig.References["UserProvider"], referConfig) + assert.Equal(t, *testConsumerConfig.Check, true) + assert.Equal(t, testConsumerConfig.ConnectTimeout, time.Minute) + assert.Equal(t, testConsumerConfig.RequestTimeout, time.Hour) + assert.Equal(t, testConsumerConfig.Registries["demoZK"], defaultZKRegistry) + assert.Equal(t, testConsumerConfig.References["UserProvider"], referConfig) } func TestNewProviderConfig(t *testing.T) { From 042a53f7c50c0f3aa53ea124151bfbd029b20b6a Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Tue, 26 Jan 2021 19:33:38 +0800 Subject: [PATCH 5/7] fix: liscence --- config/config_api_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/config/config_api_test.go b/config/config_api_test.go index 46a441ba4e..fd8d5613f4 100644 --- a/config/config_api_test.go +++ b/config/config_api_test.go @@ -1,3 +1,20 @@ +/* + * 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 ( From 8354e91af1210b55ea45913094a6ae7081d0b728 Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Wed, 27 Jan 2021 17:33:50 +0800 Subject: [PATCH 6/7] fix: some style bug --- config/config_api.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/config/config_api.go b/config/config_api.go index a3815b04e1..760975acc1 100644 --- a/config/config_api.go +++ b/config/config_api.go @@ -61,7 +61,7 @@ func NewDefaultRegistryConfig(protocol string) *RegistryConfig { type RegistryConfigOpt func(config *RegistryConfig) *RegistryConfig func NewRegistryConfig(opts ...RegistryConfigOpt) *RegistryConfig { - newRegistryConfig := NewDefaultRegistryConfig("none") + newRegistryConfig := NewDefaultRegistryConfig("") for _, v := range opts { newRegistryConfig = v(newRegistryConfig) } @@ -168,6 +168,7 @@ func NewConsumerConfig(opts ...ConsumerConfigOpt) *ConsumerConfig { } return newConfig } + func WithConsumerAppConfig(appConfig *ApplicationConfig) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.ApplicationConfig = appConfig @@ -241,24 +242,28 @@ func WithReferenceRegistry(registry string) ReferenceConfigOpt { return config } } + func WithReferenceProtocol(protocol string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Protocol = protocol return config } } + func WithReferenceInterface(interfaceName string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.InterfaceName = interfaceName return config } } + func WithReferenceCluster(cluster string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Cluster = cluster return config } } + func WithReferenceMethod(methodName, retries, lb string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Methods = append(config.Methods, &MethodConfig{ @@ -368,12 +373,14 @@ func WithServiceProtocol(protocol string) ServiceConfigOpt { return config } } + func WithServiceInterface(interfaceName string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.InterfaceName = interfaceName return config } } + func WithServiceLoadBalance(lb string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Loadbalance = lb From 2a3269affef68825f6eac366946efdfb6cf2664c Mon Sep 17 00:00:00 2001 From: LaurenceLiZhixin <382673304@qq.com> Date: Wed, 10 Feb 2021 11:17:30 +0800 Subject: [PATCH 7/7] fix: add comment --- config/config_api.go | 82 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 3 deletions(-) diff --git a/config/config_api.go b/config/config_api.go index 760975acc1..eac5a79425 100644 --- a/config/config_api.go +++ b/config/config_api.go @@ -24,12 +24,24 @@ import ( //////////////////////////////////// default registry config const ( - defaultZKAddr = "127.0.0.1:2181" - defaultConsulAddr = "127.0.0.1:8500" - defaultNacosAddr = "127.0.0.1:8848" + // defaultZKAddr is the default registry address of zookeeper + defaultZKAddr = "127.0.0.1:2181" + + // defaultConsulAddr is the default registry address of consul + defaultConsulAddr = "127.0.0.1:8500" + + // defaultNacosAddr is the default registry address of nacos + defaultNacosAddr = "127.0.0.1:8848" + + // defaultRegistryTimeout is the default registry timeout defaultRegistryTimeout = "3s" ) +// NewDefaultRegistryConfig New default registry config +// the input @protocol can only be: +// "zookeeper" with default addr "127.0.0.1:2181" +// "consul" with default addr "127.0.0.1:8500" +// "nacos" with default addr "127.0.0.1:8848" func NewDefaultRegistryConfig(protocol string) *RegistryConfig { switch protocol { case "zookeeper": @@ -60,6 +72,7 @@ func NewDefaultRegistryConfig(protocol string) *RegistryConfig { ///////////////////////////////////// registry config api type RegistryConfigOpt func(config *RegistryConfig) *RegistryConfig +// NewRegistryConfig creates New RegistryConfig with @opts func NewRegistryConfig(opts ...RegistryConfigOpt) *RegistryConfig { newRegistryConfig := NewDefaultRegistryConfig("") for _, v := range opts { @@ -68,6 +81,7 @@ func NewRegistryConfig(opts ...RegistryConfigOpt) *RegistryConfig { return newRegistryConfig } +// WithRegistryProtocol returns RegistryConfigOpt with given @regProtocol name func WithRegistryProtocol(regProtocol string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Protocol = regProtocol @@ -75,6 +89,7 @@ func WithRegistryProtocol(regProtocol string) RegistryConfigOpt { } } +// WithRegistryAddress returns RegistryConfigOpt with given @addr registry address func WithRegistryAddress(addr string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Address = addr @@ -82,6 +97,7 @@ func WithRegistryAddress(addr string) RegistryConfigOpt { } } +// WithRegistryTimeOut returns RegistryConfigOpt with given @timeout registry config func WithRegistryTimeOut(timeout string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.TimeoutStr = timeout @@ -89,6 +105,7 @@ func WithRegistryTimeOut(timeout string) RegistryConfigOpt { } } +// WithRegistryGroup returns RegistryConfigOpt with given @group registry group func WithRegistryGroup(group string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Group = group @@ -96,6 +113,7 @@ func WithRegistryGroup(group string) RegistryConfigOpt { } } +// WithRegistryTTL returns RegistryConfigOpt with given @ttl registry ttl func WithRegistryTTL(ttl string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.TTL = ttl @@ -103,6 +121,7 @@ func WithRegistryTTL(ttl string) RegistryConfigOpt { } } +// WithRegistryUserName returns RegistryConfigOpt with given @userName registry userName func WithRegistryUserName(userName string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Username = userName @@ -110,6 +129,7 @@ func WithRegistryUserName(userName string) RegistryConfigOpt { } } +// WithRegistryPassword returns RegistryConfigOpt with given @psw registry password func WithRegistryPassword(psw string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Password = psw @@ -117,6 +137,7 @@ func WithRegistryPassword(psw string) RegistryConfigOpt { } } +// WithRegistrySimplified returns RegistryConfigOpt with given @simplified registry simplified flag func WithRegistrySimplified(simplified bool) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Simplified = simplified @@ -124,6 +145,7 @@ func WithRegistrySimplified(simplified bool) RegistryConfigOpt { } } +// WithRegistryPreferred returns RegistryConfig with given @preferred registry preferred flag func WithRegistryPreferred(preferred bool) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Preferred = preferred @@ -131,6 +153,7 @@ func WithRegistryPreferred(preferred bool) RegistryConfigOpt { } } +// WithRegistryWeight returns RegistryConfigOpt with given @weight registry weight flag func WithRegistryWeight(weight int64) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Weight = weight @@ -138,6 +161,7 @@ func WithRegistryWeight(weight int64) RegistryConfigOpt { } } +// WithRegistryParams returns RegistryConfigOpt with given registry @params func WithRegistryParams(params map[string]string) RegistryConfigOpt { return func(config *RegistryConfig) *RegistryConfig { config.Params = params @@ -146,8 +170,11 @@ func WithRegistryParams(params map[string]string) RegistryConfigOpt { } ///////////////////////////////////// consumer config api +// ConsumerConfigOpt is the options to init ConsumerConfig type ConsumerConfigOpt func(config *ConsumerConfig) *ConsumerConfig +// NewDefaultConsumerConfig returns default ConsumerConfig +// with connection timeout = 3s, request timeout = 3s func NewDefaultConsumerConfig() *ConsumerConfig { check := true newConsumerConfig := &ConsumerConfig{ @@ -161,6 +188,7 @@ func NewDefaultConsumerConfig() *ConsumerConfig { return newConsumerConfig } +// NewConsumerConfig returns ConsumerConfig with @opts func NewConsumerConfig(opts ...ConsumerConfigOpt) *ConsumerConfig { newConfig := NewDefaultConsumerConfig() for _, v := range opts { @@ -169,6 +197,7 @@ func NewConsumerConfig(opts ...ConsumerConfigOpt) *ConsumerConfig { return newConfig } +// WithConsumerAppConfig returns ConsumerConfigOpt with given @appConfig func WithConsumerAppConfig(appConfig *ApplicationConfig) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.ApplicationConfig = appConfig @@ -176,6 +205,7 @@ func WithConsumerAppConfig(appConfig *ApplicationConfig) ConsumerConfigOpt { } } +// WithConsumerRegistryConfig returns ConsumerConfigOpt with given @registryKey and @regConfig func WithConsumerRegistryConfig(registryKey string, regConfig *RegistryConfig) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.Registries[registryKey] = regConfig @@ -183,6 +213,7 @@ func WithConsumerRegistryConfig(registryKey string, regConfig *RegistryConfig) C } } +// WithConsumerReferenceConfig returns ConsumerConfigOpt with func WithConsumerReferenceConfig(referenceKey string, refConfig *ReferenceConfig) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.References[referenceKey] = refConfig @@ -190,6 +221,7 @@ func WithConsumerReferenceConfig(referenceKey string, refConfig *ReferenceConfig } } +// WithConsumerConnTimeout returns ConsumerConfigOpt with given consumer conn @timeout func WithConsumerConnTimeout(timeout time.Duration) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.ConnectTimeout = timeout @@ -197,6 +229,7 @@ func WithConsumerConnTimeout(timeout time.Duration) ConsumerConfigOpt { } } +// WithConsumerRequestTimeout returns ConsumerConfigOpt with given consumer request @timeout func WithConsumerRequestTimeout(timeout time.Duration) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.RequestTimeout = timeout @@ -204,6 +237,7 @@ func WithConsumerRequestTimeout(timeout time.Duration) ConsumerConfigOpt { } } +// WithConsumerConfigCenterConfig returns ConsumerConfigOpt with given @configCenterConfig func WithConsumerConfigCenterConfig(configCenterConfig *ConfigCenterConfig) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { config.ConfigCenterConfig = configCenterConfig @@ -211,6 +245,7 @@ func WithConsumerConfigCenterConfig(configCenterConfig *ConfigCenterConfig) Cons } } +// WithConsumerConfigCheck returns ConsumerConfigOpt with given @check flag func WithConsumerConfigCheck(check bool) ConsumerConfigOpt { return func(config *ConsumerConfig) *ConsumerConfig { *config.Check = check @@ -219,8 +254,10 @@ func WithConsumerConfigCheck(check bool) ConsumerConfigOpt { } //////////////////////////////////// reference config api +// ReferenceConfigOpt is consumer's reference config type ReferenceConfigOpt func(config *ReferenceConfig) *ReferenceConfig +// NewDefaultReferenceConfig returns empty ReferenceConfig func NewDefaultReferenceConfig() *ReferenceConfig { newReferenceConfig := NewReferenceConfig("", context.Background()) newReferenceConfig.Methods = make([]*MethodConfig, 0, 8) @@ -228,6 +265,7 @@ func NewDefaultReferenceConfig() *ReferenceConfig { return newReferenceConfig } +// NewReferenceConfigByAPI returns ReferenceConfig with given @opts func NewReferenceConfigByAPI(opts ...ReferenceConfigOpt) *ReferenceConfig { newreferenceConfig := NewDefaultReferenceConfig() for _, v := range opts { @@ -236,6 +274,7 @@ func NewReferenceConfigByAPI(opts ...ReferenceConfigOpt) *ReferenceConfig { return newreferenceConfig } +// WithReferenceRegistry returns ReferenceConfigOpt with given registryKey: @registry func WithReferenceRegistry(registry string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Registry = registry @@ -243,6 +282,7 @@ func WithReferenceRegistry(registry string) ReferenceConfigOpt { } } +// WithReferenceProtocol returns ReferenceConfigOpt with given protocolKey: @protocol func WithReferenceProtocol(protocol string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Protocol = protocol @@ -250,6 +290,7 @@ func WithReferenceProtocol(protocol string) ReferenceConfigOpt { } } +// WithReferenceInterface returns ReferenceConfigOpt with given @interfaceName func WithReferenceInterface(interfaceName string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.InterfaceName = interfaceName @@ -257,6 +298,7 @@ func WithReferenceInterface(interfaceName string) ReferenceConfigOpt { } } +// WithReferenceCluster returns ReferenceConfigOpt with given cluster name: @cluster func WithReferenceCluster(cluster string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Cluster = cluster @@ -264,6 +306,7 @@ func WithReferenceCluster(cluster string) ReferenceConfigOpt { } } +// WithReferenceMethod returns ReferenceConfigOpt with given @method, @retries, and load balance: @lb func WithReferenceMethod(methodName, retries, lb string) ReferenceConfigOpt { return func(config *ReferenceConfig) *ReferenceConfig { config.Methods = append(config.Methods, &MethodConfig{ @@ -276,8 +319,10 @@ func WithReferenceMethod(methodName, retries, lb string) ReferenceConfigOpt { } ///////////////////////////////////// provider config api +// ProviderConfigOpt is the type ProviderConfigOpt func(config *ProviderConfig) *ProviderConfig +// NewDefaultProviderConfig returns ProviderConfig with default ApplicationConfig func NewDefaultProviderConfig() *ProviderConfig { newConsumerConfig := &ProviderConfig{ BaseConfig: BaseConfig{ @@ -295,6 +340,7 @@ func NewDefaultProviderConfig() *ProviderConfig { return newConsumerConfig } +// NewProviderConfig returns ProviderConfig with given @opts func NewProviderConfig(opts ...ProviderConfigOpt) *ProviderConfig { newConfig := NewDefaultProviderConfig() for _, v := range opts { @@ -303,6 +349,7 @@ func NewProviderConfig(opts ...ProviderConfigOpt) *ProviderConfig { return newConfig } +// WithPrividerRegistryConfig returns ProviderConfigOpt with given registry config: @regConfig func WithPrividerRegistryConfig(regConfig *RegistryConfig) ProviderConfigOpt { return func(config *ProviderConfig) *ProviderConfig { config.Registries[regConfig.Protocol] = regConfig @@ -310,6 +357,7 @@ func WithPrividerRegistryConfig(regConfig *RegistryConfig) ProviderConfigOpt { } } +// WithProviderAppConfig returns ProviderConfigOpt with given @appConfig func WithProviderAppConfig(appConfig *ApplicationConfig) ProviderConfigOpt { return func(config *ProviderConfig) *ProviderConfig { config.ApplicationConfig = appConfig @@ -317,6 +365,7 @@ func WithProviderAppConfig(appConfig *ApplicationConfig) ProviderConfigOpt { } } +// WithProviderServices returns ProviderConfig with given serviceNameKey @serviceName and @serviceConfig func WithProviderServices(serviceName string, serviceConfig *ServiceConfig) ProviderConfigOpt { return func(config *ProviderConfig) *ProviderConfig { config.Services[serviceName] = serviceConfig @@ -324,6 +373,7 @@ func WithProviderServices(serviceName string, serviceConfig *ServiceConfig) Prov } } +// WithProviderProtocol returns ProviderConfigOpt with given @protocolKey, protocolName @protocol and @port func WithProviderProtocol(protocolKey, protocol, port string) ProviderConfigOpt { return func(config *ProviderConfig) *ProviderConfig { config.Protocols[protocolKey] = &ProtocolConfig{ @@ -334,6 +384,7 @@ func WithProviderProtocol(protocolKey, protocol, port string) ProviderConfigOpt } } +// WithProviderRegistry returns ProviderConfigOpt with given @registryKey and registry @registryConfig func WithProviderRegistry(registryKey string, registryConfig *RegistryConfig) ProviderConfigOpt { return func(config *ProviderConfig) *ProviderConfig { config.Registries[registryKey] = registryConfig @@ -342,8 +393,10 @@ func WithProviderRegistry(registryKey string, registryConfig *RegistryConfig) Pr } /////////////////////////////////////// service config api +// ServiceConfigOpt is the option to init ServiceConfig type ServiceConfigOpt func(config *ServiceConfig) *ServiceConfig +// NewDefaultServiceConfig returns default ServiceConfig func NewDefaultServiceConfig() *ServiceConfig { newServiceConfig := NewServiceConfig("", context.Background()) newServiceConfig.Params = make(map[string]string) @@ -352,6 +405,7 @@ func NewDefaultServiceConfig() *ServiceConfig { } // NewServiceConfigByAPI is named as api, because there is NewServiceConfig func already declared +// NewServiceConfigByAPI returns ServiceConfig with given @opts func NewServiceConfigByAPI(opts ...ServiceConfigOpt) *ServiceConfig { defaultServiceConfig := NewDefaultServiceConfig() for _, v := range opts { @@ -360,6 +414,7 @@ func NewServiceConfigByAPI(opts ...ServiceConfigOpt) *ServiceConfig { return defaultServiceConfig } +// WithServiceRegistry returns ServiceConfigOpt with given registryKey @registry func WithServiceRegistry(registry string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Registry = registry @@ -367,6 +422,7 @@ func WithServiceRegistry(registry string) ServiceConfigOpt { } } +// WithServiceProtocol returns ServiceConfigOpt with given protocolKey @protocol func WithServiceProtocol(protocol string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Protocol = protocol @@ -374,6 +430,7 @@ func WithServiceProtocol(protocol string) ServiceConfigOpt { } } +// WithServiceInterface returns ServiceConfigOpt with given @interfaceName func WithServiceInterface(interfaceName string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.InterfaceName = interfaceName @@ -381,6 +438,7 @@ func WithServiceInterface(interfaceName string) ServiceConfigOpt { } } +// WithServiceLoadBalance returns ServiceConfigOpt with given load balance @lb func WithServiceLoadBalance(lb string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Loadbalance = lb @@ -388,6 +446,7 @@ func WithServiceLoadBalance(lb string) ServiceConfigOpt { } } +// WithServiceWarmUpTime returns ServiceConfigOpt with given @warmUp time func WithServiceWarmUpTime(warmUp string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Warmup = warmUp @@ -395,6 +454,7 @@ func WithServiceWarmUpTime(warmUp string) ServiceConfigOpt { } } +// WithServiceCluster returns ServiceConfigOpt with given cluster name @cluster func WithServiceCluster(cluster string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Cluster = cluster @@ -402,6 +462,7 @@ func WithServiceCluster(cluster string) ServiceConfigOpt { } } +// WithServiceMethod returns ServiceConfigOpt with given @name, @retries and load balance @lb func WithServiceMethod(name, retries, lb string) ServiceConfigOpt { return func(config *ServiceConfig) *ServiceConfig { config.Methods = append(config.Methods, &MethodConfig{ @@ -414,8 +475,16 @@ func WithServiceMethod(name, retries, lb string) ServiceConfigOpt { } ///////////////////////////////////////// Application config api +// ApplicationConfigOpt is option to init ApplicationConfig type ApplicationConfigOpt func(config *ApplicationConfig) *ApplicationConfig +// NewDefaultApplicationConfig returns ApplicationConfig with default +// name: dubbo.io +// module: sample +// organization: dubbo.io +// owner: dubbogo +// version: 0.0.1 +// environment dev func NewDefaultApplicationConfig() *ApplicationConfig { newAppConfig := &ApplicationConfig{ Name: "dubbo.io", @@ -429,6 +498,7 @@ func NewDefaultApplicationConfig() *ApplicationConfig { } // NewApplicationConfig is named as api, because there is NewServiceConfig func already declared +// NewApplicationConfig returns ApplicationConfig wigh default application config func NewApplicationConfig(opts ...ApplicationConfigOpt) *ApplicationConfig { defaultServiceConfig := NewDefaultApplicationConfig() for _, v := range opts { @@ -437,6 +507,7 @@ func NewApplicationConfig(opts ...ApplicationConfigOpt) *ApplicationConfig { return defaultServiceConfig } +// WithAppName returns ApplicationConfigOpt with given @name func WithAppName(name string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Name = name @@ -444,6 +515,7 @@ func WithAppName(name string) ApplicationConfigOpt { } } +// WithAppModule returns ApplicationConfigOpt with given @module func WithAppModule(module string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Module = module @@ -451,6 +523,7 @@ func WithAppModule(module string) ApplicationConfigOpt { } } +// WithAppOrganization returns ApplicationConfigOpt wight given organization @org func WithAppOrganization(org string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Organization = org @@ -458,6 +531,7 @@ func WithAppOrganization(org string) ApplicationConfigOpt { } } +// WithAppOwner returns ApplicationConfigOpt with given @owner func WithAppOwner(owner string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Owner = owner @@ -465,6 +539,7 @@ func WithAppOwner(owner string) ApplicationConfigOpt { } } +// WithAppVersion returns ApplicationConfigOpt with given version @version func WithAppVersion(version string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Version = version @@ -472,6 +547,7 @@ func WithAppVersion(version string) ApplicationConfigOpt { } } +// WithAppEnvironment returns ApplicationConfigOpt wigh given environment @env func WithAppEnvironment(env string) ApplicationConfigOpt { return func(config *ApplicationConfig) *ApplicationConfig { config.Environment = env