This is an automated email from the ASF dual-hosted git repository.
wuxinfan pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git
The following commit(s) were added to refs/heads/develop by this push:
new bd9cc141a fix: Resolve URL parsing panic and improve error handling in
dubbogo-cli for ZooKeeper (#2795)
bd9cc141a is described below
commit bd9cc141a12594ff746da33e141d94584f234422
Author: Lavi Khandelwal <[email protected]>
AuthorDate: Fri Mar 21 08:36:34 2025 +0530
fix: Resolve URL parsing panic and improve error handling in dubbogo-cli
for ZooKeeper (#2795)
* issue 2681 fixes
* fixes
---
tools/dubbogo-cli/cmd/show.go | 67 ++++++++++++---------
tools/dubbogo-cli/metadata/zookeeper/zookeeper.go | 71 ++++++++++++++++++-----
2 files changed, 95 insertions(+), 43 deletions(-)
diff --git a/tools/dubbogo-cli/cmd/show.go b/tools/dubbogo-cli/cmd/show.go
index 69c23c956..e92fc68a3 100644
--- a/tools/dubbogo-cli/cmd/show.go
+++ b/tools/dubbogo-cli/cmd/show.go
@@ -26,7 +26,6 @@ import (
import (
"github.com/olekukonko/tablewriter"
-
"github.com/spf13/cobra"
)
@@ -38,17 +37,16 @@ import (
// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show",
- Short: "show interfaces and methods",
- Long: ``,
+ Short: "Show interfaces and methods from registry or metadata center",
+ Long: `Displays available interfaces and their methods from the
specified registry or metadata center.`,
Run: show,
}
func init() {
rootCmd.AddCommand(showCmd)
- showCmd.Flags().String("r", "", "")
- showCmd.Flags().String("mc", "", "Get Metadata in MetadataCenter")
- showCmd.Flags().String("h", "h", "")
-
+ showCmd.Flags().String("r", "", "Registry type (e.g., zookeeper)")
+ showCmd.Flags().String("mc", "", "Metadata center type (e.g.,
zookeeper)")
+ showCmd.Flags().String("h", "", "Host address of registry or metadata
center (e.g., 127.0.0.1:2181)")
}
func show(cmd *cobra.Command, _ []string) {
@@ -57,19 +55,18 @@ func show(cmd *cobra.Command, _ []string) {
err error
)
- registry, err := cmd.Flags().GetString("r")
- if err != nil {
- panic(err)
- }
+ registry, _ := cmd.Flags().GetString("r")
+ metadataCenter, _ := cmd.Flags().GetString("mc")
+ host, _ := cmd.Flags().GetString("h")
- metadataCenter, err := cmd.Flags().GetString("mc")
- if err != nil {
- panic(err)
+ // Validate inputs
+ if registry == "" && metadataCenter == "" {
+ log.Println("Error: At least one of --r (registry) or --mc
(metadata center) must be specified")
+ return
}
-
- host, err := cmd.Flags().GetString("h")
- if err != nil {
- panic(err)
+ if host == "" {
+ log.Println("Error: Host (--h) is required")
+ return
}
table := tablewriter.NewWriter(os.Stdout)
@@ -78,34 +75,46 @@ func show(cmd *cobra.Command, _ []string) {
if registry != "" {
registryFactory, ok := metadata.GetFactory(registry)
if !ok {
- log.Print("registry not support")
+ log.Printf("Error: Registry type '%s' is not
supported", registry)
return
}
- methodsMap, err = registryFactory("dubbogo-cli",
[]string{host}).ShowRegistryCenterChildren()
+ // Pass the raw host address instead of constructing a URL
+ hostArg := []string{host}
+ methodsMap, err = registryFactory("dubbogo-cli",
hostArg).ShowRegistryCenterChildren()
if err != nil {
- panic(err)
+ log.Printf("Failed to fetch registry data from %s: %v",
registry, err)
+ fmt.Printf("======================\n")
+ fmt.Printf("Registry (%s): No data available\n",
registry)
+ fmt.Printf("======================\n")
+ return
}
fmt.Printf("======================\n")
- fmt.Printf("Registry:\n")
- for k, v := range methodsMap {
- table.Append([]string{k, strings.Join(v, ", ")})
+ fmt.Printf("Registry (%s):\n", registry)
+ if len(methodsMap) == 0 {
+ fmt.Println("No interfaces found")
+ } else {
+ for k, v := range methodsMap {
+ table.Append([]string{k, strings.Join(v, ", ")})
+ }
+ table.Render()
}
- table.Render()
fmt.Printf("======================\n")
}
if metadataCenter != "" {
metadataCenterFactory, ok := metadata.GetFactory(metadataCenter)
if !ok {
- log.Print("metadataCenter not support")
+ log.Printf("Error: Metadata center type '%s' is not
supported", metadataCenter)
return
}
- methodsMap, err = metadataCenterFactory("dubbogo-cli",
[]string{host}).ShowMetadataCenterChildren()
+ hostArg := []string{host}
+ methodsMap, err = metadataCenterFactory("dubbogo-cli",
hostArg).ShowMetadataCenterChildren()
if err != nil {
- panic(err)
+ log.Printf("Error fetching metadata center data: %v",
err)
+ return
}
fmt.Printf("======================\n")
- fmt.Printf("MetadataCenter:\n")
+ fmt.Printf("MetadataCenter (%s):\n", metadataCenter)
for k, v := range methodsMap {
table.Append([]string{k, strings.Join(v, ", ")})
}
diff --git a/tools/dubbogo-cli/metadata/zookeeper/zookeeper.go
b/tools/dubbogo-cli/metadata/zookeeper/zookeeper.go
index 1bead7b57..45f716cfc 100644
--- a/tools/dubbogo-cli/metadata/zookeeper/zookeeper.go
+++ b/tools/dubbogo-cli/metadata/zookeeper/zookeeper.go
@@ -21,6 +21,7 @@ import (
"encoding/json"
"fmt"
"log"
+ "strings"
"time"
)
@@ -38,18 +39,36 @@ func init() {
metadata.Register("zookeeper", NewZookeeperMetadataReport)
}
-// ZookeeperMetadataReport is the implementation of
-// MetadataReport based on zookeeper.
+// ZookeeperMetadataReport is the implementation of MetadataReport based on
ZooKeeper.
type ZookeeperMetadataReport struct {
client *gxzookeeper.ZookeeperClient
rootDir string
+ zkAddr string // Store the ZooKeeper address for URL construction
}
-// NewZookeeperMetadataReport create a zookeeper metadata reporter
+// NewZookeeperMetadataReport creates a ZooKeeper metadata reporter
func NewZookeeperMetadataReport(name string, zkAddrs []string)
metadata.MetaData {
+ if len(zkAddrs) == 0 || zkAddrs[0] == "" {
+ panic("No ZooKeeper address provided")
+ }
+ // Strip scheme if present (e.g., "zookeeper://127.0.0.1:2181" ->
"127.0.0.1:2181")
+ cleanAddrs := make([]string, len(zkAddrs))
+ for i, addr := range zkAddrs {
+ if strings.Contains(addr, "://") {
+ parts := strings.SplitN(addr, "://", 2)
+ if len(parts) == 2 {
+ cleanAddrs[i] = parts[1]
+ } else {
+ cleanAddrs[i] = addr
+ }
+ } else {
+ cleanAddrs[i] = addr
+ }
+ }
+
client, err := gxzookeeper.NewZookeeperClient(
name,
- zkAddrs,
+ cleanAddrs,
false,
gxzookeeper.WithZkTimeOut(15*time.Second))
if err != nil {
@@ -58,24 +77,25 @@ func NewZookeeperMetadataReport(name string, zkAddrs
[]string) metadata.MetaData
return &ZookeeperMetadataReport{
client: client,
rootDir: "/dubbo",
+ zkAddr: cleanAddrs[0], // Store the cleaned address
}
}
-// GetChildren get children node
+// GetChildren gets children nodes under a path
func (z *ZookeeperMetadataReport) GetChildren(path string) ([]string, error) {
delimiter := "/"
if path == "" {
- delimiter = path
+ delimiter = ""
}
return z.client.GetChildren(z.rootDir + delimiter + path)
}
-// ShowRegistryCenterChildren show children list
+// ShowRegistryCenterChildren shows children list from the registry
func (z *ZookeeperMetadataReport) ShowRegistryCenterChildren()
(map[string][]string, error) {
methodsMap := map[string][]string{}
inters, err := z.GetChildren("")
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("failed to get root children: %v", err)
}
for _, inter := range inters {
if _, ok := methodsMap[inter]; !ok {
@@ -84,19 +104,39 @@ func (z *ZookeeperMetadataReport)
ShowRegistryCenterChildren() (map[string][]str
interChildren, err := z.GetChildren(inter)
if err != nil {
- return nil, err
+ log.Printf("Failed to get children for %s: %v", inter,
err)
+ continue
}
for _, interChild := range interChildren {
interURLs, err := z.GetChildren(inter + "/" +
interChild)
if err != nil {
- log.Println(err)
+ log.Printf("Failed to get URLs for %s/%s: %v",
inter, interChild, err)
+ continue
}
for _, interURL := range interURLs {
- url, err := common.NewURL(interURL)
+ // Fetch the content of the node instead of
using the node name
+ fullPath := z.rootDir + "/" + inter + "/" +
interChild + "/" + interURL
+ content, _, err := z.client.GetContent(fullPath)
if err != nil {
- return nil, err
+ log.Printf("Failed to get content for
%s: %v", fullPath, err)
+ continue
+ }
+ log.Printf("Processing interURL content: %s",
string(content))
+
+ urlStr := string(content)
+ if !strings.Contains(urlStr, "://") {
+ urlStr =
fmt.Sprintf("zookeeper://%s/%s", z.zkAddr, strings.TrimLeft(urlStr, "/"))
+ }
+
+ url, err := common.NewURL(urlStr)
+ if err != nil {
+ log.Printf("Failed to parse URL %s:
%v", urlStr, err)
+ continue
+ }
+ methods := url.GetParam("methods", "")
+ if methods != "" {
+ methodsMap[inter] =
append(methodsMap[inter], strings.Split(methods, ",")...)
}
- methodsMap[inter] = append(methodsMap[inter],
url.GetParam("methods", ""))
}
}
}
@@ -107,11 +147,12 @@ func (z *ZookeeperMetadataReport)
ShowRegistryCenterChildren() (map[string][]str
return methodsMap, nil
}
+// ShowMetadataCenterChildren shows children list from the metadata center
func (z *ZookeeperMetadataReport) ShowMetadataCenterChildren()
(map[string][]string, error) {
methodsMap := map[string][]string{}
inters, err := z.GetChildren("metadata")
if err != nil {
- return nil, err
+ return nil, fmt.Errorf("failed to get metadata children: %v",
err)
}
for _, inter := range inters {
path := "metadata/" + inter
@@ -128,9 +169,11 @@ func (z *ZookeeperMetadataReport)
ShowMetadataCenterChildren() (map[string][]str
return methodsMap, nil
}
+// searchMetadataProvider recursively searches for provider metadata
func (z *ZookeeperMetadataReport) searchMetadataProvider(path string, methods
*[]string) {
interChildren, err := z.GetChildren(path)
if err != nil {
+ log.Printf("Failed to get children for %s: %v", path, err)
return
}
for _, interChild := range interChildren {