108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
package config
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"hyapp/pkg/configx"
|
|
"hyapp/pkg/logx"
|
|
)
|
|
|
|
// Config 描述 activity-service 启动配置。
|
|
type Config struct {
|
|
ServiceName string `yaml:"service_name"`
|
|
NodeID string `yaml:"node_id"`
|
|
Environment string `yaml:"environment"`
|
|
GRPCAddr string `yaml:"grpc_addr"`
|
|
// MySQLDSN 是活动状态、事件消费幂等和 outbox 的必需存储连接串。
|
|
MySQLDSN string `yaml:"mysql_dsn"`
|
|
// UserServiceAddr 是 fanout worker 读取目标用户游标的内部 gRPC 地址。
|
|
UserServiceAddr string `yaml:"user_service_addr"`
|
|
// MySQLAutoMigrate 保留给显式本地迁移;线上 schema 由发布流程或 initdb 管理。
|
|
MySQLAutoMigrate bool `yaml:"mysql_auto_migrate"`
|
|
Consumer ConsumerConfig `yaml:"consumer"`
|
|
MessageInbox MessageInboxConfig `yaml:"message_inbox"`
|
|
Log logx.Config `yaml:"log"`
|
|
}
|
|
|
|
// ConsumerConfig 保存 room outbox 消费底座配置。
|
|
type ConsumerConfig struct {
|
|
RoomOutboxPollIntervalMs int64 `yaml:"room_outbox_poll_interval_ms"`
|
|
BatchSize int `yaml:"batch_size"`
|
|
}
|
|
|
|
// MessageInboxConfig 保存 App system/activity inbox 的缓存和 fanout worker 参数。
|
|
type MessageInboxConfig struct {
|
|
UnreadCacheTTL string `yaml:"unread_cache_ttl"`
|
|
FanoutWorker MessageFanoutWorkerConfig `yaml:"fanout_worker"`
|
|
}
|
|
|
|
// MessageFanoutWorkerConfig 描述后台消息 fanout worker 的 claim、批量和重试策略。
|
|
type MessageFanoutWorkerConfig struct {
|
|
Enabled bool `yaml:"enabled"`
|
|
PollInterval string `yaml:"poll_interval"`
|
|
BatchSize int `yaml:"batch_size"`
|
|
LockTTL string `yaml:"lock_ttl"`
|
|
MaxRetry int `yaml:"max_retry"`
|
|
}
|
|
|
|
// Default 返回本地默认配置。
|
|
func Default() Config {
|
|
return Config{
|
|
ServiceName: "activity-service",
|
|
NodeID: "activity-local",
|
|
Environment: "local",
|
|
GRPCAddr: ":13006",
|
|
MySQLDSN: "hyapp:hyapp@tcp(127.0.0.1:23306)/hyapp_activity?parseTime=true&charset=utf8mb4&loc=Local",
|
|
UserServiceAddr: "127.0.0.1:13005",
|
|
MySQLAutoMigrate: false,
|
|
Consumer: ConsumerConfig{
|
|
RoomOutboxPollIntervalMs: 1000,
|
|
BatchSize: 100,
|
|
},
|
|
MessageInbox: MessageInboxConfig{
|
|
UnreadCacheTTL: "60s",
|
|
FanoutWorker: MessageFanoutWorkerConfig{
|
|
Enabled: true,
|
|
PollInterval: "1s",
|
|
BatchSize: 500,
|
|
LockTTL: "30s",
|
|
MaxRetry: 8,
|
|
},
|
|
},
|
|
Log: logx.Config{
|
|
Level: "info",
|
|
Format: "json",
|
|
MaxPayloadBytes: 2048,
|
|
},
|
|
}
|
|
}
|
|
|
|
// Load 从独立 config.yaml 读取 activity-service 配置。
|
|
func Load(path string) (Config, error) {
|
|
cfg := Default()
|
|
if path == "" {
|
|
return cfg, nil
|
|
}
|
|
|
|
if err := configx.LoadYAML(path, &cfg); err != nil {
|
|
return Config{}, err
|
|
}
|
|
if strings.TrimSpace(cfg.UserServiceAddr) == "" {
|
|
cfg.UserServiceAddr = Default().UserServiceAddr
|
|
}
|
|
cfg.ServiceName = strings.TrimSpace(cfg.ServiceName)
|
|
if cfg.ServiceName == "" {
|
|
cfg.ServiceName = "activity-service"
|
|
}
|
|
cfg.NodeID = strings.TrimSpace(cfg.NodeID)
|
|
if cfg.NodeID == "" {
|
|
cfg.NodeID = cfg.ServiceName
|
|
}
|
|
cfg.Environment = strings.ToLower(strings.TrimSpace(cfg.Environment))
|
|
if cfg.Environment == "" {
|
|
cfg.Environment = "local"
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|