nacos

package module
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 15 Imported by: 4

README

GoFrame Nacos Registry

Use nacos as service registration and discovery management.

Installation

go get -u -v github.com/gogf/gf/contrib/registry/nacos/v2

suggested using go.mod:

require github.com/gogf/gf/contrib/registry/nacos/v2 latest

Example

Reference example

server

package main

import (
	"github.com/gogf/gf/contrib/registry/nacos/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/net/gsvc"
)

func main() {
	gsvc.SetRegistry(nacos.New(`127.0.0.1:8848`).
        SetClusterName("DEFAULT").
        SetGroupName("DEFAULT_GROUP"))

	s := g.Server(`hello.svc`)
	s.BindHandler("/", func(r *ghttp.Request) {
		g.Log().Info(r.Context(), `request received`)
		r.Response.Write(`Hello world`)
	})
	s.Run()
}

client

package main

import (
	"fmt"
	"time"

	"github.com/gogf/gf/contrib/registry/nacos/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/gsel"
	"github.com/gogf/gf/v2/net/gsvc"
	"github.com/gogf/gf/v2/os/gctx"
)

func main() {
	gsvc.SetRegistry(nacos.New(`127.0.0.1:8848`))
	gsel.SetBuilder(gsel.NewBuilderRoundRobin())

	client := g.Client()
	for i := 0; i < 100; i++ {
		res, err := client.Get(gctx.New(), `http://hello.svc/`)
		if err != nil {
			panic(err)
		}
		fmt.Println(res.ReadAllString())
		res.Close()
		time.Sleep(time.Second)
	}
}

License

GoFrame Nacos is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package nacos implements service Registry and Discovery using nacos.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServiceFromInstance

func NewServiceFromInstance(instance []model.Instance) gsvc.Service

NewServiceFromInstance new one service from instance

func NewServicesFromInstances

func NewServicesFromInstances(instances []model.Instance) []gsvc.Service

NewServicesFromInstances new some services from some instances

Types

type Config

type Config struct {
	ServerConfigs []constant.ServerConfig `v:"required"` // See constant.ServerConfig
	ClientConfig  *constant.ClientConfig  `v:"required"` // See constant.ClientConfig
}

Config is the configuration object for nacos client.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is nacos registry.

func New

func New(address string, opts ...constant.ClientOption) (reg *Registry)

New new a registry with address and opts

func NewWithClient

func NewWithClient(client naming_client.INamingClient) *Registry

NewWithClient new the instance with INamingClient

func NewWithConfig

func NewWithConfig(ctx context.Context, config Config) (reg *Registry, err error)

New creates and returns registry with Config.

func (*Registry) Deregister

func (reg *Registry) Deregister(ctx context.Context, service gsvc.Service) (err error)

Deregister off-lines and removes `service` from the Registry.

func (*Registry) Register

func (reg *Registry) Register(ctx context.Context, service gsvc.Service) (registered gsvc.Service, err error)

Register registers `service` to Registry. Note that it returns a new Service if it changes the input Service with custom one.

func (*Registry) Search

func (reg *Registry) Search(ctx context.Context, in gsvc.SearchInput) (result []gsvc.Service, err error)

Search searches and returns services with specified condition.

func (*Registry) SetClusterName

func (reg *Registry) SetClusterName(clusterName string) *Registry

SetClusterName can set the clusterName. The default is 'DEFAULT'

func (*Registry) SetGroupName

func (reg *Registry) SetGroupName(groupName string) *Registry

SetGroupName can set the groupName. The default is 'DEFAULT_GROUP'

func (*Registry) Watch

func (reg *Registry) Watch(ctx context.Context, key string) (watcher gsvc.Watcher, err error)

Watch watches specified condition changes. The `key` is the prefix of service key.

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher used to mange service event such as update.

func (*Watcher) Close

func (w *Watcher) Close() (err error)

Close closes the watcher.

func (*Watcher) Proceed

func (w *Watcher) Proceed() (services []gsvc.Service, err error)

Proceed proceeds watch in blocking way. It returns all complete services that watched by `key` if any change.

func (*Watcher) Push

func (w *Watcher) Push(services []model.Instance, err error)

Push add the services watchevent to event queue

func (*Watcher) SetCloseFunc

func (w *Watcher) SetCloseFunc(close func() error)

SetCloseFunc set the close callback function

Jump to

Keyboard shortcuts

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