soa

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2023 License: MIT Imports: 16 Imported by: 0

README

SOA

Default health check is using TTL, if you want to add custom health check for a service, you can provide a health check handler by setting HealthCheck option, or if you want to use HTTP health check, you can add a health endpoint by setting HealthCheckPath in AppConfig.

Usage:

import "github.com/lalamove/gokit/soa"

s, err := soa.New(
    soa.WithApp(soa.AppConfig{
        ID: "my-demo-svc",
    }),
    soa.WithConsul(soa.ConsulConfig{
        Host:  "127.0.0.1",
        Port:  8500,
        Token: "my-token",
    }),
)

if err != nil {
    log.Fatal("fail to create soa object")
}

// register service to consul
if err := s.Register(); err != nil {
    log.Fatal("fail to register service")
}

// resolve dependency service's address
host, err := s.Resolve("my-dependency-svc")
// resolve dependency service's address by version
host2, err2 := s.ResolveWithVersion("my-dependency-svc", "v123")

if err != nil {
    log.Println("fail to resolve my-dependency-svc")
}

fmt.Println(host.Addr)

// deregister service for graceful shutdown
s.Deregister()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppConfig

type AppConfig struct {
	ID                             string
	Port                           int
	HealthCheckPath                string // use http check if it's set
	CheckTimeout                   string
	CheckInterval                  string
	CheckTTL                       string
	DeregisterCriticalServiceAfter string
	Version                        string
}

AppConfig is application config

type ConsulConfig

type ConsulConfig struct {
	Host  string
	Port  int
	Token string
}

ConsulConfig is consul config

type EmptyError

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

EmptyError is empty upstream pool error

func (*EmptyError) Error

func (ue *EmptyError) Error() string

Error returns error description

type HealthChecker added in v0.4.4

type HealthChecker func() error

HealthChecker is the customized health check function

type Host

type Host struct {
	IP      string
	Port    int
	Addr    string
	Version string
}

Host contains service ip and port

type Option

type Option func(*Options)

Option is for setting options

func WithApp

func WithApp(appCfg AppConfig) Option

WithApp sets application config

func WithConsul

func WithConsul(consulCfg ConsulConfig) Option

WithConsul sets consul config

func WithHealthCheck added in v0.4.4

func WithHealthCheck(hc HealthChecker) Option

WithHealthCheck sets health checker

func WithLogger

func WithLogger(logger log.Logger) Option

WithLogger sets logger

func WithUpstream

func WithUpstream(upstreamCfg UpstreamConfig) Option

WithUpstream sets upstream config

type Options

type Options struct {
	Logger      log.Logger
	App         AppConfig
	Consul      ConsulConfig
	Upstream    UpstreamConfig
	HealthCheck HealthChecker
}

Options is soa options

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions creates options with defaults

type SOA

type SOA struct {
	Options      // inherited options
	ConsulClient *consulapi.Client
	Service      *consulapi.AgentServiceRegistration
	// contains filtered or unexported fields
}

SOA is for soa service registration and discovery

func New

func New(opts ...Option) (*SOA, error)

New creates a new SOA object

func (*SOA) Deregister

func (soa *SOA) Deregister() error

Deregister deregisters service

func (*SOA) Register

func (soa *SOA) Register() error

Register registers service

func (*SOA) Resolve

func (soa *SOA) Resolve(appID string) (*Host, error)

Resolve service address by app id

func (*SOA) ResolveWithVersion added in v0.6.0

func (soa *SOA) ResolveWithVersion(appID string, version string) (*Host, error)

Resolve service address by app id & version

func (*SOA) TTLCheck added in v0.4.4

func (soa *SOA) TTLCheck() error

TTLCheck checks service health by ttl

type UpstreamConfig

type UpstreamConfig struct {
	WaitTimeout time.Duration
	MaxRetries  int
}

UpstreamConfig is upstream config

Jump to

Keyboard shortcuts

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