nacos_helper

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 5 Imported by: 2

README

nacos-helper-go

Nacos helper for Go language

Install

$ go get github.com/zhangsq-ax/nacos-helper-go

The environment variables of Nacos

Name Required Description Example
NACOS_HOST true The IP address of host name of the Nacos server "127.0.0.1"
NACOS_PORT true The port that the Nacos server listened "8848"
NACOS_SCHEME false The URL scheme of the Nacos server "https"
NACOS_CONTEXT_PATH false The URL context path of the Nacos server "/nacos"
NACOS_NAMESPACE_ID false The namespace ID of the client in the Nacos server -
NACOS_APP_NAME false The name of the application to which the client belongs -
NACOS_USERNAME false The username that connect to the Nacos server -
NACOS_PASSWORD false The password that connect to the Nacos server -

Usage

Register a service instance
package main

import (
	nacos_helper "github.com/zhangsq-ax/nacos-helper-go"
	"log"
)

func main() {
	...
	// use environment variables
	err := nacos_helper.RegisterServiceInstance(nil, &nacos_helper.options.RegisterServiceOptions{
		Ip:"127.0.0.1",
		Port: 8080,
		ServiceName: "foo",
	})
	if err != nil {
		log.Fatalln(err)
	}
	...
}
Select an available service instance
package main

import (
	nacos_helper "github.com/zhangsq-ax/nacos-helper-go"
	"log"
	"fmt"
)

func main() {
	// use environment variables
	instance, err := nacos_helper.SelectServiceInstance(nil, &nacos_helper.options.SelectServiceInstanceOptions{
	    ServiceName: "foo",
	})
	if err != nil {
		log.Fatalln(err)
	}
	
	fmt.Println(instance.Ip)
	fmt.Println(instance.Port)
	
	...
}
Subscribe service

When the registered services have changes, an available service instance is automatically selected

package main

import (
	nacos_helper "github.com/zhangsq-ax/nacos-helper-go"
	"log"
	"fmt"
)

func main() {
	err := nacos_helper.SubscribeServiceInstance(nil, &nacos_helper.options.SubscribeServiceInstanceOptions{
		ServiceName: "foo",
		SubscribeCallback: func(instance *model.Instance, err error) {
			if err != nil { 
				// Handling error
			} else {
				fmt.Println(instance.Ip, instance.Port)
				// Handle instance changes 
			}
		},
	})
	if err != nil {
		log.Error(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeregisterServiceInstance

func DeregisterServiceInstance(client *naming_client.INamingClient, opts *options.DeregisterServiceOptions) error

DeregisterServiceInstance Deregister a specified service instance

func GetConfigClient added in v1.2.0

func GetConfigClient(opts *options.NacosOptions) (*config_client.IConfigClient, error)

GetConfigClient Get Nacos config client

func GetNamingClient

func GetNamingClient(opts *options.NacosOptions) (*naming_client.INamingClient, error)

GetNamingClient Get Nacos naming client

func RegisterServiceInstance

func RegisterServiceInstance(client *naming_client.INamingClient, opts *options.RegisterServiceOptions) error

RegisterServiceInstance Register a service instance

func SelectServiceInstance

func SelectServiceInstance(client *naming_client.INamingClient, opts *options.SelectServiceInstanceOptions) (*model.Instance, error)

SelectServiceInstance Select an available service instance

func SubscribeServiceInstance added in v1.1.0

func SubscribeServiceInstance(client *naming_client.INamingClient, opts *options.SubscribeServiceInstanceOptions) error

SubscribeServiceInstance Subscribe services change and select an available service instance

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL