discovery

package
v1.0.30 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 14 Imported by: 0

README

Discovery Registry

This module implements a registry.Registrar and registry.Discovery interface in kratos based bilibili/discovery.

go.dev reference

Quick Start

Register a service

import (
	"github.com/go-kratos/kratos/contrib/registry/discovery/v2"
)

func main() {
	// initialize a registry
	r := discovery.New(&discovery.Config{
		Nodes:  []string{"0.0.0.0:7171"},
		Env:    "dev",
		Region: "sh1",
		Zone:   "zone1",
		Host:   "hostname",
	})

	// construct srv instance
	// ...
	
	app := kratos.New(
		kratos.Name("helloworld"),
		kratos.Server(
			httpSrv,
			grpcSrv,
		),
		kratos.Metadata(map[string]string{"color": "gray"}),
		// use Registrar
		kratos.Registrar(r),
	)
	
	if err := app.Run(); err != nil {
		log.NewHelper(logger).Fatal(err)
	}	
}

Discover a service

import (
	"github.com/go-kratos/kratos/contrib/registry/discovery/v2"
	"github.com/limes-cloud/kratos/transport/grpc"
)

func main() {
	// initialize a discovery
	r := discovery.New(&discovery.Config{
		Nodes:  []string{"0.0.0.0:7171"},
		Env:    "dev",
		Region: "sh1",
		Zone:   "zone1",
		Host:   "localhost",
	}, nil)

	conn, err := grpc.DialInsecure(
		context.Background(),
		grpc.WithEndpoint("discovery:///appid"),
		// use discovery
		grpc.WithDiscovery(r),
	)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	
	// request and log
}

Config explain

type Config struct {
	Nodes  []string // discovery nodes address
	Region string   // region of the service, sh
	Zone   string   // zone of region, sh001
	Env    string   // env of service, dev, prod and etc
	Host   string   // hostname of service
}

References

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDuplication = errors.New("register failed: instance duplicated: ")
	ErrServerError = errors.New("server error")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Nodes  []string
	Region string
	Zone   string
	Env    string
	Host   string
}

Config Discovery configures.

type Discovery

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

func New

func New(c *Config) *Discovery

New construct a Discovery instance which implements registry.Registrar, registry.Discovery and registry.Watcher.

func (*Discovery) Close

func (d *Discovery) Close() error

Close stop all running process including Discovery and register

func (*Discovery) Deregister

func (d *Discovery) Deregister(_ context.Context, service *registry.ServiceInstance) error

func (*Discovery) GetService

func (d *Discovery) GetService(ctx context.Context, serviceName string) ([]*registry.ServiceInstance, error)

func (*Discovery) Register

func (d *Discovery) Register(ctx context.Context, service *registry.ServiceInstance) (err error)

func (*Discovery) Watch

func (d *Discovery) Watch(ctx context.Context, serviceName string) (registry.Watcher, error)

type Resolve

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

Resolve Discovery resolver.

func (*Resolve) Close

func (r *Resolve) Close() error

Close resolver

func (*Resolve) Watch

func (r *Resolve) Watch() <-chan struct{}

Watch instance.

Jump to

Keyboard shortcuts

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