Skip to content

Commit

Permalink
[Fix: http client supports namespaceId.] (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzeping.ricco committed Nov 19, 2024
1 parent 86a69b9 commit 33281a5
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 15 deletions.
12 changes: 12 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 All @@ -60,6 +65,13 @@ jobs:
- name: Start Etcd Docker Compose
run: docker-compose -f docker-compose-etcd.yml up -d

- name: Check Consul Logs
run: |
docker-compose -f docker-compose-consul.yml logs consul1
docker-compose -f docker-compose-consul.yml logs consul2
docker-compose -f docker-compose-consul.yml logs consul3
docker-compose -f docker-compose-consul.yml logs consul4
- name: Run Unit tests.
run: |
go test `go list ./... | grep example` -coverprofile=coverage.txt -covermode=atomic
Expand Down
2 changes: 2 additions & 0 deletions clients/http_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func RegisterMetaData(adminTokenData model.AdminTokenData, metaData *model.MetaD
params["contextPath"] = metaData.ContextPath
params["host"] = metaData.Host
params["port"] = metaData.Port
params["namespaceId"] = metaData.NamespaceId

if metaData.RPCType != "" {
params["rpcType"] = metaData.RPCType
Expand Down Expand Up @@ -107,6 +108,7 @@ func UrlRegister(adminTokenData model.AdminTokenData, urlMetaData *model.URIRegi
params["host"] = urlMetaData.Host
params["port"] = urlMetaData.Port
params["rpcType"] = urlMetaData.RPCType
params["namespaceId"] = urlMetaData.NamespaceId

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

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
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.13.9
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.13.9
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.13.9
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.13.9
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
12 changes: 7 additions & 5 deletions example/http_client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {

adminToken, err := clients.NewShenYuAdminClient(adminClient)
if err == nil {
fmt.Printf("this is ShenYu Admin client token %v ->", adminToken.AdminTokenData.Token)
fmt.Printf("this is ShenYu Admin client token: %v\n", adminToken.AdminTokenData.Token)
}

//init MetaDataRegister
Expand All @@ -48,12 +48,13 @@ func main() {
Enabled: true, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8080", //require user provide
NamespaceId: "public", //require user provide
}
result, err := clients.RegisterMetaData(adminToken.AdminTokenData, metaData)
if err != nil {
fmt.Printf("MetaDataRegister has error %v:", err)
fmt.Printf("MetaDataRegister has error: %v\n", err)
}
fmt.Printf("finish register metadata ,the result is %v ->", result)
fmt.Printf("finish register metadata ,the result is: %v\n", result)

//init urlRegister
urlRegister := &model.URIRegister{
Expand All @@ -63,12 +64,13 @@ func main() {
RPCType: constants.RPCTYPE_HTTP, //require user provide
Host: "127.0.0.1", //require user provide
Port: "8080", //require user provide
NamespaceId: "public", //require user provide
}
result, err = clients.UrlRegister(adminToken.AdminTokenData, urlRegister)
if err != nil {
fmt.Printf("UrlRegister has error %v:", err)
fmt.Printf("UrlRegister has error: %v\n", err)
}
fmt.Printf("finish UrlRegister ,the result is %v ->", result)
fmt.Printf("finish UrlRegister ,the result is: %v\n", result)

//do you logic
}
2 changes: 1 addition & 1 deletion example/nacos_client/nacos_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {
ncp := &nacos_client.NacosClientParam{
IpAddr: "console.nacos.io",
Port: 80,
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
NamespaceId: "public",
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
Expand Down
8 changes: 4 additions & 4 deletions example/nacos_client/nacos_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestInitNacosClient(t *testing.T) {
ncp := &nacos_client.NacosClientParam{
IpAddr: "console.nacos.io",
Port: 80,
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
NamespaceId: "public",
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
Expand All @@ -57,7 +57,7 @@ func TestInitNacosClientAndRegister(t *testing.T) {
ncp := &nacos_client.NacosClientParam{
IpAddr: "console.nacos.io",
Port: 80,
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
NamespaceId: "public",
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestRegisterAndGetInstance(t *testing.T) {
ncp := &nacos_client.NacosClientParam{
IpAddr: "console.nacos.io",
Port: 80,
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
NamespaceId: "public",
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestRegisterAndDeregister(t *testing.T) {
ncp := &nacos_client.NacosClientParam{
IpAddr: "console.nacos.io",
Port: 80,
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
NamespaceId: "public",
}

sdkClient := shenyu_sdk_client.GetFactoryClient(constants.NACOS_CLIENT)
Expand Down
14 changes: 14 additions & 0 deletions example/zk_client/zk_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ func TestRegisterServiceInstanceAndGetServiceInstanceInfo(t *testing.T) {
instanceDetail3, err := zc.GetServiceInstanceInfo(metaData3)
assert.NotNil(t, instanceDetail3)
assert.Nil(t, err)

time.Sleep(time.Second)

deRegisterResult1, err := zc.DeregisterServiceInstance(metaData1)
assert.Nil(t, err)
assert.True(t, deRegisterResult1)

deRegisterResult2, err := zc.DeregisterServiceInstance(metaData2)
assert.Nil(t, err)
assert.True(t, deRegisterResult2)

deRegisterResult3, err := zc.DeregisterServiceInstance(metaData3)
assert.Nil(t, err)
assert.True(t, deRegisterResult3)
}

/**
Expand Down
1 change: 1 addition & 0 deletions model/common_meta_data_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ type MetaDataRegister struct {
PluginNames []interface{} `json:"pluginNames"`
RegisterMetaData bool `json:"registerMetaData"`
TimeMillis int64 `json:"timeMillis"`
NamespaceId string `json:"namespaceId"`
}
1 change: 1 addition & 0 deletions model/http_url_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ type URIRegister struct {
RPCType string `json:"rpcType"`
Host string `json:"host"`
Port string `json:"port"`
NamespaceId string `json:"namespaceId"`
}

0 comments on commit 33281a5

Please sign in to comment.