metadata

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package metadata 定义了 NNS(enjoydream Naming Service)中使用的核心元数据类型和常量。

包含服务名称解析结构 ServiceName(格式为 region.biz1.biz2.name.protocol)、 服务基础配置 ServiceBasic(超时、重试、负载均衡策略、认证、路由等)、 以及各类基础设施的认证信息(MySQL、Redis、HTTP、RocketMQ、Kafka、Elasticsearch)。 同时定义了协议类型常量、负载均衡策略常量和 NNS HTTP 头部常量,供整个 NNS 体系共享使用。

Index

Constants

View Source
const (
	// ProtocolHTTP HTTP 协议。
	ProtocolHTTP = "http"
	// ProtocolGRPC gRPC 协议。
	ProtocolGRPC = "grpc"
	// ProtocolRedis Redis 协议。
	ProtocolRedis = "redis"
	// ProtocolMySQL MySQL 协议。
	ProtocolMySQL = "mysql"
	// ProtocolRocketMQ RocketMQ 协议。
	ProtocolRocketMQ = "rocketmq"
	// ProtocolKafka Kafka 协议。
	ProtocolKafka = "kafka"
	// ProtocolElasticsearch Elasticsearch 协议。
	ProtocolElasticsearch = "elasticsearch"

	// LbRoundRobin 轮询负载均衡策略。
	LbRoundRobin = "roundrobin"
	// LbRandom 随机负载均衡策略。
	LbRandom = "random"
	// LbWeightedRandom 加权随机负载均衡策略。
	LbWeightedRandom = "weighted"

	// DefaultHTTPPort 默认 HTTP 端口。
	DefaultHTTPPort = 8080
	// DefaultGRPCPort 默认 gRPC 端口。
	DefaultGRPCPort = 9090

	// HeaderColor 灰度颜色标识请求头。
	HeaderColor = "x-nns-color"
	// HeaderRegion 区域标识请求头。
	HeaderRegion = "x-nns-region"
	// HeaderServiceName 服务名标识请求头。
	HeaderServiceName = "x-nns-servicename"
)

协议类型常量

Variables

This section is empty.

Functions

This section is empty.

Types

type ACLConfig

type ACLConfig struct {
	Acl            bool                              `json:"acl"`
	AclPolicy      string                            `json:"acl_policy"`
	AclRule        map[string]map[string]int         `json:"acl_rule"`
	AclRulePattern map[*regexp.Regexp]map[string]int `json:"-"`
}

ACLConfig 访问控制配置。

type Auth

type Auth struct {
	MySQL         MySQLAuth         `json:"mysql"`
	Redis         RedisAuth         `json:"redis"`
	HTTP          HTTPAuth          `json:"http"`
	RocketMQ      RocketMQAuth      `json:"rocket_mq"`
	Kafka         KafkaAuth         `json:"kafka"`
	Elasticsearch ElasticsearchAuth `json:"elasticsearch"`
}

Auth 各类中间件和服务的统一鉴权配置。

type ConnectionConfig

type ConnectionConfig struct {
	ConnTimeout  int    `json:"conn_timeout"`  // 连接超时(毫秒)
	ReadTimeout  int    `json:"read_timeout"`  // 读超时(毫秒)
	WriteTimeout int    `json:"write_timeout"` // 写超时(毫秒)
	Retry        int    `json:"retry"`         // 重试次数
	Lb           string `json:"lb"`            // 负载均衡策略
}

ConnectionConfig 连接配置(超时、重试、负载均衡策略)。

type ElasticsearchAuth

type ElasticsearchAuth struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

ElasticsearchAuth Elasticsearch 鉴权配置。

type Endpoint

type Endpoint struct {
	Addr string            `json:"addr"`
	Port int               `json:"port"`
	Meta map[string]string `json:"meta"`
}

Endpoint 服务端点信息,包含地址、端口和元数据。

type HTTPAuth

type HTTPAuth struct {
	Secret string `json:"secret"`
}

HTTPAuth HTTP 服务鉴权配置。

type KafkaAuth

type KafkaAuth struct{}

KafkaAuth Kafka 鉴权配置。

type MySQLAuth

type MySQLAuth struct {
	DBName string  `json:"dbname"`
	Master UserPsw `json:"master"`
	Slave  UserPsw `json:"slave"`
}

MySQLAuth MySQL 鉴权配置。

type RedisAuth

type RedisAuth struct {
	Token string `json:"token"`
	User  string `json:"user"`
}

RedisAuth Redis 鉴权配置。

type RocketMQAuth

type RocketMQAuth struct {
	AccessKey string `json:"access_key"`
	SecretKey string `json:"secret_key"`
}

RocketMQAuth RocketMQ 鉴权配置。

type Route

type Route []map[string]map[string]string

Route 服务路由规则定义。

type ServiceBasic

type ServiceBasic struct {
	ConnectionConfig                 // 连接配置
	Auth             Auth            `json:"auth"`
	ACLConfig                        // 访问控制配置
	Route            Route           `json:"route"`
	Extra            any             `json:"extra"`
	Endpoints        any             `json:"endpoints"`
	TimeoutRetryCfg  []*TimeoutRetry `json:"timeout_retry_rule"`
	ServiceName      ServiceName     `json:"service_name"`
}

ServiceBasic 服务基本配置,包含超时、重试、负载均衡策略和鉴权等信息。

使用嵌入子结构体按职责分组,字段访问方式不变(Go 自动提升):

sb.ConnTimeout        // 等价于 sb.ConnectionConfig.ConnTimeout
sb.Auth.Redis.Token   // 等价于 sb.AuthConfig.Auth.Redis.Token

func NewServiceBasic

func NewServiceBasic() *ServiceBasic

NewServiceBasic 创建带有默认值的 ServiceBasic 实例。

type ServiceName

type ServiceName struct {
	FullName string `json:"full_name"`
	Region   string `json:"region"`
	Biz1     string `json:"biz1"`
	Biz2     string `json:"biz2"`
	Name     string `json:"name"`
	Protocol string `json:"protocol"`
}

ServiceName 服务全名,包含区域、业务线和协议等维度信息。

func ParseServiceName

func ParseServiceName(fullName string) *ServiceName

ParseServiceName 将 "region.biz1.biz2.name.protocol" 形式的全名解析为 ServiceName。 段数不为 5 时返回仅填充 FullName 的 ServiceName(兼容未规范化的服务名)。 空串返回 nil。

type TimeoutRetry

type TimeoutRetry struct {
	Api     string `json:"api"`
	Type    int    `json:"type"`
	Timeout int    `json:"timeout"`
	Retry   int    `json:"retry"`
	Enable  bool   `json:"enable"`
}

TimeoutRetry 按接口维度的超时重试配置。

type UserPsw

type UserPsw struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

UserPsw 用户名密码对。

Jump to

Keyboard shortcuts

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