You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2023/11/07 07:47:04 UTC

(dubbo-go) branch feature-triple updated: feat: support configuring client.WithURL without protocol prefex (#2484)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch feature-triple
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/feature-triple by this push:
     new b87c3319e feat: support configuring client.WithURL without protocol prefex (#2484)
b87c3319e is described below

commit b87c3319e888e88746b9ceddeaed8ff57ab1c48b
Author: Scout Wang <33...@users.noreply.github.com>
AuthorDate: Tue Nov 7 15:46:59 2023 +0800

    feat: support configuring client.WithURL without protocol prefex (#2484)
---
 client/action.go                                   | 84 ++++++++++++----------
 client/options_test.go                             | 59 +++++++++++++++
 dubbo.go                                           | 14 ++--
 protocol/triple/internal/client/cmd_client/main.go |  2 +-
 .../triple/internal/client/cmd_instance/main.go    |  2 +-
 5 files changed, 113 insertions(+), 48 deletions(-)

diff --git a/client/action.go b/client/action.go
index 58893e561..3e5db00ea 100644
--- a/client/action.go
+++ b/client/action.go
@@ -135,44 +135,8 @@ func (opts *ClientOptions) refer(srv common.RPCService, info *ClientInfo) {
 	updateOrCreateMeshURL(opts)
 
 	// retrieving urls from config, and appending the urls to opts.urls
-	if ref.URL != "" { // use user-specific urls
-		/*
-			 Two types of URL are allowed for opts.URL:
-				1. direct url: server IP, that is, no need for a registry anymore
-				2. registry url
-			 They will be handled in different ways:
-			 For example, we have a direct url and a registry url:
-				1. "tri://localhost:10000" is a direct url
-				2. "registry://localhost:2181" is a registry url.
-			 Then, opts.URL looks like a string separated by semicolon: "tri://localhost:10000;registry://localhost:2181".
-			 The result of urlStrings is a string array: []string{"tri://localhost:10000", "registry://localhost:2181"}.
-		*/
-		urlStrings := gxstrings.RegSplit(ref.URL, "\\s*[;]+\\s*")
-		for _, urlStr := range urlStrings {
-			serviceURL, err := common.NewURL(urlStr)
-			if err != nil {
-				panic(fmt.Sprintf("url configuration error,  please check your configuration, user specified URL %v refer error, error message is %v ", urlStr, err.Error()))
-			}
-			if serviceURL.Protocol == constant.RegistryProtocol { // serviceURL in this branch is a registry protocol
-				serviceURL.SubURL = cfgURL
-				opts.urls = append(opts.urls, serviceURL)
-			} else { // serviceURL in this branch is the target endpoint IP address
-				if serviceURL.Path == "" {
-					serviceURL.Path = "/" + ref.InterfaceName
-				}
-				// replace params of serviceURL with params of cfgUrl
-				// other stuff, e.g. IP, port, etc., are same as serviceURL
-				newURL := common.MergeURL(serviceURL, cfgURL)
-				newURL.AddParam("peer", "true")
-				opts.urls = append(opts.urls, newURL)
-			}
-		}
-	} else { // use registry configs
-		opts.urls = config.LoadRegistries(ref.RegistryIDs, opts.registriesCompat, common.CONSUMER)
-		// set url to regURLs
-		for _, regURL := range opts.urls {
-			regURL.SubURL = cfgURL
-		}
+	if err := opts.processURL(cfgURL); err != nil {
+		panic(err)
 	}
 
 	// Get invokers according to opts.urls
@@ -258,6 +222,50 @@ func (opts *ClientOptions) refer(srv common.RPCService, info *ClientInfo) {
 	graceful_shutdown.RegisterProtocol(ref.Protocol)
 }
 
+func (opts *ClientOptions) processURL(cfgURL *common.URL) error {
+	ref := opts.Reference
+	if ref.URL != "" { // use user-specific urls
+		/*
+			 Two types of URL are allowed for opts.URL:
+				1. direct url: server IP, that is, no need for a registry anymore
+				2. registry url
+			 They will be handled in different ways:
+			 For example, we have a direct url and a registry url:
+				1. "tri://localhost:10000" is a direct url
+				2. "registry://localhost:2181" is a registry url.
+			 Then, opts.URL looks like a string separated by semicolon: "tri://localhost:10000;registry://localhost:2181".
+			 The result of urlStrings is a string array: []string{"tri://localhost:10000", "registry://localhost:2181"}.
+		*/
+		urlStrings := gxstrings.RegSplit(ref.URL, "\\s*[;]+\\s*")
+		for _, urlStr := range urlStrings {
+			serviceURL, err := common.NewURL(urlStr, common.WithProtocol(ref.Protocol))
+			if err != nil {
+				return fmt.Errorf(fmt.Sprintf("url configuration error,  please check your configuration, user specified URL %v refer error, error message is %v ", urlStr, err.Error()))
+			}
+			if serviceURL.Protocol == constant.RegistryProtocol { // serviceURL in this branch is a registry protocol
+				serviceURL.SubURL = cfgURL
+				opts.urls = append(opts.urls, serviceURL)
+			} else { // serviceURL in this branch is the target endpoint IP address
+				if serviceURL.Path == "" {
+					serviceURL.Path = "/" + ref.InterfaceName
+				}
+				// replace params of serviceURL with params of cfgUrl
+				// other stuff, e.g. IP, port, etc., are same as serviceURL
+				newURL := common.MergeURL(serviceURL, cfgURL)
+				newURL.AddParam("peer", "true")
+				opts.urls = append(opts.urls, newURL)
+			}
+		}
+	} else { // use registry configs
+		opts.urls = config.LoadRegistries(ref.RegistryIDs, opts.registriesCompat, common.CONSUMER)
+		// set url to regURLs
+		for _, regURL := range opts.urls {
+			regURL.SubURL = cfgURL
+		}
+	}
+	return nil
+}
+
 func (opts *ClientOptions) CheckAvailable() bool {
 	ref := opts.Reference
 	if opts.invoker == nil {
diff --git a/client/options_test.go b/client/options_test.go
new file mode 100644
index 000000000..6ca8c7610
--- /dev/null
+++ b/client/options_test.go
@@ -0,0 +1,59 @@
+/*
+ * 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 (
+	"dubbo.apache.org/dubbo-go/v3/common"
+	"github.com/stretchr/testify/assert"
+	"testing"
+)
+
+func TestWithURL(t *testing.T) {
+	tests := []struct {
+		opts    []ClientOption
+		justify func(t *testing.T, opts *ClientOptions)
+	}{
+		{
+			opts: []ClientOption{
+				WithURL("127.0.0.1:20000"),
+			},
+			justify: func(t *testing.T, opts *ClientOptions) {
+				urls := opts.urls
+				assert.Equal(t, 1, len(urls))
+				assert.Equal(t, "tri", urls[0].Protocol)
+			},
+		},
+		{
+			opts: []ClientOption{
+				WithURL("tri://127.0.0.1:20000"),
+			},
+			justify: func(t *testing.T, opts *ClientOptions) {
+				urls := opts.urls
+				assert.Equal(t, 1, len(urls))
+				assert.Equal(t, "tri", urls[0].Protocol)
+			},
+		},
+	}
+
+	for _, test := range tests {
+		newOpts := defaultClientOptions()
+		assert.Nil(t, newOpts.init(test.opts...))
+		assert.Nil(t, newOpts.processURL(&common.URL{}))
+		test.justify(t, newOpts)
+	}
+}
diff --git a/dubbo.go b/dubbo.go
index c675959e1..46cdfe563 100644
--- a/dubbo.go
+++ b/dubbo.go
@@ -23,7 +23,6 @@ import (
 
 import (
 	"dubbo.apache.org/dubbo-go/v3/client"
-	"dubbo.apache.org/dubbo-go/v3/global"
 	"dubbo.apache.org/dubbo-go/v3/server"
 )
 
@@ -61,17 +60,16 @@ func (ins *Instance) NewClient(opts ...client.ClientOption) (*client.Client, err
 	regsCfg := ins.insOpts.Registries
 	sdCfg := ins.insOpts.Shutdown
 	if conCfg != nil {
-		refCfg := &global.ReferenceConfig{
-			Check:       &conCfg.Check,
-			Filter:      conCfg.Filter,
-			Protocol:    conCfg.Protocol,
-			RegistryIDs: conCfg.RegistryIDs,
-			TracingKey:  conCfg.TracingKey,
+		if conCfg.Check {
+			cliOpts = append(cliOpts, client.WithCheck())
 		}
 		// these options come from Consumer and Root.
 		// for dubbo-go developers, referring config/ConsumerConfig.Init and config/ReferenceConfig
 		cliOpts = append(cliOpts,
-			client.SetReference(refCfg),
+			client.WithFilter(conCfg.Filter),
+			// todo(DMwangnima): deal with Protocol
+			client.WithRegistryIDs(conCfg.RegistryIDs),
+			// todo(DMwangnima): deal with TracingKey
 			client.SetConsumer(conCfg),
 		)
 	}
diff --git a/protocol/triple/internal/client/cmd_client/main.go b/protocol/triple/internal/client/cmd_client/main.go
index 7adb0fa7c..289b929c5 100644
--- a/protocol/triple/internal/client/cmd_client/main.go
+++ b/protocol/triple/internal/client/cmd_client/main.go
@@ -27,7 +27,7 @@ import (
 func main() {
 	// for the most brief RPC case
 	cli, err := client.NewClient(
-		client.WithURL("tri://127.0.0.1:20000"),
+		client.WithURL("127.0.0.1:20000"),
 	)
 	if err != nil {
 		panic(err)
diff --git a/protocol/triple/internal/client/cmd_instance/main.go b/protocol/triple/internal/client/cmd_instance/main.go
index 262ca49af..098f99c0b 100644
--- a/protocol/triple/internal/client/cmd_instance/main.go
+++ b/protocol/triple/internal/client/cmd_instance/main.go
@@ -36,7 +36,7 @@ func main() {
 	}
 	// configure the params that only client layer cares
 	cli, err := ins.NewClient(
-		client.WithURL("tri://127.0.0.1:20000"),
+		client.WithURL("127.0.0.1:20000"),
 	)
 	if err != nil {
 		panic(err)