forked from apache/dubbo-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#135 from sjcsjc123/dubboctl-zk
feat:add dubboctl zk ls command
- Loading branch information
Showing
6 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/registry/zk" | ||
) | ||
|
||
func addRegistryCmd(rootCmd *cobra.Command) { | ||
addZkRegistryCmd(rootCmd) | ||
} | ||
|
||
func addZkRegistryCmd(rootCmd *cobra.Command) { | ||
zkRegistryCmd := &cobra.Command{ | ||
Use: "zk", | ||
Short: "Commands related to zookeeper registry", | ||
Long: "Commands help user to operate zookeeper registry", | ||
} | ||
addZkLsCmd(zkRegistryCmd) | ||
rootCmd.AddCommand(zkRegistryCmd) | ||
} | ||
|
||
func addZkLsCmd(zkRegistryCmd *cobra.Command) { | ||
zkAddr := "127.0.0.1:2181" | ||
|
||
lsCmd := &cobra.Command{ | ||
Use: "ls", | ||
Short: "List services or instances", | ||
Long: "List all services or list instances of a service\n\n" + | ||
"Usage:\n\n" + | ||
"- List all services\n" + | ||
" dubboctl zk ls\n\n" + | ||
"- List instances of a service\n" + | ||
" dubboctl zk ls [APP_NAME] [SERVICE_NAME]\n\n" + | ||
"Example:\n\n" + | ||
"- List all services\n" + | ||
" dubboctl zk ls\n\n" + | ||
"- List instances of a service\n" + | ||
" dubboctl zk ls dubbo com.apache.dubbo.sample.basic.IGreeter\n", | ||
Args: cobra.MaximumNArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// if no args, list all services | ||
if len(args) == 0 { | ||
registry, err := zk.NewZkRegistry(zkAddr) | ||
if err != nil { | ||
cmd.Println(err) | ||
return | ||
} | ||
services, err := registry.ListServices(nil) | ||
if err != nil { | ||
cmd.Println(err) | ||
return | ||
} | ||
for _, service := range services { | ||
cmd.Println(service) | ||
} | ||
return | ||
} | ||
if len(args) == 2 { | ||
registry, err := zk.NewZkRegistry(zkAddr) | ||
if err != nil { | ||
cmd.Println(err) | ||
return | ||
} | ||
instances, err := registry.ListInstances(nil, args[0], args[1]) | ||
if err != nil { | ||
cmd.Println(err) | ||
return | ||
} | ||
for _, instance := range instances { | ||
cmd.Println(instance) | ||
} | ||
return | ||
} | ||
cmd.Println("Invalid args, you can use `dubboctl zk ls` to list all services or use `dubboctl zk ls [APP_NAME] [SERVICE_NAME]` to list instances of a service") | ||
}, | ||
} | ||
|
||
lsCmd.Flags().StringVarP(&zkAddr, "addr", "a", "127.0.0.1:2181", "zookeeper address, if has multiple address, use comma to separate") | ||
zkRegistryCmd.AddCommand(lsCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package registry | ||
|
||
import "context" | ||
|
||
// Registry is the interface that wraps the basic methods of registry. | ||
type Registry interface { | ||
// ListServices list all services | ||
ListServices(ctx *context.Context) ([]string, error) | ||
// ListInstances list all instances of the service | ||
ListInstances(ctx *context.Context, applicationName string, serviceName string) ([]string, error) | ||
|
||
// TODO add rules | ||
} | ||
|
||
type Service struct { | ||
Ip string | ||
Port int | ||
// TODO add more info? | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package zk | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/dubbogo/go-zookeeper/zk" | ||
|
||
"github.com/apache/dubbo-kubernetes/app/dubboctl/internal/registry" | ||
) | ||
|
||
type zkRegistry struct { | ||
client *zk.Conn | ||
} | ||
|
||
// NewZkRegistry creates a new zookeeper registry | ||
func NewZkRegistry(addr string) (registry.Registry, error) { | ||
addrList := strings.Split(addr, ",") | ||
conn, _, err := zk.Connect(addrList, 5*time.Second, zk.WithLogInfo(false)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &zkRegistry{ | ||
client: conn, | ||
}, nil | ||
} | ||
|
||
func (z *zkRegistry) ListServices(ctx *context.Context) ([]string, error) { | ||
children, _, err := z.client.Children("/") | ||
if err != nil { | ||
return nil, err | ||
} | ||
// if contain provider, then it is a service | ||
var services []string | ||
for _, firstChild := range children { | ||
// first level is application name, like "dubbo" | ||
secondChildren, _, err := z.client.Children("/" + firstChild) | ||
if err != nil { | ||
continue | ||
} | ||
for _, secondChild := range secondChildren { | ||
// second level is service name, like "com.example.user.UserProvider" | ||
thirdChildren, _, err := z.client.Children("/" + firstChild + "/" + secondChild) | ||
if err != nil { | ||
continue | ||
} | ||
// third level is "providers" or "consumers" | ||
for _, thirdChild := range thirdChildren { | ||
if strings.Contains(thirdChild, "providers") { | ||
seviceName := fmt.Sprintf("application: %s, service: %s", firstChild, secondChild) | ||
services = append(services, seviceName) | ||
break | ||
} | ||
} | ||
} | ||
|
||
} | ||
return services, nil | ||
} | ||
|
||
func (z *zkRegistry) ListInstances(ctx *context.Context, applicationName string, serviceName string) ([]string, error) { | ||
// get all instances of the service | ||
children, _, err := z.client.Children("/" + applicationName + "/" + serviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// get all instances | ||
var instances []string | ||
for _, child := range children { | ||
if strings.EqualFold(child, "providers") { | ||
serviceInstance, _, err := z.client.Children("/" + applicationName + "/" + serviceName + "/" + child) | ||
if err != nil { | ||
continue | ||
} | ||
for _, instance := range serviceInstance { | ||
queryUnescape, err := url.QueryUnescape(instance) | ||
if err != nil { | ||
continue | ||
} | ||
u, err := url.Parse(queryUnescape) | ||
if err != nil { | ||
continue | ||
} | ||
instances = append(instances, u.Host) | ||
} | ||
} | ||
} | ||
return instances, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters