Nacos middleware go sdk use hole filling guide

Author: wencoo
Blog:https://wencoo.blog.csdn.net/
Date: 10/05/2023
Details:

text

nacos is a management configuration service. It is ready to access, use go sdk, and obtain configuration, but it cannot be obtained correctly. It means that the nacos server is on the volcano server, and the management interface is accessed through the domain name. Unable to access the link, in order to troubleshoot the problem, a nacos service was built locally (linux environment), and then the configuration can be obtained correctly through the go sdk

Note 1: No namespace is specified, the default is public, the public NamespaceId field is empty, no need to fill in

Then change to a domain name to access, but the configuration cannot be obtained. Test: add a domain name to the local service, access through the domain name, you can access it correctly, use openapi to access it, and you can also access it correctly (service built by yourself), but using go sdk does not work (cloud service)

 curl -X GET "http://180.184.32.133:8848/nacos/v1/cs/configs?tenant=79de88b6-348a-465a-81f0-632f65bcc72c&dataId=nacosConfigMq.json&group=DEFAULT_GROUP"

Note 2: To set the domain name for the local service, you need to configure it in nginx, and add domain name mapping in hosts to resolve the domain name

After adding the domain name mapping, using openapi, you can access local services and cloud services. Go sdk, after various tests, searched for information, and found that it was a port problem. After adding the port to the security group of the cloud service, go sdk can also get configuration information

Note 3: add 8848, 9848, 9849 three ports, go sdk can get the configuration information

Note 4: The program is in docker, and the third point 3 ports need to be mapped out when deploying

go sdk get configuration information sample code

nacosApi.go

package nacos_go

import (
	"fmt"
	// "time"
	"io/ioutil"
	"net/http"
	// "net/url"
	// "strconv"
	// "strings"


	"github.com/nacos-group/nacos-sdk-go/v2/clients"
	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
	"github.com/nacos-group/nacos-sdk-go/v2/vo"

	// "github.com/nacos-group/nacos-sdk-go/clients"
	// "github.com/nacos-group/nacos-sdk-go/common/constant"
	// "github.com/nacos-group/nacos-sdk-go/vo"

)


func GetMqConfFromNacos(){
	// 至少一个ServerConfig   test.nacos.pick-fun.com.cn   180.184.32.133 
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "test.nacos.pick-fun.com.cn",
			Port:        8848,
			Scheme:		 "http",
			ContextPath: "/nacos",
		},
	}

	// 创建clientConfig 79de88b6-348a-465a-81f0-632f65bcc72c
	clientConfig := constant.ClientConfig{
		NamespaceId:         "79de88b6-348a-465a-81f0-632f65bcc72c", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "tmp/nacos/log", //当
		CacheDir:            "tmp/nacos/cache",
		// RotateTime:          "1h",
		// MaxAge:              3,
		LogLevel:            "debug",
	}
	// 创建动态配置客户端的另一种方式 (推荐)
	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)
	if err != nil {
		panic(err)
	}
	//获取配置信息
	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "nacosConfigMq.json",
		Group:  "DEFAULT_GROUP"})
	if err != nil {
		fmt.Println("GetConfig err: ",err)
	}else{
		fmt.Println("GetConfig : ",content)
	}

}

func Keyongyijing(){
	//tenant=79de88b6-348a-465a-81f0-632f65bcc72c&
	// url := "http://192.168.0.223:8848/nacos/v1/cs/configs?dataId=nacosConfigMq.json&group=DEFAULT_GROUP"
	url := "http://180.184.32.133:8848/nacos/v1/cs/configs?tenant=79de88b6-348a-465a-81f0-632f65bcc72c&dataId=nacosConfigMq.json&group=DEFAULT_GROUP"


	req, _ := http.NewRequest("GET", url, nil)

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))
}

main.go

package main

//#cgo LDFLAGS: -Wl,--allow-multiple-definition
import "C"

import (
	"go_plugin/nacos_go"
)

//go build -buildmode=c-archive -o lib/libgoplugin.a
func main() {
	nacos_go.GetMqConfFromNacos()
	// nacos_go.Keyongyijing()
}

reference

reward

Welcome to WeChat for technical exchanges, note "Blog technical exchanges"

insert image description here

If this article is helpful to you, you can mark it a little, your support is the greatest support for originality.

insert image description here

Guess you like

Origin blog.csdn.net/bootleader/article/details/130599511