Skip to content

Commit bd9cc14

Browse files
authored
fix: Resolve URL parsing panic and improve error handling in dubbogo-cli for ZooKeeper (#2795)
* issue 2681 fixes * fixes
1 parent 88823f4 commit bd9cc14

File tree

2 files changed

+95
-43
lines changed

2 files changed

+95
-43
lines changed

tools/dubbogo-cli/cmd/show.go

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
import (
2828
"github.com/olekukonko/tablewriter"
29-
3029
"github.com/spf13/cobra"
3130
)
3231

@@ -38,17 +37,16 @@ import (
3837
// showCmd represents the show command
3938
var showCmd = &cobra.Command{
4039
Use: "show",
41-
Short: "show interfaces and methods",
42-
Long: ``,
40+
Short: "Show interfaces and methods from registry or metadata center",
41+
Long: `Displays available interfaces and their methods from the specified registry or metadata center.`,
4342
Run: show,
4443
}
4544

4645
func init() {
4746
rootCmd.AddCommand(showCmd)
48-
showCmd.Flags().String("r", "", "")
49-
showCmd.Flags().String("mc", "", "Get Metadata in MetadataCenter")
50-
showCmd.Flags().String("h", "h", "")
51-
47+
showCmd.Flags().String("r", "", "Registry type (e.g., zookeeper)")
48+
showCmd.Flags().String("mc", "", "Metadata center type (e.g., zookeeper)")
49+
showCmd.Flags().String("h", "", "Host address of registry or metadata center (e.g., 127.0.0.1:2181)")
5250
}
5351

5452
func show(cmd *cobra.Command, _ []string) {
@@ -57,19 +55,18 @@ func show(cmd *cobra.Command, _ []string) {
5755
err error
5856
)
5957

60-
registry, err := cmd.Flags().GetString("r")
61-
if err != nil {
62-
panic(err)
63-
}
58+
registry, _ := cmd.Flags().GetString("r")
59+
metadataCenter, _ := cmd.Flags().GetString("mc")
60+
host, _ := cmd.Flags().GetString("h")
6461

65-
metadataCenter, err := cmd.Flags().GetString("mc")
66-
if err != nil {
67-
panic(err)
62+
// Validate inputs
63+
if registry == "" && metadataCenter == "" {
64+
log.Println("Error: At least one of --r (registry) or --mc (metadata center) must be specified")
65+
return
6866
}
69-
70-
host, err := cmd.Flags().GetString("h")
71-
if err != nil {
72-
panic(err)
67+
if host == "" {
68+
log.Println("Error: Host (--h) is required")
69+
return
7370
}
7471

7572
table := tablewriter.NewWriter(os.Stdout)
@@ -78,34 +75,46 @@ func show(cmd *cobra.Command, _ []string) {
7875
if registry != "" {
7976
registryFactory, ok := metadata.GetFactory(registry)
8077
if !ok {
81-
log.Print("registry not support")
78+
log.Printf("Error: Registry type '%s' is not supported", registry)
8279
return
8380
}
84-
methodsMap, err = registryFactory("dubbogo-cli", []string{host}).ShowRegistryCenterChildren()
81+
// Pass the raw host address instead of constructing a URL
82+
hostArg := []string{host}
83+
methodsMap, err = registryFactory("dubbogo-cli", hostArg).ShowRegistryCenterChildren()
8584
if err != nil {
86-
panic(err)
85+
log.Printf("Failed to fetch registry data from %s: %v", registry, err)
86+
fmt.Printf("======================\n")
87+
fmt.Printf("Registry (%s): No data available\n", registry)
88+
fmt.Printf("======================\n")
89+
return
8790
}
8891
fmt.Printf("======================\n")
89-
fmt.Printf("Registry:\n")
90-
for k, v := range methodsMap {
91-
table.Append([]string{k, strings.Join(v, ", ")})
92+
fmt.Printf("Registry (%s):\n", registry)
93+
if len(methodsMap) == 0 {
94+
fmt.Println("No interfaces found")
95+
} else {
96+
for k, v := range methodsMap {
97+
table.Append([]string{k, strings.Join(v, ", ")})
98+
}
99+
table.Render()
92100
}
93-
table.Render()
94101
fmt.Printf("======================\n")
95102
}
96103

97104
if metadataCenter != "" {
98105
metadataCenterFactory, ok := metadata.GetFactory(metadataCenter)
99106
if !ok {
100-
log.Print("metadataCenter not support")
107+
log.Printf("Error: Metadata center type '%s' is not supported", metadataCenter)
101108
return
102109
}
103-
methodsMap, err = metadataCenterFactory("dubbogo-cli", []string{host}).ShowMetadataCenterChildren()
110+
hostArg := []string{host}
111+
methodsMap, err = metadataCenterFactory("dubbogo-cli", hostArg).ShowMetadataCenterChildren()
104112
if err != nil {
105-
panic(err)
113+
log.Printf("Error fetching metadata center data: %v", err)
114+
return
106115
}
107116
fmt.Printf("======================\n")
108-
fmt.Printf("MetadataCenter:\n")
117+
fmt.Printf("MetadataCenter (%s):\n", metadataCenter)
109118
for k, v := range methodsMap {
110119
table.Append([]string{k, strings.Join(v, ", ")})
111120
}

tools/dubbogo-cli/metadata/zookeeper/zookeeper.go

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"fmt"
2323
"log"
24+
"strings"
2425
"time"
2526
)
2627

@@ -38,18 +39,36 @@ func init() {
3839
metadata.Register("zookeeper", NewZookeeperMetadataReport)
3940
}
4041

41-
// ZookeeperMetadataReport is the implementation of
42-
// MetadataReport based on zookeeper.
42+
// ZookeeperMetadataReport is the implementation of MetadataReport based on ZooKeeper.
4343
type ZookeeperMetadataReport struct {
4444
client *gxzookeeper.ZookeeperClient
4545
rootDir string
46+
zkAddr string // Store the ZooKeeper address for URL construction
4647
}
4748

48-
// NewZookeeperMetadataReport create a zookeeper metadata reporter
49+
// NewZookeeperMetadataReport creates a ZooKeeper metadata reporter
4950
func NewZookeeperMetadataReport(name string, zkAddrs []string) metadata.MetaData {
51+
if len(zkAddrs) == 0 || zkAddrs[0] == "" {
52+
panic("No ZooKeeper address provided")
53+
}
54+
// Strip scheme if present (e.g., "zookeeper://127.0.0.1:2181" -> "127.0.0.1:2181")
55+
cleanAddrs := make([]string, len(zkAddrs))
56+
for i, addr := range zkAddrs {
57+
if strings.Contains(addr, "://") {
58+
parts := strings.SplitN(addr, "://", 2)
59+
if len(parts) == 2 {
60+
cleanAddrs[i] = parts[1]
61+
} else {
62+
cleanAddrs[i] = addr
63+
}
64+
} else {
65+
cleanAddrs[i] = addr
66+
}
67+
}
68+
5069
client, err := gxzookeeper.NewZookeeperClient(
5170
name,
52-
zkAddrs,
71+
cleanAddrs,
5372
false,
5473
gxzookeeper.WithZkTimeOut(15*time.Second))
5574
if err != nil {
@@ -58,24 +77,25 @@ func NewZookeeperMetadataReport(name string, zkAddrs []string) metadata.MetaData
5877
return &ZookeeperMetadataReport{
5978
client: client,
6079
rootDir: "/dubbo",
80+
zkAddr: cleanAddrs[0], // Store the cleaned address
6181
}
6282
}
6383

64-
// GetChildren get children node
84+
// GetChildren gets children nodes under a path
6585
func (z *ZookeeperMetadataReport) GetChildren(path string) ([]string, error) {
6686
delimiter := "/"
6787
if path == "" {
68-
delimiter = path
88+
delimiter = ""
6989
}
7090
return z.client.GetChildren(z.rootDir + delimiter + path)
7191
}
7292

73-
// ShowRegistryCenterChildren show children list
93+
// ShowRegistryCenterChildren shows children list from the registry
7494
func (z *ZookeeperMetadataReport) ShowRegistryCenterChildren() (map[string][]string, error) {
7595
methodsMap := map[string][]string{}
7696
inters, err := z.GetChildren("")
7797
if err != nil {
78-
return nil, err
98+
return nil, fmt.Errorf("failed to get root children: %v", err)
7999
}
80100
for _, inter := range inters {
81101
if _, ok := methodsMap[inter]; !ok {
@@ -84,19 +104,39 @@ func (z *ZookeeperMetadataReport) ShowRegistryCenterChildren() (map[string][]str
84104

85105
interChildren, err := z.GetChildren(inter)
86106
if err != nil {
87-
return nil, err
107+
log.Printf("Failed to get children for %s: %v", inter, err)
108+
continue
88109
}
89110
for _, interChild := range interChildren {
90111
interURLs, err := z.GetChildren(inter + "/" + interChild)
91112
if err != nil {
92-
log.Println(err)
113+
log.Printf("Failed to get URLs for %s/%s: %v", inter, interChild, err)
114+
continue
93115
}
94116
for _, interURL := range interURLs {
95-
url, err := common.NewURL(interURL)
117+
// Fetch the content of the node instead of using the node name
118+
fullPath := z.rootDir + "/" + inter + "/" + interChild + "/" + interURL
119+
content, _, err := z.client.GetContent(fullPath)
96120
if err != nil {
97-
return nil, err
121+
log.Printf("Failed to get content for %s: %v", fullPath, err)
122+
continue
123+
}
124+
log.Printf("Processing interURL content: %s", string(content))
125+
126+
urlStr := string(content)
127+
if !strings.Contains(urlStr, "://") {
128+
urlStr = fmt.Sprintf("zookeeper://%s/%s", z.zkAddr, strings.TrimLeft(urlStr, "/"))
129+
}
130+
131+
url, err := common.NewURL(urlStr)
132+
if err != nil {
133+
log.Printf("Failed to parse URL %s: %v", urlStr, err)
134+
continue
135+
}
136+
methods := url.GetParam("methods", "")
137+
if methods != "" {
138+
methodsMap[inter] = append(methodsMap[inter], strings.Split(methods, ",")...)
98139
}
99-
methodsMap[inter] = append(methodsMap[inter], url.GetParam("methods", ""))
100140
}
101141
}
102142
}
@@ -107,11 +147,12 @@ func (z *ZookeeperMetadataReport) ShowRegistryCenterChildren() (map[string][]str
107147
return methodsMap, nil
108148
}
109149

150+
// ShowMetadataCenterChildren shows children list from the metadata center
110151
func (z *ZookeeperMetadataReport) ShowMetadataCenterChildren() (map[string][]string, error) {
111152
methodsMap := map[string][]string{}
112153
inters, err := z.GetChildren("metadata")
113154
if err != nil {
114-
return nil, err
155+
return nil, fmt.Errorf("failed to get metadata children: %v", err)
115156
}
116157
for _, inter := range inters {
117158
path := "metadata/" + inter
@@ -128,9 +169,11 @@ func (z *ZookeeperMetadataReport) ShowMetadataCenterChildren() (map[string][]str
128169
return methodsMap, nil
129170
}
130171

172+
// searchMetadataProvider recursively searches for provider metadata
131173
func (z *ZookeeperMetadataReport) searchMetadataProvider(path string, methods *[]string) {
132174
interChildren, err := z.GetChildren(path)
133175
if err != nil {
176+
log.Printf("Failed to get children for %s: %v", path, err)
134177
return
135178
}
136179
for _, interChild := range interChildren {

0 commit comments

Comments
 (0)