micro

package module
v0.0.0-...-3e22abe Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 7 Imported by: 0

README

微服务框架,缝合来源

- go-micro

- pitaya

文件夹说明

  • breaker 熔断器
  • broker 异步推送(默认kafka)
  • breaker grpc客户端
  • codec 序列化工具
  • config 全局配置
  • errors 通用错误
  • log 日志
  • registry 注册服务
  • resolver
  • selector 选择器
  • tracing 链路追踪
  • transport grpc传输层
  • utils 通用工具

IDEA 设置

  • Ctrl+Shift+A 搜索Registry并点击
取消勾选下列值
go.run.processes.with.pty
terminal.use.conpty.on.windows

组件方法命名

1. 标准HTTP restful方法(不可以网关转发)
Get
List
Create
Update
Patch
Delete
2. 非标准HTTP方法,使用下述前缀,内部提取前缀后的字符串小写为方法名(不可以网关转发)
GET_
POST_
PUT_
PATCH_
DELETE_
3. 内部RPC,使用下述前缀,内部提取前缀后的字符串小写为方法名(不可以网关转发)
RPC_
4. 其他公开方法为外部RPC方法,统一POST请求(可以网关转发)

Documentation

Index

Constants

View Source
const (
	NodeHeader  = "X-Node-Id"      // 限定 node
	TokenHeader = "X-Auth-Token"   // 认证头
	TokenScope  = "X-Token-Scope"  // token范围
	TokenTenant = "X-Token-Tenant" // token限定租户范围

	ContentType = "Content-Type"
	Accept      = "Accept"
	Host        = "Host"
	Tenant      = "Tenant"
	PrimaryKey  = "PrimaryKey"
)

Variables

View Source
var (
	DefaultCodecs = map[string]string{
		"text/html":              "application/grpc+bytes",
		"text/plain":             "application/grpc+bytes",
		"application/grpc+json":  "application/grpc+json",
		"application/grpc+proto": "application/grpc+proto",
		"application/grpc+bytes": "application/grpc+bytes",

		"application/json":         "application/grpc+json",
		"application/grpc":         "application/grpc+proto",
		"application/protobuf":     "application/grpc+proto",
		"application/octet-stream": "application/grpc+bytes",
	}

	DefaultContentType = "application/grpc+bytes"
)
View Source
var (
	// ErrIPNotFound no IP address found, and explicit IP not provided.
	ErrIPNotFound = errors.New("no IP address found, and explicit IP not provided")

	ErrConfigCountNotMatch = errors.New("config count not match")
	ErrHandlerFound        = errors.New("handler not found")
	ErrMismatch            = errors.New("value mismatch")
	ErrConfigFound         = errors.New("config not found")
	ErrServiceNotFound     = errors.New("service not found")
	ErrWatcherStopped      = errors.New("watcher stopped")

	ErrSelectServiceNotFound  = errors.New("no service found")
	ErrSelectEndpointNotFound = errors.New("endpoint not found")
	ErrNoneServiceAvailable   = errors.New("none available") // node not found

	ErrResultFailed = errors.New("result failed") // node not found
	ErrUnknown      = errors.New("unknown error") // node not found
)
View Source
var (
	NoVersion  = errors.New("no version")
	VersionErr = errors.New("version value error")
)

Functions

func GetProtocol

func GetProtocol(ContentType string) string

func MatchCodec

func MatchCodec(protocol, codec string) bool

Types

type Component

type Component interface {
	/*
		Init 初始化执行
	*/
	Init()
	/*
		AfterInit 初始化完成后执行
	*/
	AfterInit()
	/*
		BeforeShutdown 进程停止前时执行
	*/
	BeforeShutdown()
	/*
		Shutdown 进程停止时执行
	*/
	Shutdown()
	/*
		Name 组件对应Resource名
	*/
	Name() string
	/*
		Collection 组件对应Collection名(resource的复数形式)
	*/
	Collection() string
	/*
		Hooks pre execute hook, nil able
		@method  原始方法名
	*/
	Hooks(method string) []PreExecuteHook
}

Component 通用api组件 1. Restful方法名 Get/List/Create/Update/Patch/Delete 2. 非Restful方法名 以 GET_/POST_/PUT_/PATCH_/DELETE_开头,其余部分小写为路径 e.g User.GET_Money, 路径为/user/money 3. 以RPC_开头的方法为内部rpc方法 4. 其他方法为注册到网关可转发方法(不可与3分割后同名) 5. 一般不建议在组件中设置生命周期方法,尽量在模块中做生命周期相关操作

type ComponentBase

type ComponentBase struct{}

ComponentBase 通用组件继承

func (*ComponentBase) AfterInit

func (*ComponentBase) AfterInit()

func (*ComponentBase) BeforeShutdown

func (*ComponentBase) BeforeShutdown()

func (*ComponentBase) Hooks

func (*ComponentBase) Hooks(_ string) []PreExecuteHook

func (*ComponentBase) Init

func (*ComponentBase) Init()

func (*ComponentBase) Shutdown

func (*ComponentBase) Shutdown()

type Endpoint

type Endpoint struct {
	Name       string            `json:"name"`
	Metadata   map[string]string `json:"metadata"`           // 元数据
	PrimaryKey bool              `json:"pk,omitempty"`       // 是否需要主键
	Internal   bool              `json:"internal,omitempty"` // 是否内部rpc
}

type Module

type Module interface {
	Init() error
	AfterInit()
	BeforeShutdown()
	Shutdown() error
}

Module is the interface that represent a module.

type Node

type Node struct {
	Id       string            `json:"id"`
	Version  Version           `json:"version"` // 节点版本号
	Max      *Version          `json:"max"`     // 节点版本兼容上限
	Min      *Version          `json:"min"`     // 节点版本兼容下限
	Address  string            `json:"address"`
	Metadata map[string]string `json:"metadata"`
}

type PreExecuteHook

type PreExecuteHook func(context.Context, url.Values, []byte) (context.Context, error)

type Protocols

type Protocols struct {
	ContentType string // 原始 ContentType
	Accept      string // 原始 Accept
	Reqeust     string // 请求
	Response    string // 返回
}

type Registry

type Registry interface {
	Register(*Service) error
	Deregister(*Service) error
	GetService(service string) ([]*Service, error)
	ListServices() ([]*Service, error)
	Watch(service string) (Watcher, error)
	String() string
}

type Request

type Request interface {
	// primary key
	PrimaryKey() string
	// The host to call
	Host() string
	// The service to call
	Service() string
	// The action to take
	Method() string
	// The endpoint to invoke
	Endpoint() string
	// The content type
	Protocols() *Protocols
	// Query string
	Query() url.Values
	// The unencoded request body
	Body() interface{}
	// service version fileter
	Version() *Version
}

Request is the interface for a synchronous request used by Call or Stream.

type Response

type Response struct {
	Headers map[string]string
	Body    interface{}
}

type Result

type Result struct {
	Service *Service
	Action  string
}

type Service

type Service struct {
	Name      string            `json:"name"`
	Version   int               `json:"version"` // 服务主版本号
	Metadata  map[string]string `json:"metadata"`
	Nodes     []*Node           `json:"nodes"`
	Endpoints []*Endpoint       `json:"endpoints"`
}

type Stream

type Stream interface {
	CloseSend() error
	// Send will encode and send a request
	Send(body []byte) error
	// Recv will decode and read a response
	Recv(string, *Response) error

	// Close closes the stream
	Close() error
}

Stream is the inteface for a bidirectional synchronous stream.

type Target

type Target struct {
	ID        string     // primary key
	Method    string     // http method
	Host      string     // http host
	Service   string     // service
	Endpoint  string     // service endpoint
	Version   *Version   // service version
	Protocols *Protocols // request protocols
	Query     url.Values // request query parameters
}

type Version

type Version struct {
	Major int `json:"major,omitempty"`
	Minor int `json:"minor,omitempty"`
	Patch int `json:"patch,omitempty"`
}

func NewVersion

func NewVersion(v string) (*Version, error)

func (Version) Compare

func (v Version) Compare(version Version, patch ...bool) int

func (Version) Main

func (v Version) Main() string

func (Version) MarshalJSON

func (v Version) MarshalJSON() ([]byte, error)

MarshalJSON Implementing the json.Marshaler interface

func (Version) Version

func (v Version) Version(patch ...bool) string

Version 版本号字符串,参数用于是否输出patch版本

type Watcher

type Watcher interface {
	// Next is a blocking call
	Next() (*Result, error)
	Stop()
}

Directories

Path Synopsis
Package client is an interface for an RPC client
Package client is an interface for an RPC client
Package errors provides a way to return detailed information for an RPC request error.
Package errors provides a way to return detailed information for an RPC request error.
cache
Package cache provides a registry cache
Package cache provides a registry cache
Package transport is an interface for synchronous connection based communication
Package transport is an interface for synchronous connection based communication
aes
app
ast
flock
Package flock implements a thread-safe interface for file locking.
Package flock implements a thread-safe interface for file locking.

Jump to

Keyboard shortcuts

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