services

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package services 提供框架内置服务的实现 所有服务都实现了 core.Service 接口,支持自动依赖解析和并行初始化

内置服务列表:

  • LoggerService: 日志服务(优先级0,无依赖)
  • RedisService: Redis缓存服务(优先级10,依赖logger)
  • MySQLService: MySQL数据库服务(优先级10,依赖logger)
  • ElasticsearchService: Elasticsearch搜索服务(优先级20,依赖logger)
  • RabbitMQService: RabbitMQ消息队列服务(优先级30,依赖logger)
  • EtcdService: Etcd配置中心服务(优先级20,依赖logger)
  • ScheduleService: 定时任务服务(优先级100,依赖logger)

使用示例:

// 注册自定义服务
core.RegisterService(&MyCustomService{})

// 注册初始化钩子
core.RegisterServiceHook("mysql", core.Hook{
    Phase: core.AfterInit,
    Fn: func(ctx context.Context, name string) error {
        // 执行数据迁移
        return nil
    },
})

Package services 提供框架内置服务的实现 本文件实现了 OpenTelemetry 链路追踪服务

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ElasticsearchService

type ElasticsearchService struct{}

ElasticsearchService Elasticsearch服务

func (*ElasticsearchService) Close

func (s *ElasticsearchService) Close(ctx context.Context) error

Close 关闭Elasticsearch连接

func (*ElasticsearchService) Dependencies

func (s *ElasticsearchService) Dependencies() []string

Dependencies 返回依赖

func (*ElasticsearchService) HealthCheck

func (s *ElasticsearchService) HealthCheck(ctx context.Context) error

HealthCheck 健康检查

func (*ElasticsearchService) Init

Init 初始化Elasticsearch

func (*ElasticsearchService) Name

func (s *ElasticsearchService) Name() string

Name 返回服务名称

func (*ElasticsearchService) Priority

func (s *ElasticsearchService) Priority() int

Priority 返回初始化优先级

func (*ElasticsearchService) ShouldInit

func (s *ElasticsearchService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type EtcdService

type EtcdService struct{}

EtcdService Etcd服务

func (*EtcdService) Close

func (s *EtcdService) Close(ctx context.Context) error

Close 关闭Etcd连接

func (*EtcdService) Dependencies

func (s *EtcdService) Dependencies() []string

Dependencies 返回依赖

func (*EtcdService) HealthCheck

func (s *EtcdService) HealthCheck(ctx context.Context) error

HealthCheck 健康检查

func (*EtcdService) Init

func (s *EtcdService) Init(ctx context.Context) error

Init 初始化Etcd

func (*EtcdService) Name

func (s *EtcdService) Name() string

Name 返回服务名称

func (*EtcdService) Priority

func (s *EtcdService) Priority() int

Priority 返回初始化优先级

func (*EtcdService) ShouldInit

func (s *EtcdService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type LoggerService

type LoggerService struct{}

LoggerService 日志服务

func (*LoggerService) Close

func (s *LoggerService) Close(ctx context.Context) error

Close 关闭日志(通常不需要)

func (*LoggerService) Dependencies

func (s *LoggerService) Dependencies() []string

Dependencies 返回依赖(日志无依赖)

func (*LoggerService) Init

func (s *LoggerService) Init(ctx context.Context) error

Init 初始化日志

func (*LoggerService) Name

func (s *LoggerService) Name() string

Name 返回服务名称

func (*LoggerService) Priority

func (s *LoggerService) Priority() int

Priority 返回初始化优先级(日志最先初始化)

func (*LoggerService) ShouldInit

func (s *LoggerService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 日志总是需要初始化

type MySQLService

type MySQLService struct{}

MySQLService MySQL数据库服务

func (*MySQLService) Close

func (s *MySQLService) Close(ctx context.Context) error

Close 关闭数据库连接,显式释放所有 sql.DB 底层连接资源

func (*MySQLService) Dependencies

func (s *MySQLService) Dependencies() []string

Dependencies 返回依赖

func (*MySQLService) HealthCheck

func (s *MySQLService) HealthCheck(ctx context.Context) error

HealthCheck 健康检查

func (*MySQLService) Init

func (s *MySQLService) Init(ctx context.Context) error

Init 初始化MySQL

func (*MySQLService) Name

func (s *MySQLService) Name() string

Name 返回服务名称

func (*MySQLService) Priority

func (s *MySQLService) Priority() int

Priority 返回初始化优先级

func (*MySQLService) ShouldInit

func (s *MySQLService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type RabbitMQService

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

RabbitMQService RabbitMQ消息队列服务

func NewRabbitMQService

func NewRabbitMQService(consumerList, producerList []*config.MessageQueue) *RabbitMQService

NewRabbitMQService 创建RabbitMQ服务

func (*RabbitMQService) Close

func (s *RabbitMQService) Close(ctx context.Context) error

Close 关闭RabbitMQ连接

func (*RabbitMQService) Dependencies

func (s *RabbitMQService) Dependencies() []string

Dependencies 返回依赖

func (*RabbitMQService) Init

func (s *RabbitMQService) Init(ctx context.Context) error

Init 初始化RabbitMQ

func (*RabbitMQService) Name

func (s *RabbitMQService) Name() string

Name 返回服务名称

func (*RabbitMQService) Priority

func (s *RabbitMQService) Priority() int

Priority 返回初始化优先级

func (*RabbitMQService) SetConsumerList

func (s *RabbitMQService) SetConsumerList(list []*config.MessageQueue)

SetConsumerList 设置消费者列表

func (*RabbitMQService) SetProducerList

func (s *RabbitMQService) SetProducerList(list []*config.MessageQueue)

SetProducerList 设置生产者列表

func (*RabbitMQService) ShouldInit

func (s *RabbitMQService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type RedisService

type RedisService struct{}

RedisService Redis服务

func (*RedisService) Close

func (s *RedisService) Close(ctx context.Context) error

Close 关闭Redis连接

func (*RedisService) Dependencies

func (s *RedisService) Dependencies() []string

Dependencies 返回依赖

func (*RedisService) HealthCheck

func (s *RedisService) HealthCheck(ctx context.Context) error

HealthCheck 健康检查

func (*RedisService) Init

func (s *RedisService) Init(ctx context.Context) error

Init 初始化Redis

func (*RedisService) Name

func (s *RedisService) Name() string

Name 返回服务名称

func (*RedisService) Priority

func (s *RedisService) Priority() int

Priority 返回初始化优先级

func (*RedisService) ShouldInit

func (s *RedisService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type ScheduleService

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

ScheduleService 定时任务服务

func NewScheduleService

func NewScheduleService(scheduleList []config.ScheduleInfo) *ScheduleService

NewScheduleService 创建定时任务服务

func (*ScheduleService) Close

func (s *ScheduleService) Close(ctx context.Context) error

Close 关闭定时任务

func (*ScheduleService) Dependencies

func (s *ScheduleService) Dependencies() []string

Dependencies 返回依赖

func (*ScheduleService) Init

func (s *ScheduleService) Init(ctx context.Context) error

Init 初始化定时任务

func (*ScheduleService) Name

func (s *ScheduleService) Name() string

Name 返回服务名称

func (*ScheduleService) Priority

func (s *ScheduleService) Priority() int

Priority 返回初始化优先级(最后初始化)

func (*ScheduleService) SetScheduleList

func (s *ScheduleService) SetScheduleList(list []config.ScheduleInfo)

SetScheduleList 设置定时任务列表

func (*ScheduleService) ShouldInit

func (s *ScheduleService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 根据配置判断是否需要初始化

type TracingService

type TracingService struct{}

TracingService 链路追踪服务 实现了 core.Service 接口,用于管理 OpenTelemetry 追踪器的生命周期

func (*TracingService) Close

func (s *TracingService) Close(ctx context.Context) error

Close 关闭链路追踪 确保所有待发送的追踪数据被刷新到采集器

func (*TracingService) Dependencies

func (s *TracingService) Dependencies() []string

Dependencies 返回依赖 追踪服务依赖日志服务,用于记录初始化状态

func (*TracingService) Init

func (s *TracingService) Init(ctx context.Context) error

Init 初始化链路追踪

func (*TracingService) Name

func (s *TracingService) Name() string

Name 返回服务名称

func (*TracingService) Priority

func (s *TracingService) Priority() int

Priority 返回初始化优先级 追踪服务需要在日志之后、其他服务之前初始化 这样可以确保其他服务的初始化过程也能被追踪

func (*TracingService) ShouldInit

func (s *TracingService) ShouldInit(cfg *config.BaseConfig) bool

ShouldInit 判断是否需要初始化 只有在配置了链路追踪且启用时才初始化

Jump to

Keyboard shortcuts

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