Skip to content

Commit

Permalink
feat: refactor client module to resolve circular dependencies (#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
DMwangnima committed Aug 28, 2023
1 parent 7c678ea commit 2278939
Show file tree
Hide file tree
Showing 19 changed files with 1,027 additions and 564 deletions.
22 changes: 18 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -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 client

import (
Expand Down Expand Up @@ -98,10 +115,7 @@ func generateInvocation(methodName string, paramsRawVals []interface{}, callType
}

func NewClient(opts ...ReferenceOption) (*Client, error) {
// get default RootConfigs
//rootCfg := config.NewRootConfigBuilder().Build()
//rootCfg.Init()
// todo: create a default ReferenceConfig
// todo(DMwangnima): create a default ReferenceConfig
newRefCfg := &ReferenceConfig{}
if err := newRefCfg.Init(opts...); err != nil {
return nil, err
Expand Down
62 changes: 62 additions & 0 deletions client/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 client

import (
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/registry"
)

// these functions are used to resolve circular dependencies temporarily.
// please refer to issue(https://github.com/apache/dubbo-go/issues/2377)
// todo(DMwangnima): remove these functions when refactoring dubbo-go
func compatApplicationConfig(c *commonCfg.ApplicationConfig) *config.ApplicationConfig {
return &config.ApplicationConfig{
Organization: c.Organization,
Name: c.Name,
Module: c.Module,
Group: c.Group,
Version: c.Version,
Owner: c.Owner,
Environment: c.Environment,
MetadataType: c.MetadataType,
Tag: c.Tag,
}
}

func compatRegistryConfig(c *registry.RegistryConfig) *config.RegistryConfig {
return &config.RegistryConfig{
Protocol: c.Protocol,
Timeout: c.Timeout,
Group: c.Group,
Namespace: c.Namespace,
TTL: c.TTL,
Address: c.Address,
Username: c.Username,
Password: c.Password,
Simplified: c.Simplified,
Preferred: c.Preferred,
Zone: c.Zone,
Weight: c.Weight,
Params: c.Params,
RegistryType: c.RegistryType,
UseAsMetaReport: c.UseAsMetaReport,
UseAsConfigCenter: c.UseAsConfigCenter,
}
}
17 changes: 17 additions & 0 deletions client/options.go
Original file line number Diff line number Diff line change
@@ -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 client

import (
Expand Down
72 changes: 48 additions & 24 deletions client/reference_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/protocol"
"dubbo.apache.org/dubbo-go/v3/protocol/protocolwrapper"
"dubbo.apache.org/dubbo-go/v3/proxy"
Expand Down Expand Up @@ -84,40 +85,59 @@ type ReferenceConfig struct {
adaptiveService bool
proxyFactory string

application *commonCfg.ApplicationConfig
application *commonCfg.ApplicationConfig
applicationCompat *config.ApplicationConfig

registries map[string]*registry.RegistryConfig
registries map[string]*registry.RegistryConfig
registriesCompat map[string]*config.RegistryConfig
}

func (rc *ReferenceConfig) Prefix() string {
return constant.ReferenceConfigPrefix + rc.InterfaceName + "."
}

func (rc *ReferenceConfig) Init(opts ...ReferenceOption) error {
for _, method := range rc.Methods {
if err := method.Init(); err != nil {
return err
}
}
if err := defaults.Set(rc); err != nil {
return err
}
for _, opt := range opts {
opt(rc)
}
// init method
for _, method := range rc.Methods {
if err := method.Init(); err != nil {
return err
}
}
// init application
if rc.application != nil {
rc.metaDataType = rc.application.MetadataType
rc.applicationCompat = compatApplicationConfig(rc.application)
if err := rc.applicationCompat.Init(); err != nil {
return err
}
rc.metaDataType = rc.applicationCompat.MetadataType
if rc.Group == "" {
rc.Group = rc.application.Group
rc.Group = rc.applicationCompat.Group
}
if rc.Version == "" {
rc.Version = rc.application.Version
rc.Version = rc.applicationCompat.Version
}
}
// init cluster
if rc.Cluster == "" {
rc.Cluster = "failover"
}
// todo: move to registry package
// init registries
if rc.registries != nil {
rc.registriesCompat = make(map[string]*config.RegistryConfig)
for key, reg := range rc.registries {
rc.registriesCompat[key] = compatRegistryConfig(reg)
if err := rc.registriesCompat[key].Init(); err != nil {
return err
}
}
}
rc.RegistryIDs = commonCfg.TranslateIds(rc.RegistryIDs)

return commonCfg.Verify(rc)
Expand Down Expand Up @@ -158,16 +178,20 @@ func updateOrCreateMeshURL(rc *ReferenceConfig) {
rc.URL = "tri://" + rc.ProvidedBy + "." + podNamespace + constant.SVC + clusterDomain + ":" + strconv.Itoa(meshPort)
}

// Refer retrieves invokers from urls.
func (rc *ReferenceConfig) Refer(srv interface{}) {
rc.refer(nil, srv)
// ReferWithService retrieves invokers from urls.
func (rc *ReferenceConfig) ReferWithService(srv common.RPCService) {
rc.refer(srv, nil)
}

func (rc *ReferenceConfig) ReferWithInfo(info *ClientInfo) {
rc.refer(info, nil)
rc.refer(nil, info)
}

func (rc *ReferenceConfig) ReferWithServiceAndInfo(srv common.RPCService, info *ClientInfo) {
rc.refer(srv, info)
}

func (rc *ReferenceConfig) refer(info *ClientInfo, srv interface{}) {
func (rc *ReferenceConfig) refer(srv common.RPCService, info *ClientInfo) {
var methods []string
if info != nil {
rc.InterfaceName = info.InterfaceName
Expand Down Expand Up @@ -236,7 +260,7 @@ func (rc *ReferenceConfig) refer(info *ClientInfo, srv interface{}) {
}
}
} else { // use registry configs
rc.urls = registry.LoadRegistries(rc.RegistryIDs, rc.registries, common.CONSUMER)
rc.urls = config.LoadRegistries(rc.RegistryIDs, rc.registriesCompat, common.CONSUMER)
// set url to regURLs
for _, regURL := range rc.urls {
regURL.SubURL = cfgURL
Expand Down Expand Up @@ -382,14 +406,14 @@ func (rc *ReferenceConfig) getURLMap() url.Values {
urlMap.Set(constant.StickyKey, strconv.FormatBool(rc.Sticky))

// applicationConfig info
if rc.application != nil {
urlMap.Set(constant.ApplicationKey, rc.application.Name)
urlMap.Set(constant.OrganizationKey, rc.application.Organization)
urlMap.Set(constant.NameKey, rc.application.Name)
urlMap.Set(constant.ModuleKey, rc.application.Module)
urlMap.Set(constant.AppVersionKey, rc.application.Version)
urlMap.Set(constant.OwnerKey, rc.application.Owner)
urlMap.Set(constant.EnvironmentKey, rc.application.Environment)
if rc.applicationCompat != nil {
urlMap.Set(constant.ApplicationKey, rc.applicationCompat.Name)
urlMap.Set(constant.OrganizationKey, rc.applicationCompat.Organization)
urlMap.Set(constant.NameKey, rc.applicationCompat.Name)
urlMap.Set(constant.ModuleKey, rc.applicationCompat.Module)
urlMap.Set(constant.AppVersionKey, rc.applicationCompat.Version)
urlMap.Set(constant.OwnerKey, rc.applicationCompat.Owner)
urlMap.Set(constant.EnvironmentKey, rc.applicationCompat.Environment)
}

// filter
Expand Down
33 changes: 1 addition & 32 deletions common/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,7 @@

package config

import (
"github.com/creasty/defaults"

"github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)
// todo(DMwangnima): think about the location of this type of configuration.

// ApplicationConfig is a configuration for current applicationConfig, whether the applicationConfig is a provider or a consumer
type ApplicationConfig struct {
Expand All @@ -41,29 +33,6 @@ type ApplicationConfig struct {
Tag string `yaml:"tag" json:"tag,omitempty" property:"tag"`
}

// Prefix dubbo.application
func (ac *ApplicationConfig) Prefix() string {
return constant.ApplicationConfigPrefix
}

// Init application config and set default value
func (ac *ApplicationConfig) Init() error {
if ac == nil {
return errors.New("application is null")
}
if err := ac.check(); err != nil {
return err
}
return nil
}

func (ac *ApplicationConfig) check() error {
if err := defaults.Set(ac); err != nil {
return err
}
return Verify(ac)
}

type ApplicationOption func(*ApplicationConfig)

func WithOrganization(organization string) ApplicationOption {
Expand Down
51 changes: 47 additions & 4 deletions config/application_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,58 @@
package config

import (
commonCfg "dubbo.apache.org/dubbo-go/v3/common/config"
"github.com/creasty/defaults"

"github.com/pkg/errors"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/constant"
)

// ApplicationConfig is a configuration for current applicationConfig, whether the applicationConfig is a provider or a consumer
type ApplicationConfig struct {
Organization string `default:"dubbo-go" yaml:"organization" json:"organization,omitempty" property:"organization"`
Name string `default:"dubbo.io" yaml:"name" json:"name,omitempty" property:"name"`
Module string `default:"sample" yaml:"module" json:"module,omitempty" property:"module"`
Group string `yaml:"group" json:"group,omitempty" property:"module"`
Version string `yaml:"version" json:"version,omitempty" property:"version"`
Owner string `default:"dubbo-go" yaml:"owner" json:"owner,omitempty" property:"owner"`
Environment string `yaml:"environment" json:"environment,omitempty" property:"environment"`
// the metadata type. remote or local
MetadataType string `default:"local" yaml:"metadata-type" json:"metadataType,omitempty" property:"metadataType"`
Tag string `yaml:"tag" json:"tag,omitempty" property:"tag"`
}

// Prefix dubbo.application
func (ac *ApplicationConfig) Prefix() string {
return constant.ApplicationConfigPrefix
}

// Init application config and set default value
func (ac *ApplicationConfig) Init() error {
if ac == nil {
return errors.New("application is null")
}
if err := ac.check(); err != nil {
return err
}
return nil
}

func (ac *ApplicationConfig) check() error {
if err := defaults.Set(ac); err != nil {
return err
}
return verify(ac)
}

func NewApplicationConfigBuilder() *ApplicationConfigBuilder {
return &ApplicationConfigBuilder{application: &commonCfg.ApplicationConfig{}}
return &ApplicationConfigBuilder{application: &ApplicationConfig{}}
}

type ApplicationConfigBuilder struct {
application *commonCfg.ApplicationConfig
application *ApplicationConfig
}

func (acb *ApplicationConfigBuilder) SetOrganization(organization string) *ApplicationConfigBuilder {
Expand Down Expand Up @@ -64,6 +107,6 @@ func (acb *ApplicationConfigBuilder) SetMetadataType(metadataType string) *Appli
return acb
}

func (acb *ApplicationConfigBuilder) Build() *commonCfg.ApplicationConfig {
func (acb *ApplicationConfigBuilder) Build() *ApplicationConfig {
return acb.application
}
Loading

0 comments on commit 2278939

Please sign in to comment.