file

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: 11 Imported by: 5

README

GoFrame File Registry

Use file as service registration and discovery management.

Installation

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

suggested using go.mod:

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

Example

Reference example

server

package main

import (
	"github.com/gogf/gf/contrib/registry/file/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/net/gsvc"
	"github.com/gogf/gf/v2/os/gfile"
)

func main() {
	gsvc.SetRegistry(file.New(gfile.Temp("gsvc")))

	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/gogf/gf/contrib/registry/file/v2"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/gsvc"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/gfile"
)

func main() {
	gsvc.SetRegistry(file.New(gfile.Temp("gsvc")))

	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)
	}
}

Documentation

Overview

Package file implements service Registry and Discovery using file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(path string) gsvc.Registry

New creates and returns a gsvc.Registry implements using file.

Types

type Registry

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

Registry implements interface Registry using file. This implement is usually for testing only.

func (*Registry) Deregister

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

Deregister off-lines and removes `service` from the Registry.

func (*Registry) Register

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

Register registers `service` to Registry. Note that it returns a new Service if it changes the input Service with custom one.

func (*Registry) Search

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

Search searches and returns services with specified condition.

func (*Registry) Watch

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

Watch watches specified condition changes. The `key` is the prefix of service key.

type Service

type Service struct {
	gsvc.Service
	Endpoints gsvc.Endpoints
	Metadata  gsvc.Metadata
}

Service wrapper.

func NewService

func NewService(service gsvc.Service) *Service

NewService creates and returns local Service from gsvc.Service interface object.

func (*Service) GetEndpoints

func (s *Service) GetEndpoints() gsvc.Endpoints

GetEndpoints returns the Endpoints of service. The Endpoints contain multiple host/port information of service.

func (*Service) GetMetadata

func (s *Service) GetMetadata() gsvc.Metadata

GetMetadata returns the Metadata map of service. The Metadata is key-value pair map specifying extra attributes of a service.

func (*Service) GetValue

func (s *Service) GetValue() string

GetValue formats and returns the value of the service. The result value is commonly used for key-value registrar server.

type Watcher

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

Watcher for file changes watch.

func (*Watcher) Close

func (w *Watcher) Close() error

Close closes the watcher.

func (*Watcher) Proceed

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

Proceed proceeds watch in blocking way. It returns all complete services that watched by `key` if any change.

Jump to

Keyboard shortcuts

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