@@ -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.
4343type 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
4950func 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
6585func (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
7494func (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
110151func (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
131173func (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