api

package
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2023 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 3 more Imports: 22 Imported by: 0

README

Polaris Go API方法使用

配置API

配置初始化
// 使用默认配置对象来初始化配置,连接默认的北极星埋点域名
cfg := api.NewConfiguration()
修改北极星后端集群名
// 先初始化配置
cfg := api.NewConfiguration()
// 修改发现server集群名
cfg.GetGlobal().GetSystem().GetDiscoverCluster().SetService("polaris-server")
// 修改心跳server集群名
cfg.GetGlobal().GetSystem().GetHealthCheckCluster().SetService("healthcheck")
// 修改监控server集群名
cfg.GetGlobal().GetSystem().GetHealthCheckCluster().SetService("polaris.monitor")
修改北极星日志路径

假如需要修改北极星的日志打印目录,可以按照以下方式进行修改

if err := api.SetLoggersDir("/tmp/polaris/log"); err != nil {
// do error handle
}
修改北极星日志级别

假如需要修改北极星的日志打印级别,可以按照以下方式进行修改

if err := api.SetLoggersLevel(api.InfoLog); err != nil {
// do error handle
}

日志级别支持NoneLog, TraceLog, DebugLog, InfoLog, WarnLog, ErrorLog, FatalLog,设置成NoneLog,则不打印任何日志

同时修改北极星日志路径及日志级别

polaris-go启动后,默认会在程序运行的当前目录创建polaris/log目录,用于存放运行过程中的日志。因此用户需要保证当前目录有写权限 假如需要修改北极星的日志打印目录以及日志级别,可以按照以下方式进行修改

if err := api.ConfigLoggers("/tmp/polaris/log", api.InfoLog); err != nil {
// do error handle
}

Documentation

Overview

Package api is the core API of polaris-go, which provides the basic functions of service discovery and.

Index

Constants

View Source
const (
	// LBPolicyWeightedRandom 权重随机负载均衡策略
	LBPolicyWeightedRandom = config.DefaultLoadBalancerWR
	// LBPolicyRingHash 权重一致性hash负载均衡策略
	LBPolicyRingHash = config.DefaultLoadBalancerRingHash
	// LBPolicyMaglev Maglev算法的一致性hash负载均衡策略
	LBPolicyMaglev = config.DefaultLoadBalancerMaglev
	// LBPolicyL5CST L5一致性Hash兼容算法,保证和L5产生相同的结果
	LBPolicyL5CST = config.DefaultLoadBalancerL5CST
)
View Source
const (
	// RetSuccess the call is successful
	RetSuccess = model.RetSuccess
	// RetFail call fails
	RetFail = model.RetFail
)
View Source
const (
	// WatchModeLongPull watch model by long pulling, the invocation would be hang on until revision updated or timeout
	WatchModeLongPull = model.WatchModeLongPull
	// WatchModeNotify watch model by notify to listener
	WatchModeNotify = model.WatchModeNotify
)
View Source
const (
	// QuotaResultOk 限流状态值
	QuotaResultOk = model.QuotaResultOk
	// QuotaResultLimited 限流结果
	QuotaResultLimited = model.QuotaResultLimited
)
View Source
const (
	// TraceLog 跟踪级别
	TraceLog = log.TraceLog
	// DebugLog 调试级别
	DebugLog = log.DebugLog
	// InfoLog 一般日志级别
	InfoLog = log.InfoLog
	// WarnLog 警告日志级别
	WarnLog = log.WarnLog
	// ErrorLog 错误日志级别
	ErrorLog = log.ErrorLog
	// FatalLog 致命级别
	FatalLog = log.FatalLog
	// NoneLog 当要禁止日志的时候,可以设置此级别
	NoneLog = log.NoneLog
)

日志级别

View Source
const (
	// DefaultBaseLogLevel 默认基本日志级别
	DefaultBaseLogLevel = log.DefaultBaseLogLevel
	// DefaultStatLogLevel 默认统计日志级别
	DefaultStatLogLevel = log.DefaultStatLogLevel
	// DefaultDetectLogLevel 默认探测日志级别
	DefaultDetectLogLevel = log.DefaultDetectLogLevel
	// DefaultStatReportLogLevel 默认统计上报日志级别
	DefaultStatReportLogLevel = log.DefaultStatReportLogLevel
	// DefaultNetworkLogLevel 默认网络交互日志级别
	DefaultNetworkLogLevel = log.DefaultNetworkLogLevel
	// DefaultCacheLogLevel 默认缓存日志级别
	DefaultCacheLogLevel = log.DefaultCacheLogLevel
)
View Source
const (
	// EventInstance .
	EventInstance = model.EventInstance
)

Variables

View Source
var (
	// NewConfigFileAPIBySDKContext 通过 SDKContext 创建 ConfigFileAPI
	NewConfigFileAPIBySDKContext = newConfigFileAPIBySDKContext
	// NewConfigFileAPI 通过 polaris.yaml 创建 ConfigFileAPI
	NewConfigFileAPI = newConfigFileAPI
	// NewConfigFileAPIByConfig 通过 Configuration 创建 ConfigFileAPI
	NewConfigFileAPIByConfig = newConfigFileAPIByConfig
	// NewConfigFileAPIByFile 通过配置文件创建 ConfigFileAPI
	NewConfigFileAPIByFile = newConfigFileAPIByFile
)
View Source
var (
	// NewConsumerAPI 通过以默认域名为埋点server的默认配置创建ConsumerAPI
	NewConsumerAPI = newConsumerAPI
	// NewConsumerAPIByFile 通过配置文件创建SDK ConsumerAPI对象
	NewConsumerAPIByFile = newConsumerAPIByFile
	// NewConsumerAPIByConfig 通过配置对象创建SDK ConsumerAPI对象
	NewConsumerAPIByConfig = newConsumerAPIByConfig
	// NewConsumerAPIByContext 通过上下文创建SDK ConsumerAPI对象
	NewConsumerAPIByContext = newConsumerAPIByContext
	// NewConsumerAPIByDefaultConfigFile 从系统默认配置文件中创建ConsumerAPI
	NewConsumerAPIByDefaultConfigFile = newConsumerAPIByDefaultConfigFile
	// NewServiceCallResult 创建上报对象
	NewServiceCallResult = newServiceCallResult
	// NewConsumerAPIByAddress 通过address创建ConsumerAPI对象
	NewConsumerAPIByAddress = newConsumerAPIByAddress
)
View Source
var (
	// NewLimitAPI 通过以默认域名为埋点server的默认配置创建LimitAPI
	NewLimitAPI = newLimitAPI
	// NewLimitAPIByConfig 通过配置对象创建LimitAPI
	NewLimitAPIByConfig = newLimitAPIByConfig
	// NewLimitAPIByContext 通过sdkContext创建LimitAPI
	NewLimitAPIByContext = newLimitAPIByContext
	// NewLimitAPIByFile 通过配置文件创建SDK LimitAPI对象
	NewLimitAPIByFile = newLimitAPIByFile
	// NewLimitAPIByAddress 通过address创建LimitAPI
	NewLimitAPIByAddress = newLimitAPIByAddress
)
View Source
var (
	// NewProviderAPI 通过以默认域名为埋点server的默认配置创建ProviderAPI
	NewProviderAPI = newProviderAPI
	// NewProviderAPIByFile 通过配置文件创建SDK ProviderAPI对象
	NewProviderAPIByFile = newProviderAPIByFile
	// NewProviderAPIByConfig 通过配置对象创建SDK ProviderAPI对象
	NewProviderAPIByConfig = newProviderAPIByConfig
	// NewProviderAPIByContext 通过上下文创建SDK ProviderAPI对象
	NewProviderAPIByContext = newProviderAPIByContext
	// NewProviderAPIByDefaultConfigFile 通过系统默认配置文件创建ProviderAPI
	NewProviderAPIByDefaultConfigFile = newProviderAPIByDefaultConfigFile
	// NewProviderAPIByAddress 通过address创建ProviderAPI对象
	NewProviderAPIByAddress = newProviderAPIByAddress
)

Functions

func CheckAvailable

func CheckAvailable(owner SDKOwner) error

CheckAvailable 判断API是否可用

func ConfigBaseLogger

func ConfigBaseLogger(logDir string, logLevel int) error

ConfigBaseLogger 配置基础日志对象

func ConfigCacheLogger

func ConfigCacheLogger(logDir string, logLevel int) error

ConfigCacheLogger 配置缓存更新日志对象

func ConfigDetectLogger

func ConfigDetectLogger(logDir string, logLevel int) error

ConfigDetectLogger 配置探测日志对象

func ConfigLoggers

func ConfigLoggers(logDir string, logLevel int) error

ConfigLoggers 全局配置日志对象

func ConfigNetworkLogger

func ConfigNetworkLogger(logDir string, logLevel int) error

ConfigNetworkLogger 配置网络交互日志对象

func ConfigStatLogger

func ConfigStatLogger(logDir string, logLevel int) error

ConfigStatLogger 配置统计日志对象

func ConfigStatReportLogger

func ConfigStatReportLogger(logDir string, logLevel int) error

ConfigStatReportLogger 配置统计上报日志对象

func NewConfiguration

func NewConfiguration() config.Configuration

NewConfiguration 创建默认配置

func SetBaseLogger

func SetBaseLogger(logger Logger)

SetBaseLogger 设置基础日志对象

func SetCacheLogger

func SetCacheLogger(logger Logger)

SetCacheLogger 设置缓存日志对象

func SetDetectLogger

func SetDetectLogger(logger Logger)

SetDetectLogger 设置探测日志对象

func SetLoggersDir

func SetLoggersDir(logDir string) error

SetLoggersDir 设置日志的目录,会创建新的具有默认打印级别的logger

func SetLoggersLevel

func SetLoggersLevel(loglevel int) error

SetLoggersLevel 设置所有日志级别

func SetStatLogger

func SetStatLogger(logger Logger)

SetStatLogger 设置统计日志对象

func SetStatReportLogger

func SetStatReportLogger(logger Logger)

SetStatReportLogger 设置统计上报日志对象

Types

type ConfigFileAPI

type ConfigFileAPI interface {
	SDKOwner
	// GetConfigFile 获取配置文件
	GetConfigFile(namespace, fileGroup, fileName string) (model.ConfigFile, error)
}

ConfigFileAPI 配置文件的 API

type ConsumerAPI

type ConsumerAPI interface {
	SDKOwner
	// GetOneInstance 获取单个服务(会执行路由链与负载均衡,获取负载均衡后的服务实例)
	GetOneInstance(req *GetOneInstanceRequest) (*model.OneInstanceResponse, error)
	// GetInstances 获取可用的服务列表(会执行路由链,默认去掉隔离以及不健康的服务实例)
	GetInstances(req *GetInstancesRequest) (*model.InstancesResponse, error)
	// GetAllInstances 获取完整的服务列表(包括隔离及不健康的服务实例)
	GetAllInstances(req *GetAllInstancesRequest) (*model.InstancesResponse, error)
	// GetRouteRule 同步获取服务路由规则
	GetRouteRule(req *GetServiceRuleRequest) (*model.ServiceRuleResponse, error)
	// UpdateServiceCallResult 上报服务调用结果
	UpdateServiceCallResult(req *ServiceCallResult) error
	// Destroy 销毁API,销毁后无法再进行调用
	Destroy()
	// Deprecated: please use WatchAllInstances instead
	// WatchService 订阅服务消息
	WatchService(req *WatchServiceRequest) (*model.WatchServiceResponse, error)
	// GetServices 根据业务同步获取批量服务
	GetServices(req *GetServicesRequest) (*model.ServicesResponse, error)
	// InitCalleeService 初始化服务运行中需要的被调服务
	InitCalleeService(req *InitCalleeServiceRequest) error
	// WatchAllInstances 监听服务实例变更事件
	WatchAllInstances(req *WatchAllInstancesRequest) (*model.WatchAllInstancesResponse, error)
	// WatchAllServices 监听服务列表变更事件
	WatchAllServices(req *WatchAllServicesRequest) (*model.WatchAllServicesResponse, error)
}

ConsumerAPI 主调端API方法

type GetAllInstancesRequest

type GetAllInstancesRequest struct {
	model.GetAllInstancesRequest
}

GetAllInstancesRequest 获取服务下所有实例的请求对象

type GetInstancesRequest

type GetInstancesRequest struct {
	model.GetInstancesRequest
}

GetInstancesRequest 获取多个服务的请求对象

type GetOneInstanceRequest

type GetOneInstanceRequest struct {
	model.GetOneInstanceRequest
}

GetOneInstanceRequest 获取单个服务的请求对象

type GetServiceRuleRequest

type GetServiceRuleRequest struct {
	model.GetServiceRuleRequest
}

GetServiceRuleRequest 获取服务规则请求

type GetServicesRequest

type GetServicesRequest struct {
	model.GetServicesRequest
}

GetServicesRequest 获取批量服务请求

type InitCalleeServiceRequest

type InitCalleeServiceRequest struct {
	model.InitCalleeServiceRequest
}

InitCalleeServiceRequest .

type InstanceDeRegisterRequest

type InstanceDeRegisterRequest struct {
	model.InstanceDeRegisterRequest
}

InstanceDeRegisterRequest 反注册服务请求

type InstanceHeartbeatRequest

type InstanceHeartbeatRequest struct {
	model.InstanceHeartbeatRequest
}

InstanceHeartbeatRequest 心跳上报请求

type InstanceRegisterRequest

type InstanceRegisterRequest struct {
	model.InstanceRegisterRequest
}

InstanceRegisterRequest 注册服务请求

type InstanceRequest

type InstanceRequest struct {
	// 服务标识
	model.ServiceKey
	// InstanceID 实例ID
	InstanceID string

	IP   string
	Port uint16
}

InstanceRequest 实例请求

func (InstanceRequest) Validate

func (g InstanceRequest) Validate() error

Validate 校验实例请求对象

type LimitAPI

type LimitAPI interface {
	SDKOwner
	// GetQuota 获取限流配额,一次接口只获取一个配额
	GetQuota(request QuotaRequest) (QuotaFuture, error)
	// Destroy 销毁API,销毁后无法再进行调用
	Destroy()
}

LimitAPI 限流相关的API相关接口

type Logger

type Logger log.Logger

Logger 别名类

func GetBaseLogger

func GetBaseLogger() Logger

GetBaseLogger 获取基础日志对象

func GetCacheLogger

func GetCacheLogger() Logger

GetCacheLogger 获取缓存日志对象

func GetDetectLogger

func GetDetectLogger() Logger

GetDetectLogger 获取探测日志对象

func GetStatLogger

func GetStatLogger() Logger

GetStatLogger 获取统计日志对象

func GetStatReportLogger

func GetStatReportLogger() Logger

GetStatReportLogger 获取统计上报日志对象

type ProviderAPI

type ProviderAPI interface {
	SDKOwner
	// RegisterInstance
	// minimum supported version of polaris-server is v1.10.0
	RegisterInstance(instance *InstanceRegisterRequest) (*model.InstanceRegisterResponse, error)
	// Register
	// 同步注册服务,服务注册成功后会填充instance中的InstanceID字段
	// 用户可保持该instance对象用于反注册和心跳上报
	// Deprecated: Use RegisterInstance instead.
	Register(instance *InstanceRegisterRequest) (*model.InstanceRegisterResponse, error)
	// Deregister synchronize the anti registration service
	Deregister(instance *InstanceDeRegisterRequest) error
	// Heartbeat the heartbeat report
	// Deprecated: Use RegisterInstance instead.
	Heartbeat(instance *InstanceHeartbeatRequest) error
	// Destroy the api is destroyed and cannot be called again
	Destroy()
}

ProviderAPI CL5服务端API的主接口

type QuotaFuture

type QuotaFuture interface {
	// Done 标识分配是否结束
	Done() <-chan struct{}
	// Get 等待一段时间后,获取分配结果,用于匀速排队
	Get() *model.QuotaResponse
	// GetImmediately 立刻获取分配结果,不等待
	GetImmediately() *model.QuotaResponse
	// Release 释放资源,仅用于并发数限流的场景
	Release()
}

QuotaFuture 实时/延时分配future

type QuotaRequest

type QuotaRequest interface {

	// SetNamespace 设置命名空间
	SetNamespace(string)

	// SetService 设置服务名
	SetService(string)

	// SetLabels 设置业务标签信息
	// Deprecated: please use AddArgument instead
	SetLabels(map[string]string)

	// SetMethod set method
	SetMethod(method string)

	// AddArgument add the match argument
	AddArgument(argument model.Argument)

	// SetToken set token to acquire
	SetToken(uint32)

	// SetTimeout 设置单次请求超时时间
	SetTimeout(timeout time.Duration)

	// SetRetryCount 设置最大重试次数
	SetRetryCount(retryCount int)
}

QuotaRequest 配额查询请求

func NewQuotaRequest

func NewQuotaRequest() QuotaRequest

NewQuotaRequest 创建配额查询请求

type SDKContext

type SDKContext interface {
	// Destroy
	// @brief 销毁SDK上下文
	Destroy()

	// IsDestroyed
	// @brief SDK上下文是否已经销毁
	IsDestroyed() bool

	// GetConfig
	// @brief 获取全局配置信息
	GetConfig() config.Configuration

	// GetPlugins
	// @brief 获取插件列表
	GetPlugins() plugin.Manager

	// GetEngine
	// @brief 获取执行引擎
	GetEngine() model.Engine

	// GetValueContext
	// @brief 获取值上下文
	GetValueContext() model.ValueContext
}

SDKContext . @brief SDK配置对象,每个API实例都会挂载一个context,包含: 插件实例列表 配置实例 执行流程引擎,包括定时器等

func InitContextByConfig

func InitContextByConfig(cfg config.Configuration) (SDKContext, error)

InitContextByConfig InitContextByStream 通过配置对象新建上下文

func InitContextByFile

func InitContextByFile(path string) (SDKContext, error)

InitContextByFile 通过配置文件新建服务消费者配置

func InitContextByStream

func InitContextByStream(buf []byte) (SDKContext, error)

InitContextByStream 通过YAML流新建服务消费者配置

type SDKOwner

type SDKOwner interface {
	// SDKContext 获取SDK上下文
	SDKContext() SDKContext
}

SDKOwner 获取SDK上下文接口

type ServiceCallResult

type ServiceCallResult struct {
	model.ServiceCallResult
}

ServiceCallResult 服务调用结果

type WatchAllInstancesRequest

type WatchAllInstancesRequest struct {
	model.WatchAllInstancesRequest
}

WatchAllInstancesRequest .

type WatchAllServicesRequest

type WatchAllServicesRequest struct {
	model.WatchAllServicesRequest
}

WatchAllServicesRequest .

type WatchServiceRequest

type WatchServiceRequest struct {
	model.WatchServiceRequest
}

WatchServiceRequest WatchService req

Jump to

Keyboard shortcuts

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