discovery

package module
v0.0.0-...-fb16bd2 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2015 License: Apache-2.0 Imports: 9 Imported by: 0

README

MOVED TO fsgo

Curator Service Discovery (for Go)

Implement Curator-sytle service discovery in Go -- both server-side registration as well as client-side discovery.

Server-side

First, create teh service-discovery framework (passing it a connected curator) and call MaintainRegistrations() to start its connection monitor:

  s := discovery.NewServiceDiscovery(c, "/path/to/services")
  s.MaintainRegistrations()

Then register some services:

  reg := discovery.NewSimpleServiceInstance("baz", "127.0.0.1", 8080)
  ...
  if err := s.Register(reg); err != nil {
    log.Fatal("failed to register: ", err)
  }
  defer s.Unregister(reg)

Client-side

First, create a service-discovery framework (passing it a connected curator) and call Watch() to start watching for changes.

Use Provider(service) to get a provider, which can be used to get a registered instance (via GetInstance()) whenever one is needed.

The default strategy used when creating providers is to randomly select a registed node. Use ProviderWithStrategy(name, strategy) to pass a different ProviderStrategy, such as the RoundRobinProvider.

  s := discovery.NewServiceDiscovery(c, "/path/to/services")
  s.Watch()

  bazProvider := s.Provider("baz")
  ...

  def HandleReq() {
  ...
    hostForThisReq := bazProvider.GetInstance().Spec()
    http.Get(fmt.Sprintf("http://%s/path/to/resource", hostForThisReq))
  ...
  }

Testing

The tests use zk.StartTestCluster to get a testing zookeeper instance. This requires you have zookeeper installed locally (it searches a few relative and system paths). On OSX, brew install zookeeper is enough to get it working.

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewServiceDiscoveryAndConn

func NewServiceDiscoveryAndConn(connString, basePath string) (*ServiceDiscovery, Conn, error)

Types

type Conn

type Conn interface {
	curator.CuratorFramework
}

func DefaultConn

func DefaultConn(conn string) (Conn, error)

type InstanceProvider

type InstanceProvider interface {
	/**
	 * Return the current available set of instances <b>IMPORTANT: </b> users
	 * should not hold on to the instance returned. They should always get a fresh list.
	 */
	GetAllInstances() ([]*ServiceInstance, error)
}

type InstanceSerializer

type InstanceSerializer interface {
	Serialize(i *ServiceInstance) ([]byte, error)
	Deserialize(b []byte) (*ServiceInstance, error)
}

type JsonInstanceSerializer

type JsonInstanceSerializer struct {
}

func (*JsonInstanceSerializer) Deserialize

func (s *JsonInstanceSerializer) Deserialize(b []byte) (*ServiceInstance, error)

func (*JsonInstanceSerializer) Serialize

func (s *JsonInstanceSerializer) Serialize(i *ServiceInstance) ([]byte, error)

type ProviderStrategy

type ProviderStrategy interface {
	// Given a source of instances, return one of them for a single use.
	GetInstance(instanceProvider InstanceProvider) (*ServiceInstance, error)
}

type RandomProvider

type RandomProvider struct {
	*rand.Rand
}

func NewRandomProvider

func NewRandomProvider() *RandomProvider

func (RandomProvider) GetInstance

func (r RandomProvider) GetInstance(ip InstanceProvider) (*ServiceInstance, error)

type RoundRobinProvider

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

func (*RoundRobinProvider) GetInstance

type ServiceDiscovery

type ServiceDiscovery struct {

	// Cache of watched services
	Services map[string][]*ServiceInstance
	// contains filtered or unexported fields
}

func NewServiceDiscovery

func NewServiceDiscovery(client Conn, basePath string) *ServiceDiscovery

func (*ServiceDiscovery) MaintainRegistrations

func (s *ServiceDiscovery) MaintainRegistrations() error

func (*ServiceDiscovery) Provider

func (s *ServiceDiscovery) Provider(name string) ServiceProvider

func (*ServiceDiscovery) ProviderWithStrategy

func (s *ServiceDiscovery) ProviderWithStrategy(name string, strat ProviderStrategy) ServiceProvider

func (*ServiceDiscovery) Register

func (s *ServiceDiscovery) Register(service *ServiceInstance) error

func (*ServiceDiscovery) ReregisterAll

func (s *ServiceDiscovery) ReregisterAll() error

func (*ServiceDiscovery) StateChanged

func (s *ServiceDiscovery) StateChanged(c curator.CuratorFramework, n curator.ConnectionState)

func (*ServiceDiscovery) Unregister

func (s *ServiceDiscovery) Unregister(service *ServiceInstance) error

func (*ServiceDiscovery) UnregisterAll

func (s *ServiceDiscovery) UnregisterAll() error

func (*ServiceDiscovery) Watch

func (s *ServiceDiscovery) Watch() error

type ServiceDiscoveryInstanceProvider

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

func (*ServiceDiscoveryInstanceProvider) GetAllInstances

func (s *ServiceDiscoveryInstanceProvider) GetAllInstances() ([]*ServiceInstance, error)

func (*ServiceDiscoveryInstanceProvider) GetInstance

type ServiceInstance

type ServiceInstance struct {
	Name                string      `json:"name"`
	Id                  string      `json:"id"`
	Address             string      `json:"address"`
	Port                *int        `json:"port"`
	SslPort             *int        `json:"sslPort"`
	Payload             *string     `json:"payload"`
	RegistrationTimeUTC int64       `json:"registrationTimeUTC"`
	ServiceType         ServiceType `json:"serviceType"`
	UriSpec             *string     `json:"uriSpec"`
}

func NewServiceInstance

func NewServiceInstance(name, address string, port, ssl *int, payload *string) *ServiceInstance

func NewSimpleServiceInstance

func NewSimpleServiceInstance(name, address string, port int) *ServiceInstance

func (*ServiceInstance) Spec

func (i *ServiceInstance) Spec() string

type ServiceProvider

type ServiceProvider interface {
	InstanceProvider
	/**
	 * Return an instance for a single use. <b>IMPORTANT: </b> users
	 * should not hold on to the instance returned. They should always get a fresh instance.
	 */
	GetInstance() (*ServiceInstance, error)
}

type ServiceType

type ServiceType string
const (
	DYNAMIC   ServiceType = "DYNAMIC"
	STATIC    ServiceType = "STATIC"
	PERMANENT ServiceType = "PERMANENT"
)

type TreeCache

type TreeCache struct {
	*ServiceDiscovery
	// contains filtered or unexported fields
}

func NewTreeCache

func NewTreeCache(s *ServiceDiscovery) *TreeCache

func (*TreeCache) Start

func (t *TreeCache) Start()

Jump to

Keyboard shortcuts

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