Skip to content

danielgatis/go-discovery

Repository files navigation

Go - Discovery

Go Report Card License MIT Go Doc

A collection of service discovery implementations.

Install

go get -u github.com/danielgatis/go-discovery

And then import the package in your code:

import "github.com/danielgatis/go-discovery"

Example

An example described below is one of the use cases.

package main

import (
	"context"
	"flag"
	"fmt"
	"time"

	"github.com/danielgatis/go-ctrlc"
	"github.com/danielgatis/go-discovery"
	"github.com/sirupsen/logrus"
)

var (
	port int
)

func init() {
	flag.IntVar(&port, "port", 3001, "port number")
}

func main() {
	flag.Parse()

	discovery := discovery.NewMdnsDiscovery(fmt.Sprintf("test:%d", port), "_test._tcp", "local.", port, logrus.StandardLogger())
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

	ctrlc.Watch(func() {
		cancel()
	})

	go func() {
		discovery.Register(ctx)
	}()

	for {
		select {
		case <-ctx.Done():
			return
		default:
			peers, err := discovery.Lookup()
			if err != nil {
				logrus.Fatal(err)
			}

			for _, peer := range peers {
				logrus.Info(peer)
			}
		}
	}
}
❯ go run main.go -p 3001
❯ go run main.go -p 3002

License

Copyright (c) 2021-present Daniel Gatis

Licensed under MIT License

Buy me a coffee

Liked some of my work? Buy me a coffee (or more likely a beer)

Buy Me A Coffee

About

A collection of service discovery implementations.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages