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 all commits
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
20 changes: 20 additions & 0 deletions dubbogo-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ dubbogo-cli supports the following capabilities

- View Istio's registration information [ in development ]


- View dubbo-go application metadata center information

- View the metadata information on Zookeeper
```bash
$ dubbogo-cli show --mc zookeeper --h 127.0.0.1:2181
interface: grpc.health.v1.Health
methods: [Watch Check]
interface: grpc.reflection.v1alpha.ServerReflection
methods: [ServerReflectionInfo]
interface: org.apache.dubbo.metadata.MetadataService
methods: [GetServiceDefinition GetServiceDefinitionByServiceKey UnsubscribeURL getMetadataInfo PublishServiceDefinition GetMetadataServiceURL GetSubscribedURLs RefreshMetadata GetExportedServiceURLs getExportedURLs ServiceName SetMetadataServiceURL SubscribeURL UnexportURL Version ExportURL]
interface: com.apache.dubbo.sample.basic.IGreeter
methods: [SayHello SayHelloStream]
```

- View the metadata information on Nacos [ in development ]

- View Istio's metadata information [ in development ]

- Debug Dubbo protocol application

- Debug Triple protocol application
Expand Down
20 changes: 20 additions & 0 deletions dubbogo-cli/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ dubbogo-cli 支持以下能力

- 查看 Istio 的注册信息【功能开发中】

- 查看 dubbo-go 元数据中心信息

- 查看 Zookeeper 上面的元数据信息, 获取接口及方法列表

```bash
$ dubbogo-cli show --mc zookeeper --h 127.0.0.1:2181
interface: grpc.health.v1.Health
methods: [Watch Check]
interface: grpc.reflection.v1alpha.ServerReflection
methods: [ServerReflectionInfo]
interface: org.apache.dubbo.metadata.MetadataService
methods: [GetServiceDefinition GetServiceDefinitionByServiceKey UnsubscribeURL getMetadataInfo PublishServiceDefinition GetMetadataServiceURL GetSubscribedURLs RefreshMetadata GetExportedServiceURLs getExportedURLs ServiceName SetMetadataServiceURL SubscribeURL UnexportURL Version ExportURL]
interface: com.apache.dubbo.sample.basic.IGreeter
methods: [SayHello SayHelloStream]
```

- 查看 Nacos 上面的元数据信息 【功能开发中】

- 查看 Istio 的元数据信息【功能开发中】

- 调试 Dubbo 协议接口

- 调试 Triple 协议接口
Expand Down
60 changes: 49 additions & 11 deletions dubbogo-cli/cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,68 @@ var showCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(showCmd)
showCmd.Flags().String("r", "r", "")
showCmd.Flags().String("r", "", "")
showCmd.Flags().String("mc", "", "Get Metadata in MetadataCenter")
showCmd.Flags().String("h", "h", "")

}

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

registry, err := cmd.Flags().GetString("r")
if err != nil {
panic(err)
}
host, err := cmd.Flags().GetString("h")

metadataCenter, err := cmd.Flags().GetString("mc")
if err != nil {
panic(err)
}
fact, ok := metadata.GetFactory(registry)
if !ok {
log.Print("registry not support")
return
}
methodsMap, err := fact("dubbogo-cli", []string{host}).ShowChildren()

host, err := cmd.Flags().GetString("h")
if err != nil {
panic(err)
}
for k, v := range methodsMap {
fmt.Printf("interface: %s\n", k)
fmt.Printf("methods: %v\n", v)

if registry != "" {
registryFactory, ok := metadata.GetFactory(registry)
if !ok {
log.Print("registry not support")
return
}
methodsMap, err = registryFactory("dubbogo-cli", []string{host}).ShowRegistryCenterChildren()
if err != nil {
panic(err)
}
fmt.Printf("======================\n")
fmt.Printf("Registry:\n")
for k, v := range methodsMap {
fmt.Printf("interface: %s\n", k)
fmt.Printf("methods: %v\n", v)
}
fmt.Printf("======================\n")
}

if metadataCenter != "" {
metadataCenterFactory, ok := metadata.GetFactory(metadataCenter)
if !ok {
log.Print("metadataCenter not support")
return
}
methodsMap, err = metadataCenterFactory("dubbogo-cli", []string{host}).ShowMetadataCenterChildren()
if err != nil {
panic(err)
}
fmt.Printf("======================\n")
fmt.Printf("MetadataCenter:\n")
for k, v := range methodsMap {
fmt.Printf("interface: %s\n", k)
fmt.Printf("methods: %v\n", v)
}
fmt.Printf("======================\n")
}
}
3 changes: 2 additions & 1 deletion dubbogo-cli/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (

// MetaData meta data from registration center
type MetaData interface {
ShowChildren() (map[string][]string, error)
ShowRegistryCenterChildren() (map[string][]string, error)
ShowMetadataCenterChildren() (map[string][]string, error)
}

// Factory metaData factory function
Expand Down
52 changes: 51 additions & 1 deletion 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 @@ -69,7 +71,7 @@ func (z *ZookeeperMetadataReport) GetChildren(path string) ([]string, error) {
}

// ShowChildren shou children list
func (z *ZookeeperMetadataReport) ShowChildren() (map[string][]string, error) {
func (z *ZookeeperMetadataReport) ShowRegistryCenterChildren() (map[string][]string, error) {
methodsMap := map[string][]string{}
inters, err := z.GetChildren("")
if err != nil {
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)
}
}
}