Skip to content

Commit 8324b64

Browse files
author
cachen
committed
feat: get interfaces and methods form remote
1 parent 057d301 commit 8324b64

File tree

10 files changed

+508
-0
lines changed

10 files changed

+508
-0
lines changed

cmd/dubbogo-cli-v2/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# dubbogo-cli-v2
2+
> dubbo-go integration tool
3+
4+
## How to use
5+
1. 安装
6+
```bash
7+
go get -u github.com/dubbogo/tools/cmd/dubbogo-cli-v2
8+
```
9+
## The main function
10+
11+
### Get a list of interfaces and methods
12+
```bash
13+
./dubbogo-cli-v2 show --r zookeeper --h 127.0.0.1:2181
14+
```
15+
The output is as follows
16+
17+
```bash
18+
interface: org.apache.dubbo.game.basketballService
19+
methods: []
20+
interface: com.apache.dubbo.sample.basic.IGreeter
21+
methods: []
22+
interface: com.dubbogo.pixiu.UserService
23+
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
24+
interface: org.apache.dubbo.gate.basketballService
25+
methods: []
26+
interface: org.apache.dubbo.game.basketballService
27+
methods: []
28+
interface: com.apache.dubbo.sample.basic.IGreeter
29+
methods: []
30+
interface: com.dubbogo.pixiu.UserService
31+
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
32+
interface: org.apache.dubbo.gate.basketballService
33+
methods: []
34+
35+
```

cmd/dubbogo-cli-v2/README_CN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# dubbogo-cli-v2
2+
> dubbo-go 集成工具
3+
4+
## 使用方式
5+
1. 安装
6+
```bash
7+
go get -u github.com/dubbogo/tools/cmd/dubbogo-cli-v2
8+
```
9+
## 主要功能
10+
11+
### 获取接口及方法列表
12+
```bash
13+
./dubbogo-cli-v2 show --r zookeeper --h 127.0.0.1:2181
14+
```
15+
输出如下
16+
17+
```bash
18+
interface: org.apache.dubbo.game.basketballService
19+
methods: []
20+
interface: com.apache.dubbo.sample.basic.IGreeter
21+
methods: []
22+
interface: com.dubbogo.pixiu.UserService
23+
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
24+
interface: org.apache.dubbo.gate.basketballService
25+
methods: []
26+
interface: org.apache.dubbo.game.basketballService
27+
methods: []
28+
interface: com.apache.dubbo.sample.basic.IGreeter
29+
methods: []
30+
interface: com.dubbogo.pixiu.UserService
31+
methods: [CreateUser,GetUserByCode,GetUserByName,GetUserByNameAndAge,GetUserTimeout,UpdateUser,UpdateUserByName]
32+
interface: org.apache.dubbo.gate.basketballService
33+
methods: []
34+
35+
```

cmd/dubbogo-cli-v2/cmd/root.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cmd
19+
20+
import (
21+
"fmt"
22+
"os"
23+
)
24+
25+
import (
26+
"github.com/spf13/cobra"
27+
"github.com/spf13/viper"
28+
)
29+
30+
var cfgFile string
31+
32+
// rootCmd represents the base command when called without any subcommands
33+
var rootCmd = &cobra.Command{
34+
Use: "dubbogo-cli-v2",
35+
Short: "",
36+
Long: ``,
37+
Run: func(cmd *cobra.Command, args []string) {
38+
fmt.Println("hello")
39+
},
40+
}
41+
42+
// Execute adds all child commands to the root command and sets flags appropriately.
43+
// This is called by main.main(). It only needs to happen once to the rootCmd.
44+
func Execute() {
45+
cobra.CheckErr(rootCmd.Execute())
46+
}
47+
48+
func init() {
49+
cobra.OnInitialize(initConfig)
50+
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.dubbogo-cli-v2.yaml)")
51+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
52+
}
53+
54+
// initConfig reads in config file and ENV variables if set.
55+
func initConfig() {
56+
if cfgFile != "" {
57+
// Use config file from the flag.
58+
viper.SetConfigFile(cfgFile)
59+
} else {
60+
home, err := os.UserHomeDir()
61+
cobra.CheckErr(err)
62+
63+
viper.AddConfigPath(home)
64+
viper.SetConfigType("yaml")
65+
viper.SetConfigName(".dubbogo-cli-v2")
66+
}
67+
68+
viper.AutomaticEnv() // read in environment variables that match
69+
70+
// If a config file is found, read it in.
71+
if err := viper.ReadInConfig(); err == nil {
72+
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
73+
}
74+
}

cmd/dubbogo-cli-v2/cmd/show.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cmd
19+
20+
import (
21+
"fmt"
22+
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/metadata"
23+
"log"
24+
25+
"github.com/spf13/cobra"
26+
)
27+
28+
import (
29+
_ "github.com/dubbogo/tools/cmd/dubbogo-cli-v2/metadata/zookeeper"
30+
)
31+
32+
// showCmd represents the show command
33+
var showCmd = &cobra.Command{
34+
Use: "show",
35+
Short: "show interfaces and methods",
36+
Long: ``,
37+
Run: show,
38+
}
39+
40+
func init() {
41+
rootCmd.AddCommand(showCmd)
42+
showCmd.Flags().String("r", "r", "")
43+
showCmd.Flags().String("h", "h", "")
44+
}
45+
46+
func show(cmd *cobra.Command, args []string) {
47+
registry, err := cmd.Flags().GetString("r")
48+
if err != nil {
49+
panic(err)
50+
}
51+
host, err := cmd.Flags().GetString("h")
52+
if err != nil {
53+
panic(err)
54+
}
55+
fact, ok := metadata.GetFactory(registry)
56+
if !ok {
57+
log.Print("registry not support")
58+
return
59+
}
60+
methodsMap, err := fact("dubbogo-cli", []string{host}).ShowChildren()
61+
if err != nil {
62+
panic(err)
63+
}
64+
for k, v := range methodsMap {
65+
fmt.Printf("interface: %s\n", k)
66+
fmt.Printf("methods: %v\n", v)
67+
}
68+
}

cmd/dubbogo-cli-v2/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package main
19+
20+
import "github.com/dubbogo/tools/cmd/dubbogo-cli-v2/cmd"
21+
22+
func main() {
23+
cmd.Execute()
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package metadata
19+
20+
import (
21+
"log"
22+
)
23+
24+
type MetaData interface {
25+
ShowChildren() (map[string][]string, error)
26+
}
27+
28+
type Factory func(name string, zkAddrs []string) MetaData
29+
30+
var factMap = make(map[string]Factory)
31+
32+
func Register(name string, fact Factory) {
33+
if _, ok := factMap[name]; ok {
34+
log.Printf("duplicate name: %s", name)
35+
}
36+
factMap[name] = fact
37+
}
38+
39+
func GetFactory(name string) (fact Factory, ok bool) {
40+
fact, ok = factMap[name]
41+
return
42+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package zookeeper
19+
20+
import (
21+
"fmt"
22+
"log"
23+
"time"
24+
)
25+
26+
import (
27+
"dubbo.apache.org/dubbo-go/v3/common"
28+
gxzookeeper "github.com/dubbogo/gost/database/kv/zk"
29+
)
30+
31+
import (
32+
"github.com/dubbogo/tools/cmd/dubbogo-cli-v2/metadata"
33+
)
34+
35+
func init() {
36+
metadata.Register("zookeeper", NewZk)
37+
}
38+
39+
func NewZk(name string, zkAddrs []string) metadata.MetaData {
40+
return NewZookeeperMetadataReport(name, zkAddrs)
41+
}
42+
43+
// ZookeeperMetadataReport is the implementation of
44+
// MetadataReport based on zookeeper.
45+
type ZookeeperMetadataReport struct {
46+
client *gxzookeeper.ZookeeperClient
47+
rootDir string
48+
}
49+
50+
func NewZookeeperMetadataReport(name string, zkAddrs []string) *ZookeeperMetadataReport {
51+
client, err := gxzookeeper.NewZookeeperClient(
52+
name,
53+
zkAddrs,
54+
false,
55+
gxzookeeper.WithZkTimeOut(15*time.Second))
56+
if err != nil {
57+
panic(err)
58+
}
59+
return &ZookeeperMetadataReport{
60+
client: client,
61+
rootDir: "/dubbo",
62+
}
63+
}
64+
65+
func (z *ZookeeperMetadataReport) GetChildren(path string) ([]string, error) {
66+
delimiter := "/"
67+
if path == "" {
68+
delimiter = path
69+
}
70+
return z.client.GetChildren(z.rootDir + delimiter + path)
71+
}
72+
73+
func (z *ZookeeperMetadataReport) ShowChildren() (map[string][]string, error) {
74+
methodsMap := map[string][]string{}
75+
inters, err := z.GetChildren("")
76+
if err != nil {
77+
return nil, err
78+
}
79+
for _, inter := range inters {
80+
if _, ok := methodsMap[inter]; !ok {
81+
methodsMap[inter] = make([]string, 0)
82+
}
83+
84+
interChildren, err := z.GetChildren(inter)
85+
if err != nil {
86+
return nil, err
87+
}
88+
for _, interChild := range interChildren {
89+
interURLs, err := z.GetChildren(inter + "/" + interChild)
90+
if err != nil {
91+
log.Println(err)
92+
}
93+
for _, interURL := range interURLs {
94+
url, err := common.NewURL(interURL)
95+
if err != nil {
96+
return nil, err
97+
}
98+
methodsMap[inter] = append(methodsMap[inter], url.GetParam("methods", ""))
99+
}
100+
}
101+
}
102+
for k, v := range methodsMap {
103+
fmt.Printf("interface: %s\n", k)
104+
fmt.Printf("methods: %v\n", v)
105+
}
106+
return methodsMap, nil
107+
}

0 commit comments

Comments
 (0)