grpcpolaris

package module
v1.13.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 33 Imported by: 2

README

gRPC-Go with Polaris

English | 简体中文

Introduction

gRPC-Go-Polaris provides a series of components based on gRPC-Go framework, developers can use gRPC-Go-Polaris to build distributed gRPC-Go applications.

Key Features

  • Service Registration and Heartbeat: To register the gRPC Service and send heartbeat periodly.
  • Service Routing and LoadBalancing: Implement gRPC resover and balancer, providing semantic rule routing and loadbalacing cases.
  • Fault node circuitbreaking: Kick of the unhealthy nodes when loadbalacing, base on the service invoke successful rate.
  • RateLimiting: Implement gRPC interceptor, providing request ratelimit check to ensure the reliability for server.

Base Architecture

arch gRPC-Go-Polaris implements the interfaces on gRPC-Go, to access polarismesh functions.

How To Use

Prerequisites
  • Go: any one of the three latest major releases.
Installation

With Go module support (Go 1.11+), simply add the following import

import "github.com/polarismesh/grpc-go-polaris"

to your code, and then go [build|run|test] will automatically fetch the necessary dependencies.

Otherwise, to install the grpc-go-polaris package, run the following command:

$ go get -u github.com/polarismesh/grpc-go-polaris

Note: gRPC-Go-Polaris has gRPC-Go dependencies, it will encounter timeout while go getting gRPC-Go in China, to solution the problem, you can refer FAQ.

Examples

Documentation

Overview

Package grpcpolaris grpc-go integrates with polaris-go

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultNamespace default namespace when namespace is not set
	DefaultNamespace = "default"
	// DefaultTTL default ttl value when ttl is not set
	DefaultTTL = 20
	// DefaultGracefulStopMaxWaitDuration default stop max wait duration when not set
	DefaultGracefulStopMaxWaitDuration = 30 * time.Second
	// DefaultDelayStopWaitDuration default delay time before stop
	DefaultDelayStopWaitDuration = 4 * 2 * time.Second
	// MinGracefulStopWaitDuration low bound of stop wait duration
	MinGracefulStopWaitDuration = 5 * time.Second
)
View Source
var (
	// ErrorPolarisServiceRouteRuleEmpty error service route rule is empty
	ErrorPolarisServiceRouteRuleEmpty = errors.New("service route rule is empty")
)

Functions

func BuildTarget

func BuildTarget(target string, opts ...DialOption) (string, error)

BuildTarget build the invoker grpc target Deprecated: will remove in 1.4

func DialContext

func DialContext(ctx context.Context, target string, opts ...DialOption) (conn *grpc.ClientConn, err error)

DialContext dial target and get connection

func PolarisConfig

func PolarisConfig() config.Configuration

PolarisConfig get or init the global polaris configuration

func PolarisContext

func PolarisContext() (api.SDKContext, error)

PolarisContext get or init the global polaris context

func RegisterResolverInterceptor

func RegisterResolverInterceptor(i ResolverInterceptor)

RegisterResolverInterceptor NOTE: this function must only be called during initialization time (i.e. in an init() function), and is not thread-safe. If multiple RegisterInterceptor are registered with the same name, the one registered last will take effect.

func RequestScopeLbHashKey

func RequestScopeLbHashKey(ctx context.Context, key string) context.Context

RequestScopeLbHashKey set request scope LbHashKey

func RequestScopeLbPolicy

func RequestScopeLbPolicy(ctx context.Context, policy string) context.Context

RequestScopeLbPolicy set request scope LbPolicy

func Serve

func Serve(gSrv *grpc.Server, lis net.Listener, opts ...ServerOption) error

Serve start polaris server

func SetReportInfoAnalyzer

func SetReportInfoAnalyzer(analyzer ReportInfoAnalyzer)

SetReportInfoAnalyzer sets report info analyzer

Types

type DelayStrategy

type DelayStrategy interface {
	// Allow delay strategy is allowed to continue to exec
	Allow() bool
}

DelayStrategy delay register strategy. e.g. wait some time

type DialOption

type DialOption interface {
	// contains filtered or unexported methods
}

DialOption dialOptions for gRPC-Go-Polaris

func WithClientNamespace

func WithClientNamespace(namespace string) DialOption

WithClientNamespace set the namespace for dial service

func WithDisableCircuitBreaker

func WithDisableCircuitBreaker() DialOption

WithDisableCircuitBreaker close polaris circuitbreaker ability

func WithDisableRouter

func WithDisableRouter() DialOption

WithDisableRouter close polaris route ability

func WithDstMetadata

func WithDstMetadata(dstMetadata map[string]string) DialOption

WithDstMetadata set the dstMetadata for dial service routing

func WithEnableCircuitBreaker

func WithEnableCircuitBreaker() DialOption

WithEnableCircuitBreaker open polaris circuitbreaker ability

func WithEnableRouter

func WithEnableRouter() DialOption

WithEnableRouter open polaris route ability

func WithGRPCDialOptions

func WithGRPCDialOptions(opts ...grpc.DialOption) DialOption

WithGRPCDialOptions set the raw gRPC dialOption

func WithHeaderPrefix

func WithHeaderPrefix(headerPrefix []string) DialOption

WithHeaderPrefix set the header filter to get the header values to routing Deprecated: will remove in 1.4

func WithPolarisConfig

func WithPolarisConfig(polarisCfg config.Configuration) DialOption

WithPolarisConfig set polaris configuration

func WithPolarisContext

func WithPolarisContext(sdkContext api.SDKContext) DialOption

func WithSrcMetadata

func WithSrcMetadata(srcMetadata map[string]string) DialOption

WithSrcMetadata set the srcMetadata for dial service routing Deprecated: will remove in 1.4

func WithSrcService

func WithSrcService(srcService string) DialOption

WithSrcService set the srcMetadata for dial service routing

type LBConfig

type LBConfig struct {
	serviceconfig.LoadBalancingConfig `json:"-"`
	HashKey                           string `json:"hash_key"`
	LbPolicy                          string `json:"lb_policy"`
}

LBConfig is the LB config for the polaris policy.

type NoopDelayStrategy

type NoopDelayStrategy struct{}

NoopDelayStrategy noop delay strategy

func (*NoopDelayStrategy) Allow

func (d *NoopDelayStrategy) Allow() bool

Allow delay strategy is allowed to continue to exec

type RateLimitInterceptor

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

RateLimitInterceptor is a gRPC interceptor that implements rate limiting.

func NewRateLimitInterceptor

func NewRateLimitInterceptor() *RateLimitInterceptor

NewRateLimitInterceptor creates a new RateLimitInterceptor.

func (*RateLimitInterceptor) UnaryInterceptor

func (p *RateLimitInterceptor) UnaryInterceptor(ctx context.Context, req interface{},
	info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error)

UnaryInterceptor returns a unary interceptor for rate limiting.

func (*RateLimitInterceptor) WithNamespace

func (p *RateLimitInterceptor) WithNamespace(namespace string) *RateLimitInterceptor

WithNamespace sets the namespace of the service.

func (*RateLimitInterceptor) WithServiceName

func (p *RateLimitInterceptor) WithServiceName(svcName string) *RateLimitInterceptor

WithServiceName sets the service name.

type RegisterContext

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

RegisterContext context parameters by register

type ReportInfoAnalyzer

type ReportInfoAnalyzer func(info balancer.DoneInfo) (model.RetStatus, uint32)

ReportInfoAnalyzer analyze balancer.DoneInfo to polaris report info

type ResolverContext

type ResolverContext struct {
	Target        resolver.Target
	Host          string
	Port          int
	SourceService model.ServiceInfo
}

type ResolverInterceptor

type ResolverInterceptor interface {
	After(ctx *ResolverContext, response *model.InstancesResponse) *model.InstancesResponse
}

ResolverInterceptor 从 polaris sdk 拉取完 instances 执行过滤

type Server

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

Server encapsulated server with gRPC option

func Register

func Register(gSrv *grpc.Server, lis net.Listener, opts ...ServerOption) (*Server, error)

Register server as polaris instances

func (*Server) Deregister

func (s *Server) Deregister()

Deregister deregister services from polaris

func (*Server) Stop

func (s *Server) Stop()

Stop deregister and stop

type ServerOption

type ServerOption interface {
	// contains filtered or unexported methods
}

A ServerOption sets options such as credentials, codec and keepalive parameters, etc.

func WithDelayRegisterEnable

func WithDelayRegisterEnable(strategy DelayStrategy) ServerOption

WithDelayRegisterEnable enables delay register

func WithDelayStopDisable

func WithDelayStopDisable() ServerOption

WithDelayStopDisable disable delay stop Deprecated: will remove in 1.4

func WithDelayStopEnable

func WithDelayStopEnable(strategy DelayStrategy) ServerOption

WithDelayStopEnable enables delay stop Deprecated: will remove in 1.4

func WithGRPCServerOptions

func WithGRPCServerOptions(opts ...grpc.ServerOption) ServerOption

WithGRPCServerOptions set the raw gRPC serverOptions

func WithGracefulStopDisable

func WithGracefulStopDisable() ServerOption

WithGracefulStopDisable disable graceful stop

func WithGracefulStopEnable

func WithGracefulStopEnable(duration time.Duration) ServerOption

WithGracefulStopEnable enables graceful stop

func WithHeartbeatEnable

func WithHeartbeatEnable(enable bool) ServerOption

WithHeartbeatEnable enables the heartbeat task to instance Deprecated: will remove in 1.4

func WithPort

func WithPort(port int) ServerOption

WithPort set the port to register instance Deprecated: will remove in 1.4

func WithSDKContext

func WithSDKContext(sdkContext api.SDKContext) ServerOption

func WithServerApplication

func WithServerApplication(application string) ServerOption

WithServerApplication set application name Deprecated: use WithServiceName to replace WithServerApplication

func WithServerHost

func WithServerHost(host string) ServerOption

WithServerHost set the host to register instance

func WithServerMetadata

func WithServerMetadata(metadata map[string]string) ServerOption

WithServerMetadata set the metadata to register instance

func WithServerNamespace

func WithServerNamespace(namespace string) ServerOption

WithServerNamespace set the namespace to register instance

func WithServerVersion

func WithServerVersion(version string) ServerOption

WithServerVersion set the version to register instance

func WithServiceName

func WithServiceName(svcName string) ServerOption

WithServiceName set the application to register instance

func WithTTL

func WithTTL(ttl int) ServerOption

WithTTL set the ttl to register instance

func WithToken

func WithToken(token string) ServerOption

WithToken set the token to do server operations

type WaitDelayStrategy

type WaitDelayStrategy struct {
	WaitTime time.Duration
}

WaitDelayStrategy wait delay strategy

func (*WaitDelayStrategy) Allow

func (d *WaitDelayStrategy) Allow() bool

Allow delay strategy is allowed to continue to exec

Jump to

Keyboard shortcuts

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