discovery

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2024 License: MIT Imports: 11 Imported by: 0

README

discovery

Service discovery, corresponding to the service registry, supports etcd, consul and nacos.

Example of use

    import "gitee.com/yzsunjianguo/sponge/pkg/servicerd/discovery"

    var cliOptions = []grpccli.Option{}
    var endpoint string

	switch grpcClientCfg.RegistryDiscoveryType {
	// discovering services using consul
	case "consul":
		endpoint = "discovery:///" + grpcClientCfg.Name // Connecting to grpc services by service name
		cli, err := consulcli.Init(cfg.Consul.Addr, consulcli.WithWaitTime(time.Second*5))
		if err != nil {
			panic(fmt.Sprintf("consulcli.Init error: %v, addr: %s", err, cfg.Consul.Addr))
		}
		iDiscovery := consul.New(cli)
		cliOptions = append(cliOptions, grpccli.WithDiscovery(iDiscovery))
	// discovering services using etcd
	case "etcd":
		endpoint = "discovery:///" + grpcClientCfg.Name // Connecting to grpc services by service name
		cli, err := etcdcli.Init(cfg.Etcd.Addrs, etcdcli.WithDialTimeout(time.Second*5))
		if err != nil {
			panic(fmt.Sprintf("etcdcli.Init error: %v, addr: %s", err, cfg.Etcd.Addrs))
		}
		iDiscovery := etcd.New(cli)
		cliOptions = append(cliOptions, grpccli.WithDiscovery(iDiscovery))
	// discovering services using nacos
	case "nacos":
		// example: endpoint = "discovery:///serverName.scheme"
		endpoint = "discovery:///" + grpcClientCfg.Name + ".grpc"
		cli, err := nacoscli.NewNamingClient(
			cfg.NacosRd.IPAddr,
			cfg.NacosRd.Port,
			cfg.NacosRd.NamespaceID)
		if err != nil {
			panic(fmt.Sprintf("nacoscli.NewNamingClient error: %v, ipAddr: %s, port: %d",
				err, cfg.NacosRd.IPAddr, cfg.NacosRd.Port))
		}
		iDiscovery := nacos.New(cli)
		cliOptions = append(cliOptions, grpccli.WithDiscovery(iDiscovery))
	}

    serverNameExampleConn, err = grpccli.DialInsecure(context.Background(), endpoint, cliOptions...)
    if err != nil {
        panic(fmt.Sprintf("dial rpc server failed: %v, endpoint: %s", err, endpoint))
    }

Documentation

Overview

Package discovery is service discovery library, supports etcd, consul and nacos.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsSecure

func IsSecure(u *url.URL) bool

IsSecure parses isSecure for Endpoint URL.

func NewBuilder

func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder

NewBuilder creates a builder which is used to factory registry resolvers.

Types

type Option

type Option func(o *builder)

Option is builder option.

func DisableDebugLog

func DisableDebugLog() Option

DisableDebugLog disables update instances log.

func WithInsecure

func WithInsecure(insecure bool) Option

WithInsecure with isSecure option.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout with timeout option.

Jump to

Keyboard shortcuts

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