Giter VIP home page Giter VIP logo

nacos-helper-go's Introduction

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)
	}
}

nacos-helper-go's People

Contributors

zhangsq-ax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.