Skip to content

Commit

Permalink
CLI: Support getting metadata from metadataCenter (apache#2066)
Browse files Browse the repository at this point in the history
* support get metadata from metadataCenter

* rename function name

* add new description
  • Loading branch information
Leospard authored Oct 31, 2022
1 parent c575061 commit 96a30ac
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 13 deletions.
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)
}
}
}

0 comments on commit 96a30ac

Please sign in to comment.