nacos连接go-sdk

需要配置服务,创建的服务端口要暴露出 8848,9849,9849.才行。

1 需要创建 configmap

1
2
3
4
5
6
7
8
9
apiVersion: v1
kind: ConfigMap
metadata:
  name: tcp-services
  namespace: nacos
data:
  8848: "nacos/nacos-hs:8848"
  9848: "nacos/nacos-hs:9848"
  9849: "nacos/nacos-hs:9849"

创建 configmap

1
kubectl apply -f configmap.yaml

第二步修改暴露服务

1
2
3
4
k edit deployment default-gateway  -n kubegems-gateway
##在args中添加如下一行数据
        - --tcp-services-configmap=nacos/tcp-services
## 修改后退出即可

第三步修改暴露的服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
k edit svc default-gateway -n kubegems-gateway
###添加如下内容
 - name: nacos-headless
    nodePort: 32698
    port: 8848
    protocol: TCP
    targetPort: 8848
  - name: nacos-rpc
    nodePort: 33698
    port: 9848
    protocol: TCP
    targetPort: 9848
  - name: nacos-grpc
    nodePort: 33699
    port: 9849
    protocol: TCP
    targetPort: 9849

退出保存即可。 Img

在 nacos 的对应的代码如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main

import (
	"fmt"
	"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"
	"time"
)

func main() {
	//sc := []constant.ServerConfig{
	//
	//	*constant.NewServerConfig("nacos.kubegems.io", 30498, constant.WithContextPath("/nacos")),
	//}
	//
	//cc := constant.ClientConfig{
	//	NamespaceId:         "2cfea28330290809ac7ebc7747fa7a6f3672cba5", //namespace id
	//	TimeoutMs:           5000,
	//	NotLoadCacheAtStart: true,
	//	LogDir:              "/tmp/nacos/log",
	//	CacheDir:            "/tmp/nacos/cache",
	//	LogLevel:            "debug",
	//}
	sc := []constant.ServerConfig{
		*constant.NewServerConfig("nacos.kubegems.io", 32698),
	}

	//create ClientConfig
	cc := *constant.NewClientConfig(
		constant.WithNamespaceId("20a02f696f9440e9e8cf0461574921bbeff04c18"),
		constant.WithTimeoutMs(5000),
		constant.WithNotLoadCacheAtStart(true),
		constant.WithLogDir("/tmp/nacos/log"),
		constant.WithCacheDir("/tmp/nacos/cache"),
		constant.WithLogLevel("debug"),
	)

	// create config client
	client, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &cc,
			ServerConfigs: sc,
		},
	)
	if err != nil {
		panic(err)
	}
	_, err = client.PublishConfig(vo.ConfigParam{
		DataId:  "test-data",
		Group:   "test",
		Content: "hello world!",
	})
	_, err = client.PublishConfig(vo.ConfigParam{
		DataId:  "dataId",
		Group:   "test",
		Content: "hello world!222222"})

	if err != nil {
		fmt.Printf("PublishConfig err:%+v \n", err)
	}
	content, err := client.GetConfig(vo.ConfigParam{
		DataId: "hxf",
		Group:  "test",
	})
	fmt.Println("GetConfig,config :" + content)
	if err != nil {
		fmt.Println(err)
	}

	err = client.ListenConfig(vo.ConfigParam{
		DataId: "hxf",
		Group:  "test",
		OnChange: func(namespace, group, dataId, data string) {
			fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
		},
	})
	time.Sleep(300 * time.Second)
}

运行 go 即可

Img

Kubernetes 调整 nodePort 端口范围 vim /etc/kubernetes/manifests/kube-apiserver.yaml 在 command 下添加 –service-node-port-range=1-65535 参数,修改后会自动生效。

Licensed under CC BY-NC-SA 4.0
最后更新于 Jan 06, 2025 05:52 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up