polaris

package module
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2024 License: MIT Imports: 14 Imported by: 6

README

GoFrame Polaris Registry

English | 简体中文

Use PolarisMesh as service registration, discovery management and heartbeat reporting.

Installation

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

suggested using go.mod:

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

Example

Reference example

server

package main

import (
    "github.com/polarismesh/polaris-go/pkg/config"
    
    "github.com/gogf/gf/contrib/registry/polaris/v2"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
    "github.com/gogf/gf/v2/net/gsvc"
)

func main() {
    conf := config.NewDefaultConfiguration([]string{"183.47.111.80:8091"})
    
    // TTL egt 2*time.Second
    gsvc.SetRegistry(polaris.NewWithConfig(conf, polaris.WithTTL(10)))
    
    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()
}

client

package main

import (
    "fmt"
    "time"
    
    "github.com/polarismesh/polaris-go/pkg/config"
    
    "github.com/gogf/gf/contrib/registry/polaris/v2"
    
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/gsvc"
    "github.com/gogf/gf/v2/os/gctx"
)

func main() {
    conf := config.NewDefaultConfiguration([]string{"183.47.111.80:8091"})
    
    gsvc.SetRegistry(polaris.NewWithConfig(conf, polaris.WithTTL(10)))
    
    for i := 0; i < 100; i++ {
        res, err := g.Client().Get(gctx.New(), `http://hello.svc/`)
        if err != nil {
            panic(err)
        }
        fmt.Println(res.ReadAllString())
        res.Close()
        time.Sleep(time.Second)
    }
}

License

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

Documentation

Overview

Package polaris implements service Registry and Discovery using polaris.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(provider polaris.ProviderAPI, consumer polaris.ConsumerAPI, opts ...Option) gsvc.Registry

New create a new registry.

func NewWithConfig

func NewWithConfig(conf config.Configuration, opts ...Option) gsvc.Registry

NewWithConfig new a registry with config.

Types

type Option

type Option func(o *options)

Option The option is a polaris option.

func WithHealthy

func WithHealthy(healthy bool) Option

WithHealthy with the Healthy option.

func WithIsolate

func WithIsolate(isolate bool) Option

WithIsolate with the Isolate option.

func WithLogger added in v2.2.0

func WithLogger(logger glog.ILogger) Option

WithLogger with the Logger option.

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace with the Namespace option.

func WithProtocol

func WithProtocol(protocol string) Option

WithProtocol with the Protocol option.

func WithRetryCount

func WithRetryCount(retryCount int) Option

WithRetryCount with RetryCount option.

func WithServiceToken

func WithServiceToken(serviceToken string) Option

WithServiceToken with ServiceToken option.

func WithTTL

func WithTTL(TTL int) Option

WithTTL with the TTL option.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout the Timeout option.

func WithWeight

func WithWeight(weight int) Option

WithWeight with the Weight option.

type Registry

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

Registry is polaris registry.

func (*Registry) Deregister

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

Deregister the registration.

func (*Registry) Register

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

Register the registration.

func (*Registry) Search

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

Search returns the service instances in memory according to the service name.

func (*Registry) Watch

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

Watch creates a watcher according to the service name.

type Service

type Service struct {
	gsvc.Service        // Common service object.
	ID           string // ID is the unique instance ID as registered, for some registrar server.
}

Service for wrapping gsvc.Server and extends extra attributes for polaris purpose.

func (*Service) GetKey

func (s *Service) GetKey() string

GetKey overwrites the GetKey function of gsvc.Service for replacing separator string.

func (*Service) GetPrefix

func (s *Service) GetPrefix() string

GetPrefix overwrites the GetPrefix function of gsvc.Service for replacing separator string.

type Watcher

type Watcher struct {
	ServiceName      string
	Namespace        string
	Ctx              context.Context
	Cancel           context.CancelFunc
	Channel          <-chan model.SubScribeEvent
	ServiceInstances []gsvc.Service
}

Watcher is a service watcher.

func (*Watcher) Close

func (w *Watcher) Close() error

Close the watcher.

func (*Watcher) Proceed

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

Proceed returns services in the following two cases: 1.the first time to watch and the service instance list is not empty. 2.any service instance changes found. if the above two conditions are not met, it will block until the context deadline is exceeded or canceled

Jump to

Keyboard shortcuts

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