You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2022/06/27 02:19:35 UTC

[incubator-shenyu-client-golang] branch main updated: [feature: Adjust Nacos Registory Example] (#15)

This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu-client-golang.git


The following commit(s) were added to refs/heads/main by this push:
     new 3ec6d48  [feature: Adjust Nacos Registory Example] (#15)
3ec6d48 is described below

commit 3ec6d48cf7fd24d0add0dcfdbcd45a2f6b495e57
Author: Lisandro <li...@163.com>
AuthorDate: Mon Jun 27 10:19:30 2022 +0800

    [feature: Adjust Nacos Registory Example] (#15)
    
    * [feat: Add Nacos Registory.]
    
    * [feat: Adjust mod.]
    
    * [feat: Add Register Example.]
    
    * [feat: Add Nacos Registory.]
    
    * [feat: Adjust mod.]
    
    * [feat: Add Register Example.]
    
    * [feat: Adjust Nacos Example.]
    
    Co-authored-by: lishuo <li...@mesomia-tech.com>
---
 clients/nacos_client/nacos_client.go |  9 ++++-----
 example/nacos_client/main.go         | 34 +++++++++++++++++++++++-----------
 go.mod                               |  2 +-
 3 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/clients/nacos_client/nacos_client.go b/clients/nacos_client/nacos_client.go
index 50f0beb..5ff9db7 100644
--- a/clients/nacos_client/nacos_client.go
+++ b/clients/nacos_client/nacos_client.go
@@ -18,7 +18,6 @@
 package nacos_client
 
 import (
-	"github.com/apache/incubator-shenyu-client-golang/model"
 	"github.com/nacos-group/nacos-sdk-go/clients"
 	"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
 	"github.com/nacos-group/nacos-sdk-go/common/constant"
@@ -76,7 +75,7 @@ func (ncp *NacosClientParam) initNacosClient() (clientProxy naming_client.INamin
 		},
 	)
 
-	if err != nil {
+	if err == nil {
 		return client, nil
 	}
 	return
@@ -85,11 +84,11 @@ func (ncp *NacosClientParam) initNacosClient() (clientProxy naming_client.INamin
 /**
  * register nacos instance
  **/
-func RegisterNacosInstance(client naming_client.INamingClient, nri model.NacosRegisterInstance) (registerResult bool, err error) {
-	registerResult, err = client.RegisterInstance(nri.RegisterInstance)
+func RegisterNacosInstance(client naming_client.INamingClient, rip vo.RegisterInstanceParam) (registerResult bool, err error) {
+	registerResult, err = client.RegisterInstance(rip)
 	if err != nil {
 		logger.Fatal("RegisterServiceInstance failure! ,error is :%+v", err)
 	}
-	logger.Info("RegisterServiceInstance,param:%+v,result:%+v \n\n", nri.RegisterInstance, registerResult)
+	logger.Info("RegisterServiceInstance,param:%+v,result:%+v \n\n", rip, registerResult)
 	return registerResult, nil
 }
diff --git a/example/nacos_client/main.go b/example/nacos_client/main.go
index c807bbd..2de72ce 100644
--- a/example/nacos_client/main.go
+++ b/example/nacos_client/main.go
@@ -18,7 +18,9 @@
 package main
 
 import (
+	"encoding/json"
 	"github.com/apache/incubator-shenyu-client-golang/clients/nacos_client"
+	"github.com/apache/incubator-shenyu-client-golang/common/constants"
 	"github.com/apache/incubator-shenyu-client-golang/model"
 	"github.com/nacos-group/nacos-sdk-go/vo"
 	"github.com/wonderivan/logger"
@@ -36,18 +38,27 @@ func main() {
 		NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
 	}
 
+	//metaData is necessary param, this will be register to shenyu gateway to use
+	metaData := &model.URIRegister{
+		Protocol:    "testMetaDataRegister", //require user provide
+		AppName:     "testURLRegister",      //require user provide
+		ContextPath: "contextPath",          //require user provide
+		RPCType:     constants.RPCTYPE_HTTP, //require user provide
+		Host:        "127.0.0.1",            //require user provide
+		Port:        "8080",                 //require user provide
+	}
+	metaDataStringJson, _ := json.Marshal(metaData)
+
 	//init NacosRegisterInstance
-	nacosRegisterInstance := model.NacosRegisterInstance{
-		RegisterInstance: vo.RegisterInstanceParam{
-			Ip:          "10.0.0.10",
-			Port:        8848,
-			ServiceName: "demo.go",
-			Weight:      10,
-			Enable:      true,
-			Healthy:     true,
-			Ephemeral:   true,
-			Metadata:    map[string]string{"idc": "beijing"},
-		},
+	nacosRegisterInstance := vo.RegisterInstanceParam{
+		Ip:          "10.0.0.10", //require user provide
+		Port:        8848,        //require user provide
+		ServiceName: "demo.go",   //require user provide
+		Weight:      10,          //require user provide
+		Enable:      true,        //require user provide
+		Healthy:     true,        //require user provide
+		Ephemeral:   true,        //require user provide
+		Metadata:    map[string]string{"contextPath": "contextPath", "uriMetadata": string(metaDataStringJson)},
 	}
 
 	client, err := nacos_client.NewNacosClient(ncp)
@@ -60,4 +71,5 @@ func main() {
 		logger.Fatal("Register nacos Instance error : %+V", err)
 	}
 
+	//do your logic
 }
diff --git a/go.mod b/go.mod
index 979c59b..2e7294b 100644
--- a/go.mod
+++ b/go.mod
@@ -22,4 +22,4 @@ require (
 	golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
 	gopkg.in/ini.v1 v1.42.0 // indirect
 	gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
-)
+)
\ No newline at end of file