github.com/jtzjtz/kit@v1.0.2/balance/service.go (about)

     1  package balance
     2  
     3  import (
     4  	"github.com/nacos-group/nacos-sdk-go/vo"
     5  	"log"
     6  )
     7  
     8  //注册服务
     9  func RegisterService(serviceName, groupName, serverIp string, serverPort, weight int, metaData map[string]string, nacosConf NacosConfig) error {
    10  	var err error
    11  
    12  	if cli == nil {
    13  		cli, err = createNacosClient(nacosConf)
    14  		if err != nil {
    15  			return err
    16  		}
    17  	}
    18  	//err = withAlive(serviceName, ip, port, ttl)
    19  
    20  	_, err = cli.RegisterInstance(vo.RegisterInstanceParam{
    21  		Ip:          serverIp,
    22  		Port:        uint64(serverPort),
    23  		ServiceName: serviceName,
    24  		GroupName:   groupName,
    25  		Weight:      float64(weight),
    26  		Enable:      true,
    27  		Healthy:     true,
    28  		Ephemeral:   true,
    29  		Metadata:    metaData,
    30  	})
    31  	if err != nil {
    32  		log.Println(err)
    33  	}
    34  	return err
    35  }
    36  
    37  // UnRegister remove service from nacos
    38  func UnRegisterService(serviceName string, groupName string, serverIp string, serverPort int) {
    39  	if cli != nil {
    40  		cli.DeregisterInstance(vo.DeregisterInstanceParam{
    41  			Ip:          serverIp,
    42  			Port:        uint64(serverPort),
    43  			ServiceName: serviceName,
    44  			GroupName:   groupName,
    45  			Ephemeral:   true, //it must be true
    46  		})
    47  		log.Printf("group:%s service:%s %s:%v  取消注册", groupName, serviceName, serverIp, serverPort)
    48  	}
    49  }