Categorygithub.com/eddieraa/registry
modulepackage
0.4.10
Repository: https://github.com/eddieraa/registry.git
Documentation: pkg.go.dev

# README

Simple Service registry based on pub/sub pattern

License Apache 2

This first version service registry use nats (https://nats.io/) or reids (https://redis.io/) to implement a service registry You just need to start nats before using this service registry

The project is still under development and is not ready for production.

A java implentation exist

Simple to use:

API is simple to use, simple to understand. No required external installation. No configuration file is required.

  • Connect: registry.Connect(registry.Nats(c)) init the service both on the client/server side and return registry service instance.
  • Register: reg.Register(registry.Service{Name: "httptest", URL: "http://localhost:8083/test"}) register you service on the service side.
  • GetService: reg.GetService("myservice") on the client side
  • Unregister: reg.Unregister("myservice") on the service side
  • Close : reg.Close() on the server side 'Close' function deregisters all registered services. On the client and service side, unsubsribe to subscriptions

Safe

Can be used in concurrent context. Singleton design pattern is used. The first called to registry.Connect(registry.Nats(c), registry.Timeout(100*time.MilliSecond)) create instance of registry service. And then you can get an instance of registry service without argument reg, _ := registry.Connect()

Fast

This "service registry" uses a local cache as well as the pub / sub pattern. The registration of a new service is in real time. When a new service registers, all clients are notified in real time and update their cache.

Light

The project uses very few external dependencies. There is no service to install. Just your favorite pub/sub implementation (NATS)

Multi-tenant

Multi-tenant support is planned. The tool is based on pub / sub, each message can be pre-fixed by a text of your choice.

Exemple

on the server side :

    package main

import (
	"log"
	"net/http"

	"github.com/eddieraa/registry"
	rnats "github.com/eddieraa/registry/nats"
	"github.com/nats-io/nats.go"
)

func main() {
	//connect to nats server
	conn, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		log.Fatal("Could not connect to nats ", err)
	}

	//create registry default instance
	reg, err := rnats.SetDefault(conn)
	if err != nil {
		log.Fatal("Could not create registry instance ", err)
	}

	addr, _ := registry.FindFreeLocalAddress(10000, 10010)
	//register "myservice"
	fnuregister, err := reg.Register(registry.Service{Name: "myservice", Address: addr})
	if err != nil {
		log.Fatal("Could not register service ", err)
	}

	server := http.NewServeMux()
	server.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("hello"))
	})
	if err = http.ListenAndServe(addr, server); err != nil {
		log.Fatal("could not create server ", err)
	}

	//Unregister
	fnuregister()
	reg.Close()
	conn.Close()

}
    

On the client side :

package main

import (
	"log"

	rnats "github.com/eddieraa/registry/nats"
	"github.com/nats-io/nats.go"
)

func main() {
	//connect to nats server
	conn, err := nats.Connect(nats.DefaultURL)
	if err != nil {
		log.Fatal("Could not connect to nats ", err)
	}

	//create registry default instance
	reg, err := rnats.SetDefault(conn)
	if err != nil {
		log.Fatal("Could not create registry instance ", err)
	}

	//lookup for "myservice"
	s, err := reg.GetService("myservice")
	if err != nil {
		log.Fatal("Could not get service")
	}

	//do something with your service
	log.Print("Find service with address ", s.Address)

	reg.Close()
	conn.Close()

}

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AddFilter add filter.
AddObserveFilter adding filter.
Close the registry instance Call SetDefault before.
FindFreeLocalAddress return local free address using range of port (127.0.0.1:23432).
FindFreePort from range of integer.
FreePort request a new free port return -1 if error.
GetDefault return default instance.
No description provided by the author
No description provided by the author
GetService find service with service name Call SetDefault before use.
GetServices return all registered service Call SetDefault before use.
LoadBalanceFilter basic loadbalancer.
LocalFreeAddr return local free address (127.0.0.1:34545).
LocalFreeIPv6Addr return local free address ([::1]:34545).
LocalhostFilter return true if hostname is equals to service host.
LocalhostOFilter accept only service on the same machine.
NewRegistry create a new service registry instance.
Observe subscribe to service Call SetDefault before use.
No description provided by the author
Port extract port from service.
Register register a new service Call SetDefault before use.
SetDefault set the default instance ex pubsub transport.
No description provided by the author
Unregister unregister a service Call SetDefault before use.
WithLoglevel set log level.
WithMainTopic all topic will start with topic.
WithObserverEvent set handler for Observer Event.
WithPubsub initialyse service registry with nats connection.
WithRegisterInterval time between 2 register publish.
WithTimeout define GetService timeout option * GetService look service in his cache, if cache is empty, or if filtered services is empty then a ping is sent.

# Constants

No description provided by the author
EventRegister register event.
EventUnregister unregister event.
No description provided by the author
No description provided by the author

# Variables

DefaultCheckDueInterval time between 2 checks.
DefaultDueDurationFactor service expire when currentTime > lastRegisteredTime + registerInternal * dueDrationFactor.
DefaultMainTopic default message base.
DefaultRegisterInterval time between 2 registration.
DefaultTimeout timeout for GetService.
ErrNoDefaultInstance when intance singleton has not been set.
ErrNoFreePort when no free port is available.
ErrNotFound when no service found.

# Structs

Options all configurable option.
Pong response to ping.
Service service struct.
Timestamps define registered datetime and expiration duration.

# Interfaces

No description provided by the author
Registry Register, Unregister.

# Type aliases

Event represent event (register|unregister|unavailbale).
Filter used for filtering service do not return nil Ex Load Balancing.
FnUnregister call this func for unregister the service.
ObserveFilter used to accept (or not) new registered service.
ObserverEvent event tigered.
Option option func.
No description provided by the author