ZookeeperServiceDiscovery

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

grpc-ecs-zk-service-discovery

gRPC service discovery for Go using Apace ZooKeeper

Usage
package main

import (
	"fmt"
	"github.com/gergof/grpc-ecs-zk-service-discovery"
	"google.golang.org/grpc"
)

func main() {
	// Create zookeeper service discovery instance
	// You should pass all the zookeeper servers, so if one goes down, the service
	// will reconnect to another
	// The timeout should be a small value, 3-5 seconds should be plenty
	// You can choose any path you want. Remember, this path will only be used
	// to **register** the services to
	zkSd, err := ZookeeperServiceDiscovery.NewEcsZkServiceDiscovery(
		[]string{
			"zk-1.zookeeper.example.com",
			"zk-2.zookeeper.example.com",
			"zk-3.zookeeper.example.com",
		},
		5,
		"/com.example.some-service/service-discovery",
	)

	if err != nil {
		fmt.Errorf("Could not initialize service discovery: %w", err)
		return
	}

	// Register the gRPC resolver
	// You need to call this before calling grpc.Dial()
	zkSd.RegisterResolver()

	// Register a service with a port and an optional host
	err = zkSd.RegisterService("8080")

	// or:
	err = zkSd.RegisterService("8888", "10.0.1.5")

	if err != nil {
		fmt.Errorf("Could not register service: %w", err)
		return
	}

	// You should always clean up before exiting and before stopping the service itself
	// so the service will be unregistered before it stops to answer requests
	// Note: After calling unregister, you **can not** restart the service discovery, you
	// need to create a new instance
	defer zkSd.Unregister()

	// Use the zk:// scheme to dial a gRPC service
	c, _ := grpc.Dial("zk:///com.example.some-service/service-discovery", grpc.WithInsecure())
	defer c.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EcsZkDiscovery

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

func NewEcsZkServiceDiscovery

func NewEcsZkServiceDiscovery(zkServers []string, zkTimeout int, registrationPath string, args ...string) (*EcsZkDiscovery, error)

func (*EcsZkDiscovery) RegisterResolver

func (sd *EcsZkDiscovery) RegisterResolver()

func (*EcsZkDiscovery) RegisterService

func (sd *EcsZkDiscovery) RegisterService(port string, args ...string) error

func (*EcsZkDiscovery) Unregister

func (sd *EcsZkDiscovery) Unregister()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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