microservice

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConfigCenter

type ConfigCenter interface {
	// Get 获取配置
	Get(key string) (string, error)

	// Set 设置配置
	Set(key, value string) error

	// Watch 监听配置变化
	Watch(key string) (<-chan string, error)

	// Delete 删除配置
	Delete(key string) error
}

ConfigCenter 配置中心接口

type LoadBalancer

type LoadBalancer interface {
	Select(instances []*ServiceInstance) *ServiceInstance
}

LoadBalancer 负载均衡器接口

type MemoryConfigCenter

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

MemoryConfigCenter 内存配置中心

func NewMemoryConfigCenter

func NewMemoryConfigCenter() *MemoryConfigCenter

NewMemoryConfigCenter 创建内存配置中心

func (*MemoryConfigCenter) Delete

func (c *MemoryConfigCenter) Delete(key string) error

Delete 删除配置

func (*MemoryConfigCenter) Get

func (c *MemoryConfigCenter) Get(key string) (string, error)

Get 获取配置

func (*MemoryConfigCenter) Set

func (c *MemoryConfigCenter) Set(key, value string) error

Set 设置配置

func (*MemoryConfigCenter) Watch

func (c *MemoryConfigCenter) Watch(key string) (<-chan string, error)

Watch 监听配置变化

type MemoryRegistry

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

MemoryRegistry 内存注册中心(开发用)

func NewMemoryRegistry

func NewMemoryRegistry() *MemoryRegistry

NewMemoryRegistry 创建内存注册中心

func (*MemoryRegistry) Deregister

func (r *MemoryRegistry) Deregister(instanceID string) error

Deregister 注销服务

func (*MemoryRegistry) Discover

func (r *MemoryRegistry) Discover(serviceName string) ([]*ServiceInstance, error)

Discover 发现服务

func (*MemoryRegistry) Heartbeat

func (r *MemoryRegistry) Heartbeat(instanceID string) error

Heartbeat 心跳

func (*MemoryRegistry) Register

func (r *MemoryRegistry) Register(instance *ServiceInstance) error

Register 注册服务

func (*MemoryRegistry) Watch

func (r *MemoryRegistry) Watch(serviceName string) (<-chan []*ServiceInstance, error)

Watch 监听服务变化

type RandomBalancer

type RandomBalancer struct{}

RandomBalancer 随机负载均衡

func NewRandomBalancer

func NewRandomBalancer() *RandomBalancer

NewRandomBalancer 创建随机负载均衡器

func (*RandomBalancer) Select

func (b *RandomBalancer) Select(instances []*ServiceInstance) *ServiceInstance

Select 选择实例

type Registry

type Registry interface {
	// Register 注册服务
	Register(instance *ServiceInstance) error

	// Deregister 注销服务
	Deregister(instanceID string) error

	// Discover 发现服务
	Discover(serviceName string) ([]*ServiceInstance, error)

	// Watch 监听服务变化
	Watch(serviceName string) (<-chan []*ServiceInstance, error)

	// Heartbeat 心跳
	Heartbeat(instanceID string) error
}

Registry 服务注册中心接口

type RoundRobinBalancer

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

RoundRobinBalancer 轮询负载均衡

func NewRoundRobinBalancer

func NewRoundRobinBalancer() *RoundRobinBalancer

NewRoundRobinBalancer 创建轮询负载均衡器

func (*RoundRobinBalancer) Select

func (b *RoundRobinBalancer) Select(instances []*ServiceInstance) *ServiceInstance

Select 选择实例

type ServiceClient

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

ServiceClient 服务客户端

func NewServiceClient

func NewServiceClient(registry Registry, loadBalancer LoadBalancer) *ServiceClient

NewServiceClient 创建服务客户端

func (*ServiceClient) Call

func (c *ServiceClient) Call(ctx context.Context, serviceName string, handler func(instance *ServiceInstance) error) error

Call 调用服务

type ServiceInstance

type ServiceInstance struct {
	ID       string            `json:"id"`
	Name     string            `json:"name"`
	Address  string            `json:"address"`
	Port     int               `json:"port"`
	Metadata map[string]string `json:"metadata"`
	Health   string            `json:"health"` // healthy, unhealthy
	Updated  time.Time         `json:"updated"`
}

ServiceInstance 服务实例

func (*ServiceInstance) MarshalJSON

func (s *ServiceInstance) MarshalJSON() ([]byte, error)

MarshalJSON 序列化为 JSON

Jump to

Keyboard shortcuts

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