consul

package module
v2.8.3 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MIT Imports: 10 Imported by: 4

README

GoFrame Consul Registry

Use consul as service registration and discovery management.

Installation

go get -u github.com/gogf/gf/contrib/registry/consul/v2

suggested using go.mod:

require github.com/gogf/gf/contrib/registry/consul/v2 latest

Example

HTTP Server
package main

import (
    "context"
    
    "github.com/gogf/gf/contrib/registry/consul/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
    "github.com/gogf/gf/v2/net/gsvc"
)

func main() {
    registry, err := consul.New(consul.WithAddress("127.0.0.1:8500"))
    if err != nil {
        g.Log().Fatal(context.Background(), err)
    }
    gsvc.SetRegistry(registry)

    s := g.Server("hello.svc")
    s.BindHandler("/", func(r *ghttp.Request) {
        g.Log().Info(r.Context(), "request received")
        r.Response.Write("Hello world")
    })
    s.Run()
}
HTTP Client
package main

import (
    "context"
    "fmt"
    "time"

    "github.com/gogf/gf/contrib/registry/consul/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/gsel"
    "github.com/gogf/gf/v2/net/gsvc"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    registry, err := consul.New(consul.WithAddress("127.0.0.1:8500"))
    if err != nil {
        g.Log().Fatal(context.Background(), err)
    }
    gsvc.SetRegistry(registry)
    gsel.SetBuilder(gsel.NewBuilderRoundRobin())

    client := g.Client()
    for i := 0; i < 100; i++ {
        res, err := client.Get(gctx.New(), "http://hello.svc/")
        if err != nil {
            panic(err)
        }
        fmt.Println(res.ReadAllString())
        res.Close()
        time.Sleep(time.Second)
    }
}

Configuration Options

The registry supports the following configuration options:

  • WithAddress(address string): Sets the Consul server address (default: "127.0.0.1:8500")
  • WithToken(token string): Sets the ACL token for Consul authentication

Features

  • Service registration with TTL health check
  • Service discovery with health status filtering
  • Service metadata support
  • Watch support for service changes
  • Consul ACL token support

Requirements

  • Go 1.18 or higher
  • Consul 1.0 or higher

License

GoFrame Consul is licensed under the MIT License, 100% free and open-source, forever.

Documentation

Overview

Package consul implements service Registry and Discovery using consul.

Index

Constants

View Source
const (
	// DefaultTTL is the default TTL for service registration
	DefaultTTL = 20 * time.Second

	// DefaultHealthCheckInterval is the default interval for health check
	DefaultHealthCheckInterval = 10 * time.Second
)

Variables

This section is empty.

Functions

func New

func New(opts ...Option) (gsvc.Registry, error)

New creates and returns a new Registry.

Types

type Option

type Option func(r *Registry)

Option is the configuration option type for registry.

func WithAddress

func WithAddress(address string) Option

WithAddress sets the address for consul client.

func WithToken

func WithToken(token string) Option

WithToken sets the ACL token for consul client.

type Registry

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

Registry implements gsvc.Registry interface using consul.

func (*Registry) Deregister

func (r *Registry) Deregister(ctx context.Context, service gsvc.Service) error

Deregister deregisters a service from consul.

func (*Registry) GetAddress

func (r *Registry) GetAddress() string

GetAddress returns the consul address

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, service gsvc.Service) (gsvc.Service, error)

Register registers a service to consul.

func (*Registry) Search

func (r *Registry) Search(ctx context.Context, in gsvc.SearchInput) ([]gsvc.Service, error)

Search searches and returns services with specified condition.

func (*Registry) Watch

func (r *Registry) Watch(ctx context.Context, key string) (gsvc.Watcher, error)

Watch creates and returns a watcher for specified service.

type Watcher

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

Watcher watches the service changes.

func (*Watcher) Close

func (w *Watcher) Close() error

Close closes the watcher.

func (*Watcher) Proceed

func (w *Watcher) Proceed() ([]gsvc.Service, error)

Proceed returns current services and waits for the next service change.

func (*Watcher) Services

func (w *Watcher) Services() ([]gsvc.Service, error)

Services returns current services from the watcher.

Jump to

Keyboard shortcuts

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