孟令鑫 1 vuosi sitten
vanhempi
commit
e2b05dcead

+ 1 - 1
aarch64/plugin-compile/main/main.go

@@ -17,7 +17,7 @@ const serviceName = "plugin-compile"
 func init() {
 	c_log.InitLog(serviceName)
 	c_oss.InitOss(false)
-	c_nacos.InitNacos(true, serviceName, servicePort, map[string]string{
+	c_nacos.InitService(true, serviceName, servicePort, map[string]string{
 		"开发者":  "孟令鑫",
 		"内网IP": "10.14.85.228",
 		"外网IP": "36.110.106.142",

+ 9 - 4
amd64/kubernetes-scheduler/main/main.go

@@ -15,19 +15,24 @@ import (
 	"os"
 )
 
-const servicePort = 12341
-const serviceName = "kubernetes-scheduler"
-const routerPrefix = "/cicv-data-closedloop/kubernetes-scheduler/"
+const (
+	servicePort  = 12341
+	serviceName  = "kubernetes-scheduler"
+	routerPrefix = "/cicv-data-closedloop/kubernetes-scheduler/"
+	configDataId = "kubernetes-scheduler-prod.yaml"
+	configGroup  = "DEFAULT_GROUP"
+)
 
 func init() {
 	c_log.InitLog("kubernetes-scheduler")
 	c_log.InitLog(serviceName)
 	c_oss.InitOss(false)
-	c_nacos.InitNacos(false, serviceName, servicePort, map[string]string{
+	c_nacos.InitService(false, serviceName, servicePort, map[string]string{
 		"开发者":  "孟令鑫",
 		"内网IP": "10.14.85.228",
 		"外网IP": "36.110.106.156",
 	})
+	c_nacos.InitConfig(false, configDataId, configGroup)
 }
 
 func main() {

+ 3 - 1
amd64/kubernetes-scheduler/package/service/push_algorithm_image.go

@@ -1,4 +1,6 @@
 package service
 
 // PushAlgorithmImage 将镜像文件推送到镜像仓库,共集群内所有节点使用
-func PushAlgorithmImage() {}
+func PushAlgorithmImage() {
+
+}

+ 47 - 0
common/config/c_nacos/listen_config.go

@@ -0,0 +1,47 @@
+package c_nacos
+
+import (
+	"cicv-data-closedloop/common/config/c_log"
+	"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"
+	"os"
+)
+
+func InitConfig(private bool, dataId string, group string) {
+	nacosIp := nacosPublicIp
+	if private {
+		nacosIp = nacosPrivateIp
+	}
+
+	serverConfig := []constant.ServerConfig{*constant.NewServerConfig(nacosIp, uint64(nacosPort), constant.WithContextPath("/nacos"))}
+	clientConfig := constant.NewClientConfig(
+		constant.WithNamespaceId(namespaceId),
+		constant.WithNotLoadCacheAtStart(true),
+		constant.WithUsername(username),
+		constant.WithPassword(password),
+	)
+
+	// ------- 配置刷新 -------
+	configClient, err := clients.NewConfigClient(
+		vo.NacosClientParam{
+			ServerConfigs: serverConfig,
+			ClientConfig:  clientConfig,
+		},
+	)
+	if err != nil {
+		c_log.GlobalLogger.Error("程序崩溃,nacos 连接失败:", err)
+		os.Exit(-1)
+	}
+	if err = configClient.ListenConfig(vo.ConfigParam{
+		DataId: dataId,
+		Group:  group,
+		OnChange: func(namespace, group, dataId, data string) {
+			fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
+		},
+	}); err != nil {
+		c_log.GlobalLogger.Error("程序崩溃,配置监听连接失败:", err)
+		os.Exit(-1)
+	}
+}

+ 0 - 147
common/config/c_nacos/nacos_config.go

@@ -1,147 +0,0 @@
-package c_nacos
-
-import (
-	"cicv-data-closedloop/common/config/c_log"
-	"fmt"
-	"github.com/nacos-group/nacos-sdk-go/v2/clients"
-	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
-	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
-	"github.com/nacos-group/nacos-sdk-go/v2/vo"
-	"os"
-)
-
-var (
-	nacosPrivateIp = "10.14.85.228"
-	nacosPublicIp  = "36.110.106.142"
-	nacosPort      = 8848
-	namespaceId    = "45e828a5-1a1d-4c46-a746-d4dfa3ca8f5f"
-	username       = "cicv"
-	password       = "cicv"
-	serviceIp      = "36.110.106.142"
-)
-
-func InitNacos(private bool, serviceName string, servicePort uint64, metadata map[string]string) {
-	//create ServerConfig
-	var sc []constant.ServerConfig
-	if private {
-		sc = []constant.ServerConfig{*constant.NewServerConfig(nacosPrivateIp, uint64(nacosPort), constant.WithContextPath("/nacos"))}
-	} else {
-		sc = []constant.ServerConfig{*constant.NewServerConfig(nacosPublicIp, uint64(nacosPort), constant.WithContextPath("/nacos"))}
-	}
-
-	//create ClientConfig
-	cc := *constant.NewClientConfig(
-		constant.WithNamespaceId(namespaceId),
-		constant.WithNotLoadCacheAtStart(true),
-		constant.WithUsername(username),
-		constant.WithPassword(password),
-	)
-
-	// create naming client
-	client, err := clients.NewNamingClient(
-		vo.NacosClientParam{
-			ClientConfig:  &cc,
-			ServerConfigs: sc,
-		},
-	)
-
-	if err != nil {
-		c_log.GlobalLogger.Error("程序崩溃,nacos 连接失败:", err)
-		os.Exit(-1)
-	}
-
-	//Register
-	registerServiceInstance(client, vo.RegisterInstanceParam{
-		Ip:          serviceIp,
-		Port:        servicePort,
-		ServiceName: serviceName,
-		//GroupName:   "group-a",
-		//ClusterName: "cluster-a",
-		Weight:    10,
-		Enable:    true,
-		Healthy:   true,
-		Ephemeral: true,
-		Metadata:  metadata,
-	})
-
-}
-
-func registerServiceInstance(client naming_client.INamingClient, param vo.RegisterInstanceParam) {
-	success, err := client.RegisterInstance(param)
-	if !success || err != nil {
-		panic("RegisterServiceInstance failed!" + err.Error())
-	}
-	fmt.Printf("RegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
-}
-
-func batchRegisterServiceInstance(client naming_client.INamingClient, param vo.BatchRegisterInstanceParam) {
-	success, err := client.BatchRegisterInstance(param)
-	if !success || err != nil {
-		panic("BatchRegisterServiceInstance failed!" + err.Error())
-	}
-	fmt.Printf("BatchRegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
-}
-
-func deRegisterServiceInstance(client naming_client.INamingClient, param vo.DeregisterInstanceParam) {
-	success, err := client.DeregisterInstance(param)
-	if !success || err != nil {
-		panic("DeRegisterServiceInstance failed!" + err.Error())
-	}
-	fmt.Printf("DeRegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
-}
-
-func updateServiceInstance(client naming_client.INamingClient, param vo.UpdateInstanceParam) {
-	success, err := client.UpdateInstance(param)
-	if !success || err != nil {
-		panic("UpdateInstance failed!" + err.Error())
-	}
-	fmt.Printf("UpdateServiceInstance,param:%+v,result:%+v \n\n", param, success)
-}
-
-func getService(client naming_client.INamingClient, param vo.GetServiceParam) {
-	service, err := client.GetService(param)
-	if err != nil {
-		panic("GetService failed!" + err.Error())
-	}
-	fmt.Printf("GetService,param:%+v, result:%+v \n\n", param, service)
-}
-
-func selectAllInstances(client naming_client.INamingClient, param vo.SelectAllInstancesParam) {
-	instances, err := client.SelectAllInstances(param)
-	if err != nil {
-		panic("SelectAllInstances failed!" + err.Error())
-	}
-	fmt.Printf("SelectAllInstance,param:%+v, result:%+v \n\n", param, instances)
-}
-
-func selectInstances(client naming_client.INamingClient, param vo.SelectInstancesParam) {
-	instances, err := client.SelectInstances(param)
-	if err != nil {
-		panic("SelectInstances failed!" + err.Error())
-	}
-	fmt.Printf("SelectInstances,param:%+v, result:%+v \n\n", param, instances)
-}
-
-func selectOneHealthyInstance(client naming_client.INamingClient, param vo.SelectOneHealthInstanceParam) {
-	instances, err := client.SelectOneHealthyInstance(param)
-	if err != nil {
-		panic("SelectOneHealthyInstance failed!")
-	}
-	fmt.Printf("SelectOneHealthyInstance,param:%+v, result:%+v \n\n", param, instances)
-}
-
-func subscribe(client naming_client.INamingClient, param *vo.SubscribeParam) {
-	client.Subscribe(param)
-}
-
-func unSubscribe(client naming_client.INamingClient, param *vo.SubscribeParam) {
-	client.Unsubscribe(param)
-}
-
-func getAllService(client naming_client.INamingClient, param vo.GetAllServiceInfoParam) {
-	service, err := client.GetAllServicesInfo(param)
-	if err != nil {
-		panic("GetAllService failed!")
-	}
-	fmt.Printf("GetAllService,param:%+v, result:%+v \n\n", param, service)
-}

+ 71 - 0
common/config/c_nacos/register_service.go

@@ -0,0 +1,71 @@
+package c_nacos
+
+import (
+	"cicv-data-closedloop/common/config/c_log"
+	"fmt"
+	"github.com/nacos-group/nacos-sdk-go/v2/clients"
+	"github.com/nacos-group/nacos-sdk-go/v2/clients/naming_client"
+	"github.com/nacos-group/nacos-sdk-go/v2/common/constant"
+	"github.com/nacos-group/nacos-sdk-go/v2/vo"
+	"os"
+)
+
+var (
+	nacosPrivateIp = "10.14.85.228"
+	nacosPublicIp  = "36.110.106.142"
+	nacosPort      = 8848
+	namespaceId    = "45e828a5-1a1d-4c46-a746-d4dfa3ca8f5f"
+	username       = "cicv"
+	password       = "cicv"
+	serviceIp      = "36.110.106.142"
+)
+
+func InitService(private bool, serviceName string, servicePort uint64, metadata map[string]string) {
+	nacosIp := nacosPublicIp
+	if private {
+		nacosIp = nacosPrivateIp
+	}
+
+	serverConfig := []constant.ServerConfig{*constant.NewServerConfig(nacosIp, uint64(nacosPort), constant.WithContextPath("/nacos"))}
+	clientConfig := constant.NewClientConfig(
+		constant.WithNamespaceId(namespaceId),
+		constant.WithNotLoadCacheAtStart(true),
+		constant.WithUsername(username),
+		constant.WithPassword(password),
+	)
+
+	namingClient, err := clients.NewNamingClient(
+		vo.NacosClientParam{
+			ClientConfig:  clientConfig,
+			ServerConfigs: serverConfig,
+		},
+	)
+	if err != nil {
+		c_log.GlobalLogger.Error("程序崩溃,nacos 连接失败:", err)
+		os.Exit(-1)
+	}
+	if registerServiceInstance(namingClient, vo.RegisterInstanceParam{
+		Ip:          serviceIp,
+		Port:        servicePort,
+		ServiceName: serviceName,
+		//GroupName:   "group-a",
+		//ClusterName: "cluster-a",
+		Weight:    10,
+		Enable:    true,
+		Healthy:   true,
+		Ephemeral: true,
+		Metadata:  metadata,
+	}); err != nil {
+		c_log.GlobalLogger.Error("程序崩溃,服务注册连接失败:", err)
+		os.Exit(-1)
+	}
+
+}
+
+func registerServiceInstance(client naming_client.INamingClient, param vo.RegisterInstanceParam) {
+	success, err := client.RegisterInstance(param)
+	if !success || err != nil {
+		panic("RegisterServiceInstance failed!" + err.Error())
+	}
+	fmt.Printf("RegisterServiceInstance,param:%+v,result:%+v \n\n", param, success)
+}

+ 1 - 0
go.sum

@@ -248,6 +248,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
 github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
 github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
 github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
+github.com/nacos-group/nacos-sdk-go/v2 v2.2.3/go.mod h1:UL4U89WYdnyajgKJUMpuT1Rr6iNmbjrxOO40JRgtA00=
 github.com/nacos-group/nacos-sdk-go/v2 v2.2.5 h1:r0wwT7PayEjvEHzWXwr1ROi/JSqzujM4w+1L5ikThzQ=
 github.com/nacos-group/nacos-sdk-go/v2 v2.2.5/go.mod h1:OObBon0prVJVPoIbSZxpEkFiBfL0d1LcBtuAMiNn+8c=
 github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=