Skip to content

Commit

Permalink
[Fix: Supports namespaceId.]
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzeping.ricco committed Nov 20, 2024
1 parent 86a69b9 commit 0ab0a0b
Show file tree
Hide file tree
Showing 16 changed files with 341 additions and 273 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ jobs:
- name: Check out code
uses: actions/checkout@v1

- name: Install docker-compose
run: |
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
- name: Start Zookeeper Docker Compose
run: docker-compose -f docker-compose-zk.yml up -d

Expand Down
102 changes: 59 additions & 43 deletions clients/http_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/apache/shenyu-client-golang/common/shenyu_error"
"github.com/apache/shenyu-client-golang/model"
"reflect"
"strings"
)

/**
Expand Down Expand Up @@ -57,65 +58,80 @@ func NewShenYuAdminClient(client *model.ShenYuAdminClient) (adminToken model.Adm
* Register metadata to ShenYu Gateway
**/
func RegisterMetaData(adminTokenData model.AdminTokenData, metaData *model.MetaDataRegister) (registerResult bool, err error) {
headers := adapterHeaders(adminTokenData)

params := map[string]string{}
if metaData.AppName == "" || metaData.Path == "" || metaData.Host == "" || metaData.Port == "" {
if metaData.AppName == "" || metaData.Path == "" || metaData.Host == "" || metaData.Port == "" || metaData.NamespaceIds == "" {
return false, shenyu_error.NewShenYuError(constants.MISS_PARAM_ERROR_CODE, constants.MISS_PARAM_ERROR_MSG, err)
}
params["appName"] = metaData.AppName
params["path"] = metaData.Path
params["contextPath"] = metaData.ContextPath
params["host"] = metaData.Host
params["port"] = metaData.Port

if metaData.RPCType != "" {
params["rpcType"] = metaData.RPCType
} else {
params["rpcType"] = constants.RPCTYPE_HTTP
}

if metaData.RuleName != "" {
params["ruleName"] = metaData.RuleName
} else {
params["ruleName"] = metaData.Path
}

tokenRequest := initShenYuCommonRequest(headers, params, constants.REGISTER_METADATA, "")

registerResult, err = http_client.RegisterMetaData(tokenRequest)
if err == nil {
return registerResult, nil
} else {
return false, err
namespaceIds := strings.Split(metaData.NamespaceIds, ";")

for _, namespaceId := range namespaceIds {
if namespaceId == "" {
continue
}
params := map[string]string{}
params["appName"] = metaData.AppName
params["path"] = metaData.Path
params["contextPath"] = metaData.ContextPath
params["host"] = metaData.Host
params["port"] = metaData.Port
params["namespaceId"] = namespaceId

if metaData.RPCType != "" {
params["rpcType"] = metaData.RPCType
} else {
params["rpcType"] = constants.RPCTYPE_HTTP
}

if metaData.RuleName != "" {
params["ruleName"] = metaData.RuleName
} else {
params["ruleName"] = metaData.Path
}
headers := adapterHeaders(adminTokenData)
tokenRequest := initShenYuCommonRequest(headers, params, constants.REGISTER_METADATA, "")

_, err = http_client.RegisterMetaData(tokenRequest)
if err != nil {
return false, err
}
}
return true, nil
}

/**
* Url Register to ShenYu Gateway
**/
func UrlRegister(adminTokenData model.AdminTokenData, urlMetaData *model.URIRegister) (registerResult bool, err error) {
headers := adapterHeaders(adminTokenData)

params := map[string]string{}
if urlMetaData.AppName == "" || urlMetaData.RPCType == "" || urlMetaData.Host == "" || urlMetaData.Port == "" {
if urlMetaData.AppName == "" || urlMetaData.RPCType == "" || urlMetaData.Host == "" || urlMetaData.Port == "" || urlMetaData.NamespaceIds == "" {
return false, shenyu_error.NewShenYuError(constants.MISS_PARAM_ERROR_CODE, constants.MISS_PARAM_ERROR_MSG, err)
}
params["protocol"] = urlMetaData.Protocol
params["appName"] = urlMetaData.AppName
params["contextPath"] = urlMetaData.ContextPath
params["host"] = urlMetaData.Host
params["port"] = urlMetaData.Port
params["rpcType"] = urlMetaData.RPCType

tokenRequest := initShenYuCommonRequest(headers, params, constants.REGISTER_URI, "")

registerResult, err = http_client.DoUrlRegister(tokenRequest)
if err == nil {
return registerResult, nil
} else {
return false, err
namespaceIds := strings.Split(urlMetaData.NamespaceIds, ";")

for _, namespaceId := range namespaceIds {
if namespaceId == "" {
continue
}
params := map[string]string{}
params["protocol"] = urlMetaData.Protocol
params["appName"] = urlMetaData.AppName
params["contextPath"] = urlMetaData.ContextPath
params["host"] = urlMetaData.Host
params["port"] = urlMetaData.Port
params["rpcType"] = urlMetaData.RPCType
params["namespaceId"] = namespaceId

headers := adapterHeaders(adminTokenData)
tokenRequest := initShenYuCommonRequest(headers, params, constants.REGISTER_URI, "")

_, err = http_client.DoUrlRegister(tokenRequest)
if err != nil {
return false, err
}
}
return true, nil
}

/**
Expand Down
2 changes: 1 addition & 1 deletion clients/nacos_client/nacos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type ShenYuNacosClient struct {
type NacosClientParam struct {
IpAddr string //the nacos server address require user provide
Port uint64 //the nacos server port require user provide
NamespaceId string // the namespaceId of Nacos.When namespace is public, fill in the blank string here require user provide.
NamespaceId string //the namespaceId of Nacos require user provide.
}

/**
Expand Down
6 changes: 6 additions & 0 deletions clients/zk_client/zk_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func (zc *ShenYuZkClient) DeregisterServiceInstance(metaData interface{}) (deReg
if err != nil {
return false, err
}
if len(childs) == 0 {
err := zc.ZkClient.Delete(path, stat.Version)
if err != nil {
return false, err
}
}
for _, child := range childs {
fullPath := path + "/" + child
err := zc.ZkClient.Delete(fullPath, stat.Version)
Expand Down
16 changes: 12 additions & 4 deletions docker-compose-consul.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ version: "3.1"

services:
consul1:
image: consul:latest
image: consul:1.15.4
container_name: consul1
restart: always
networks:
Expand All @@ -28,35 +28,43 @@ services:
- ./data/consul/consul1/config:/consul/config

consul2:
image: consul:latest
image: consul:1.15.4
container_name: consul2
restart: always
networks:
- shenyu
depends_on:
- consul1
command: agent -server -client=0.0.0.0 -bind 0.0.0.0 -retry-join=consul1 -node=consul2
volumes:
- ./data/consul/consul2/data:/consul/data
- ./data/consul/consul2/config:/consul/config

consul3:
image: consul:latest
image: consul:1.15.4
container_name: consul3
restart: always
networks:
- shenyu
depends_on:
- consul1
command: agent -server -client=0.0.0.0 -bind 0.0.0.0 -retry-join=consul1 -node=consul3
volumes:
- ./data/consul/consul3/data:/consul/data
- ./data/consul/consul3/config:/consul/config

consul4:
image: consul:latest
image: consul:1.15.4
container_name: consul4
restart: always
ports:
- 8500:8500
networks:
- shenyu
depends_on:
- consul1
- consul2
- consul3
command: agent -client=0.0.0.0 -bind 0.0.0.0 -retry-join=consul1 -ui -node=client1
volumes:
- ./data/consul/consul4/data:/consul/data
Expand Down
39 changes: 21 additions & 18 deletions example/consul_client/consul_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,39 @@ func main() {
metaData1 := &model.ConsulMetaDataRegister{
ServiceId: uuid1,
ShenYuMetaData: &model.MetaDataRegister{
AppName: "testMetaDataRegister1", //require user provide
Path: "/your/path1", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8080", //require user provide
RPCType: "http", //require user provide
AppName: "testMetaDataRegister1", //require user provide
Path: "/your/path1", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8080", //require user provide
RPCType: "http", //require user provide
NamespaceIds: "649330b6-c2d7-4edc-be8e-8a54df9eb385", //require user provide
},
}

metaData2 := &model.ConsulMetaDataRegister{
ServiceId: uuid2,
ShenYuMetaData: &model.MetaDataRegister{
AppName: "testMetaDataRegister2", //require user provide
Path: "/your/path2", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8181", //require user provide
RPCType: "http", //require user provide
AppName: "testMetaDataRegister2", //require user provide
Path: "/your/path2", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8181", //require user provide
RPCType: "http", //require user provide
NamespaceIds: "649330b6-c2d7-4edc-be8e-8a54df9eb385", //require user provide
},
}

metaData3 := &model.ConsulMetaDataRegister{
ServiceId: uuid3,
ShenYuMetaData: &model.MetaDataRegister{
AppName: "testMetaDataRegister3", //require user provide
Path: "/your/path3", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8282", //require user provide
RPCType: "http", //require user provide
AppName: "testMetaDataRegister3", //require user provide
Path: "/your/path3", //require user provide
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8282", //require user provide
RPCType: "http", //require user provide
NamespaceIds: "649330b6-c2d7-4edc-be8e-8a54df9eb385", //require user provide
},
}

Expand Down
Loading

0 comments on commit 0ab0a0b

Please sign in to comment.