Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions client/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (refOpts *ReferenceOptions) refer(srv common.RPCService, info *ClientInfo)
// TODO: remove ISIDL after old triple removed
common.WithParamsValue(constant.IDLMode, ref.IDLMode),
common.WithAttribute(constant.ConsumerConfigKey, refOpts.Consumer),
common.WithAttribute(constant.RegistriesConfigKey, refOpts.Registries),
)

// for new triple IDL mode
Expand Down
52 changes: 42 additions & 10 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
)

type ReferenceOptions struct {
Reference *global.ReferenceConfig
Consumer *global.ConsumerConfig
Metrics *global.MetricsConfig
Otel *global.OtelConfig
TLS *global.TLSConfig
Reference *global.ReferenceConfig
Consumer *global.ConsumerConfig
Metrics *global.MetricsConfig
Otel *global.OtelConfig
TLS *global.TLSConfig
Registries map[string]*global.RegistryConfig

pxy *proxy.Proxy
id string
Expand All @@ -61,10 +62,11 @@

func defaultReferenceOptions() *ReferenceOptions {
return &ReferenceOptions{
Reference: global.DefaultReferenceConfig(),
Metrics: global.DefaultMetricsConfig(),
Otel: global.DefaultOtelConfig(),
TLS: global.DefaultTLSConfig(),
Reference: global.DefaultReferenceConfig(),
Metrics: global.DefaultMetricsConfig(),
Otel: global.DefaultOtelConfig(),
TLS: global.DefaultTLSConfig(),
Registries: global.DefaultRegistriesConfig(),
}
}

Expand Down Expand Up @@ -107,6 +109,19 @@
}

// init registries
// convert Registries to registriesCompat
if refOpts.Registries != nil && len(refOpts.Registries) > 0 {

Check failure on line 113 in client/options.go

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest - Go 1.23)

S1009: should omit nil check; len() for nil maps is defined as zero (staticcheck)
if refOpts.registriesCompat == nil {
refOpts.registriesCompat = make(map[string]*config.RegistryConfig)
}
for id, reg := range refOpts.Registries {
refOpts.registriesCompat[id] = compatRegistryConfig(reg)
if err := refOpts.registriesCompat[id].Init(); err != nil {
return err
}
}
}

if len(refOpts.registriesCompat) > 0 {
regs := refOpts.registriesCompat
if len(refConf.RegistryIDs) <= 0 {
Expand Down Expand Up @@ -193,6 +208,17 @@
}
}

func WithRegistry(opts ...registry.Option) ReferenceOption {
regOpts := registry.NewOptions(opts...)

return func(refOpts *ReferenceOptions) {
if refOpts.Registries == nil {
refOpts.Registries = make(map[string]*global.RegistryConfig)
}
refOpts.Registries[regOpts.ID] = regOpts.Registry
}
}

// ========== Cluster Strategy ==========

func WithClusterAvailable() ReferenceOption {
Expand Down Expand Up @@ -478,6 +504,12 @@
}
}

func setRegistries(regs map[string]*global.RegistryConfig) ReferenceOption {

Check failure on line 507 in client/options.go

View workflow job for this annotation

GitHub Actions / CI (ubuntu-latest - Go 1.23)

func setRegistries is unused (unused)
return func(opts *ReferenceOptions) {
opts.Registries = regs
}
}

type ClientOptions struct {
Consumer *global.ConsumerConfig
Application *global.ApplicationConfig
Expand All @@ -495,7 +527,7 @@
func defaultClientOptions() *ClientOptions {
return &ClientOptions{
Consumer: global.DefaultConsumerConfig(),
Registries: make(map[string]*global.RegistryConfig),
Registries: global.DefaultRegistriesConfig(),
Application: global.DefaultApplicationConfig(),
Shutdown: global.DefaultShutdownConfig(),
Metrics: global.DefaultMetricsConfig(),
Expand Down
13 changes: 3 additions & 10 deletions cluster/router/affinity/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
)

import (
"dubbo.apache.org/dubbo-go/v3/cluster/router"
"dubbo.apache.org/dubbo-go/v3/cluster/router/condition"
"dubbo.apache.org/dubbo-go/v3/common"
conf "dubbo.apache.org/dubbo-go/v3/common/config"
Expand Down Expand Up @@ -85,13 +84,7 @@ type ApplicationAffinityRoute struct {

func newApplicationAffinityRouter(url *common.URL) *ApplicationAffinityRoute {

application, ok := url.GetAttribute(constant.ApplicationKey)
if !ok {
logger.Warnf("ApplicationAffinityRoute url does not have application attribute, url=%s", url)
return nil
}

applicationName := application.(global.ApplicationConfig).Name
applicationName := url.GetParam(constant.ApplicationKey, "")

a := &ApplicationAffinityRoute{
currentApplication: applicationName,
Expand Down Expand Up @@ -225,8 +218,8 @@ func (a *affinityRoute) Notify(_ []base.Invoker) {
panic("this function should not be called")
}

func parseConfig(c string) (router.AffinityRouter, error) {
res := router.AffinityRouter{}
func parseConfig(c string) (global.AffinityRouter, error) {
res := global.AffinityRouter{}
err := yaml.Unmarshal([]byte(c), &res)
return res, err
}
32 changes: 0 additions & 32 deletions cluster/router/config.go

This file was deleted.

4 changes: 2 additions & 2 deletions cluster/router/polaris/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func NewPolarisRouterFactory() router.PriorityRouterFactory {
}

// NewPriorityRouter construct a new PriorityRouter
func (f *RouteFactory) NewPriorityRouter(_ *common.URL) (router.PriorityRouter, error) {
return newPolarisRouter()
func (f *RouteFactory) NewPriorityRouter(url *common.URL) (router.PriorityRouter, error) {
return newPolarisRouter(url)
}
41 changes: 28 additions & 13 deletions cluster/router/polaris/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"dubbo.apache.org/dubbo-go/v3/cluster/router"
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
remotingpolaris "dubbo.apache.org/dubbo-go/v3/remoting/polaris"
"dubbo.apache.org/dubbo-go/v3/remoting/polaris/parser"
Expand All @@ -50,7 +50,16 @@ var (
ErrorPolarisServiceRouteRuleEmpty = errors.New("service route rule is empty")
)

func newPolarisRouter() (*polarisRouter, error) {
func newPolarisRouter(url *common.URL) (*polarisRouter, error) {

// get from url param
applicationName := url.GetParam(constant.ApplicationKey, "")
// get from url attr
registries, ok := url.GetAttribute(constant.RegistriesConfigKey)
if !ok {
registries = make(map[string]*global.RegistryConfig)
}

if err := remotingpolaris.Check(); errors.Is(err, remotingpolaris.ErrorNoOpenPolarisAbility) {
return &polarisRouter{
openRoute: false,
Expand All @@ -67,9 +76,11 @@ func newPolarisRouter() (*polarisRouter, error) {
}

return &polarisRouter{
openRoute: true,
routerAPI: routerAPI,
consumerAPI: consumerAPI,
openRoute: true,
routerAPI: routerAPI,
consumerAPI: consumerAPI,
currentApplication: applicationName,
Registries: registries.(map[string]*global.RegistryConfig),
}, nil
}

Expand All @@ -78,6 +89,10 @@ type polarisRouter struct {

routerAPI polaris.RouterAPI
consumerAPI polaris.ConsumerAPI

// config change: config to global
currentApplication string
Registries map[string]*global.RegistryConfig
}

// Route Determine the target invokers list.
Expand All @@ -94,7 +109,7 @@ func (p *polarisRouter) Route(invokers []base.Invoker, url *common.URL,
return invokers
}

service := getService(url)
service := p.getService(url)
instanceMap := p.buildInstanceMap(service)
if len(instanceMap) == 0 {
return invokers
Expand Down Expand Up @@ -138,17 +153,17 @@ func (p *polarisRouter) Route(invokers []base.Invoker, url *common.URL,
return ret
}

func getService(url *common.URL) string {
func (p *polarisRouter) getService(url *common.URL) string {
applicationMode := false
for _, item := range config.GetRootConfig().Registries {
for _, item := range p.Registries {
if item.Protocol == constant.PolarisKey {
applicationMode = item.RegistryType == constant.ServiceKey
}
}

service := url.Interface()
if applicationMode {
service = config.GetApplicationConfig().Name
service = p.currentApplication
}

return service
Expand Down Expand Up @@ -176,7 +191,7 @@ func (p *polarisRouter) buildRouteRequest(svc string, url *common.URL,
for i := range labels {
label := labels[i]
if strings.Compare(label, model.LabelKeyPath) == 0 {
routeReq.AddArguments(model.BuildPathArgument(getInvokeMethod(url, invocation)))
routeReq.AddArguments(model.BuildPathArgument(p.getInvokeMethod(url, invocation)))
continue
}
if strings.HasPrefix(label, model.LabelKeyHeader) {
Expand Down Expand Up @@ -220,9 +235,9 @@ func (p *polarisRouter) buildTrafficLabels(svc string) ([]string, error) {
return labels, nil
}

func getInvokeMethod(url *common.URL, invoaction base.Invocation) string {
func (p *polarisRouter) getInvokeMethod(url *common.URL, invoaction base.Invocation) string {
applicationMode := false
for _, item := range config.GetRootConfig().Registries {
for _, item := range p.Registries {
if item.Protocol == constant.PolarisKey {
applicationMode = item.RegistryType == constant.ServiceKey
}
Expand Down Expand Up @@ -293,7 +308,7 @@ func (p *polarisRouter) Notify(invokers []base.Invoker) {
if len(invokers) == 0 {
return
}
service := getService(invokers[0].GetURL())
service := p.getService(invokers[0].GetURL())
if service == "" {
logger.Error("url service is empty")
return
Expand Down
8 changes: 4 additions & 4 deletions cluster/router/tag/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func staticTag(invokers []base.Invoker, url *common.URL, invocation base.Invocat
}

// dynamic tag matching. used configuration center to create tag router configuration
func dynamicTag(invokers []base.Invoker, url *common.URL, invocation base.Invocation, cfg config.RouterConfig) []base.Invoker {
func dynamicTag(invokers []base.Invoker, url *common.URL, invocation base.Invocation, cfg global.RouterConfig) []base.Invoker {
tag := invocation.GetAttachmentWithDefaultValue(constant.Tagkey, url.GetParam(constant.Tagkey, ""))
if tag == "" {
return requestEmptyTag(invokers, cfg)
Expand All @@ -73,7 +73,7 @@ func dynamicTag(invokers []base.Invoker, url *common.URL, invocation base.Invoca
// if request.tag is not set, only providers with empty tags will be matched.
// even if a service is available in the cluster, it cannot be invoked if the tag does not match,
// and requests without tags or other tags will never be able to access services with other tags.
func requestEmptyTag(invokers []base.Invoker, cfg config.RouterConfig) []base.Invoker {
func requestEmptyTag(invokers []base.Invoker, cfg global.RouterConfig) []base.Invoker {
result := filterInvokers(invokers, "", func(invoker base.Invoker, tag any) bool {
return invoker.GetURL().GetParam(constant.Tagkey, "") != ""
})
Expand All @@ -95,7 +95,7 @@ func requestEmptyTag(invokers []base.Invoker, cfg config.RouterConfig) []base.In
// if no service corresponding to the request tag exists in the cluster,
// the provider with the empty request tag is degraded by default.
// to change the default behavior that no provider matching TAG1 returns an exception, set request.tag.force=true.
func requestTag(invokers []base.Invoker, url *common.URL, invocation base.Invocation, cfg config.RouterConfig, tag string) []base.Invoker {
func requestTag(invokers []base.Invoker, url *common.URL, invocation base.Invocation, cfg global.RouterConfig, tag string) []base.Invoker {
var (
addresses []string
result []base.Invoker
Expand Down
8 changes: 4 additions & 4 deletions cluster/router/tag/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ import (
"dubbo.apache.org/dubbo-go/v3/common"
conf "dubbo.apache.org/dubbo-go/v3/common/config"
"dubbo.apache.org/dubbo-go/v3/common/constant"
"dubbo.apache.org/dubbo-go/v3/config"
"dubbo.apache.org/dubbo-go/v3/config_center"
"dubbo.apache.org/dubbo-go/v3/global"
"dubbo.apache.org/dubbo-go/v3/protocol/base"
"dubbo.apache.org/dubbo-go/v3/remoting"
)
Expand All @@ -58,7 +58,7 @@ func (p *PriorityRouter) Route(invokers []base.Invoker, url *common.URL, invocat
if !ok {
return staticTag(invokers, url, invocation)
}
routerCfg := value.(config.RouterConfig)
routerCfg := value.(global.RouterConfig)
if !*routerCfg.Enabled || !*routerCfg.Valid {
return staticTag(invokers, url, invocation)
}
Expand Down Expand Up @@ -116,9 +116,9 @@ func (p *PriorityRouter) Process(event *config_center.ConfigChangeEvent) {
logger.Infof("[tag router]Parse tag router config success,routerConfig=%+v", routerConfig)
}

func parseRoute(routeContent string) (*config.RouterConfig, error) {
func parseRoute(routeContent string) (*global.RouterConfig, error) {
routeDecoder := yaml.NewDecoder(strings.NewReader(routeContent))
routerConfig := &config.RouterConfig{}
routerConfig := &global.RouterConfig{}
err := routeDecoder.Decode(routerConfig)
if err != nil {
return nil, err
Expand Down
Loading
Loading