Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Support getting metadata from metadataCenter #2066

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion dubbogo-cli/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@ func init() {
rootCmd.AddCommand(showCmd)
showCmd.Flags().String("r", "r", "")
showCmd.Flags().String("h", "h", "")
showCmd.Flags().Bool("mc", false, "Get Metadata in MetadataCenter or not")
}

func show(cmd *cobra.Command, _ []string) {
var (
methodsMap map[string][]string
err error
)

registry, err := cmd.Flags().GetString("r")
if err != nil {
panic(err)
Expand All @@ -59,7 +65,18 @@ func show(cmd *cobra.Command, _ []string) {
log.Print("registry not support")
return
}
methodsMap, err := fact("dubbogo-cli", []string{host}).ShowChildren()

metadataCenter, err := cmd.Flags().GetBool("mc")
if err != nil {
panic(err)
}

if metadataCenter {
methodsMap, err = fact("dubbogo-cli", []string{host}).ShowMetadataCenterChildren()
} else {
methodsMap, err = fact("dubbogo-cli", []string{host}).ShowChildren()
}

if err != nil {
panic(err)
}
Expand Down
1 change: 1 addition & 0 deletions dubbogo-cli/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
// MetaData meta data from registration center
type MetaData interface {
ShowChildren() (map[string][]string, error)
ShowMetadataCenterChildren() (map[string][]string, error)
}

// Factory metaData factory function
Expand Down
50 changes: 50 additions & 0 deletions dubbogo-cli/metadata/zookeeper/zookeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package zookeeper

import (
"encoding/json"
"fmt"
"log"
"time"
Expand All @@ -30,6 +31,7 @@ import (
import (
"dubbo.apache.org/dubbo-go/v3/common"
"dubbo.apache.org/dubbo-go/v3/dubbogo-cli/metadata"
"dubbo.apache.org/dubbo-go/v3/metadata/definition"
)

func init() {
Expand Down Expand Up @@ -104,3 +106,51 @@ func (z *ZookeeperMetadataReport) ShowChildren() (map[string][]string, error) {
}
return methodsMap, nil
}

func (z *ZookeeperMetadataReport) ShowMetadataCenterChildren() (map[string][]string, error) {
methodsMap := map[string][]string{}
inters, err := z.GetChildren("metadata")
if err != nil {
return nil, err
}
for _, inter := range inters {
path := "metadata/" + inter
var methods []string
z.searchMetadataProvider(path, &methods)

if _, ok := methodsMap[inter]; !ok && len(methods) != 0 {
methodsMap[inter] = make([]string, 0)
}
for _, method := range methods {
methodsMap[inter] = append(methodsMap[inter], method)
}
}
return methodsMap, nil
}

func (z *ZookeeperMetadataReport) searchMetadataProvider(path string, methods *[]string) {
interChildren, err := z.GetChildren(path)
if err != nil {
return
}
for _, interChild := range interChildren {
if interChild == "provider" {
content , _, err := z.client.GetContent("/" + "dubbo" + "/" + path + "/" + interChild)
if err != nil {
fmt.Printf("Zookeeper Get Content Error: %v\n", err)
return
}

var serviceDefinition definition.FullServiceDefinition
err = json.Unmarshal(content, &serviceDefinition)
if err != nil {
fmt.Printf("Json Unmarshal fail: %v\n", err)
}
for _, method := range serviceDefinition.Methods {
*methods = append(*methods, method.Name)
}
} else {
z.searchMetadataProvider(path + "/" + interChild, methods)
}
}
}