feat(service): align with new discover stateless API and add structured config (by AI)
This commit is contained in:
parent
449d8b0ad9
commit
0ff3b47156
14
config.go
14
config.go
@ -6,8 +6,20 @@ type CertSet struct {
|
|||||||
KeyFile string
|
KeyFile string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CallConfig 下游服务调用配置
|
||||||
|
type CallConfig struct {
|
||||||
|
Timeout int // 超时时间 (ms)
|
||||||
|
Token string // 访问凭证
|
||||||
|
Http2 bool // 是否强制使用 HTTP/2 (H2C)
|
||||||
|
SSL bool // 是否使用 HTTPS/WSS
|
||||||
|
}
|
||||||
|
|
||||||
// ServiceConfig 核心服务配置
|
// ServiceConfig 核心服务配置
|
||||||
type ServiceConfig struct {
|
type ServiceConfig struct {
|
||||||
|
App string // 应用名称。优先从环境变量 DISCOVER_APP 获取,若为空则自动通过代码检测。
|
||||||
|
Register string // 发现服务注册中心地址。支持 Redis URL 或 Redis 配置名称。
|
||||||
|
Weight int // 当前节点在发现服务中的权重 (默认 100)
|
||||||
|
Calls map[string]CallConfig // 依赖的下游服务调用配置
|
||||||
Listen string // 监听端口(|隔开多个监听)(,隔开多个选项),例如 80,http|443|443:h2|127.0.0.1:8080,h2c
|
Listen string // 监听端口(|隔开多个监听)(,隔开多个选项),例如 80,http|443|443:h2|127.0.0.1:8080,h2c
|
||||||
SSL map[string]*CertSet // SSL 证书配置,key 为域名
|
SSL map[string]*CertSet // SSL 证书配置,key 为域名
|
||||||
NoLogGets bool // 不记录 GET 请求的日志
|
NoLogGets bool // 不记录 GET 请求的日志
|
||||||
@ -29,7 +41,6 @@ type ServiceConfig struct {
|
|||||||
StatisticTimeInterval int // 统计时间间隔 (ms)
|
StatisticTimeInterval int // 统计时间间隔 (ms)
|
||||||
Fast bool // 是否启用快速模式
|
Fast bool // 是否启用快速模式
|
||||||
MaxUploadSize int64 // 最大上传文件大小 (Bytes)
|
MaxUploadSize int64 // 最大上传文件大小 (Bytes)
|
||||||
IpPrefix string // Discover 服务发现时指定使用的 IP 网段
|
|
||||||
Cpu int // CPU 占用的核数限制
|
Cpu int // CPU 占用的核数限制
|
||||||
Memory int // 内存限制 (MB)
|
Memory int // 内存限制 (MB)
|
||||||
CpuMonitor bool // 记录 CPU 使用情况
|
CpuMonitor bool // 记录 CPU 使用情况
|
||||||
@ -42,7 +53,6 @@ type ServiceConfig struct {
|
|||||||
SessionWithoutCookie bool // Session 禁用 Cookie
|
SessionWithoutCookie bool // Session 禁用 Cookie
|
||||||
DeviceWithoutCookie bool // 设备ID禁用 Cookie
|
DeviceWithoutCookie bool // 设备ID禁用 Cookie
|
||||||
IdServer string // Redis 服务器连接 (用于全局唯一 ID 生成)
|
IdServer string // Redis 服务器连接 (用于全局唯一 ID 生成)
|
||||||
DiscoverApp string // Discover 应用名称。优先从环境变量 DISCOVER_APP 获取,若为空则自动通过代码检测。
|
|
||||||
KeepKeyCase bool // 是否保持 Key 的首字母大小写
|
KeepKeyCase bool // 是否保持 Key 的首字母大小写
|
||||||
IndexFiles []string // 静态文件索引文件
|
IndexFiles []string // 静态文件索引文件
|
||||||
IndexDir bool // 访问目录时显示文件列表
|
IndexDir bool // 访问目录时显示文件列表
|
||||||
|
|||||||
@ -32,7 +32,7 @@ func TestGetServerIp(t *testing.T) {
|
|||||||
func TestSmartStartup(t *testing.T) {
|
func TestSmartStartup(t *testing.T) {
|
||||||
// Reset config
|
// Reset config
|
||||||
Config.Listen = ""
|
Config.Listen = ""
|
||||||
Config.DiscoverApp = "smart-test"
|
Config.App = "smart-test"
|
||||||
|
|
||||||
as := AsyncStart()
|
as := AsyncStart()
|
||||||
if as.Addr == "" {
|
if as.Addr == "" {
|
||||||
|
|||||||
50
server.go
50
server.go
@ -1,9 +1,9 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"apigo.cc/go/config"
|
|
||||||
"apigo.cc/go/discover"
|
"apigo.cc/go/discover"
|
||||||
"apigo.cc/go/log"
|
"apigo.cc/go/log"
|
||||||
|
"apigo.cc/go/safe"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
@ -70,11 +70,11 @@ func (as *AsyncServer) start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否需要启动服务发现
|
// 检查是否需要启动服务发现
|
||||||
appName := Config.DiscoverApp
|
appName := Config.App
|
||||||
if appName == "" {
|
if appName == "" {
|
||||||
appName = GetDefaultName()
|
appName = GetDefaultName()
|
||||||
}
|
}
|
||||||
if appName != "" {
|
if appName != "" || Config.Register != "" {
|
||||||
as.useDiscover = true
|
as.useDiscover = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,26 +105,48 @@ func (as *AsyncServer) start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 启动服务发现
|
// 启动服务发现
|
||||||
if as.useDiscover && appName != "" {
|
if as.useDiscover {
|
||||||
_, port, _ := net.SplitHostPort(as.Addr)
|
_, port, _ := net.SplitHostPort(as.Addr)
|
||||||
ip := GetServerIp()
|
ip := GetServerIp()
|
||||||
discoverAddr := fmt.Sprintf("%s:%s", ip, port)
|
discoverAddr := fmt.Sprintf("%s:%s", ip, port)
|
||||||
|
|
||||||
// 从 discover.yml/json 加载基础结构,但不包含敏感的 SafeBuf
|
// 转换配置
|
||||||
// 此处不再强行注入 discover.Config 的 Calls,而是留给具体业务手动注入,或者后续提供安全的反序列化机制
|
discConf := discover.Config{
|
||||||
var discConf discover.Config
|
Weight: Config.Weight,
|
||||||
_ = config.Load(&discConf, "discover")
|
CallRetryTimes: 10, // Default
|
||||||
|
Calls: make(map[string]discover.CallConfig),
|
||||||
// Logger 继承
|
}
|
||||||
logger := log.DefaultLogger
|
|
||||||
|
|
||||||
// 解析必需的 Registry,支持环境变量 fallback
|
if discConf.Weight <= 0 {
|
||||||
registry := os.Getenv("DISCOVER_REGISTRY")
|
discConf.Weight = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, call := range Config.Calls {
|
||||||
|
dc := discover.CallConfig{
|
||||||
|
Http2: call.Http2,
|
||||||
|
SSL: call.SSL,
|
||||||
|
}
|
||||||
|
if call.Timeout > 0 {
|
||||||
|
dc.Timeout = time.Duration(call.Timeout) * time.Millisecond
|
||||||
|
} else if Config.RedirectTimeout > 0 {
|
||||||
|
dc.Timeout = time.Duration(Config.RedirectTimeout) * time.Millisecond
|
||||||
|
}
|
||||||
|
if call.Token != "" {
|
||||||
|
dc.Token = safe.NewSafeBuf([]byte(call.Token))
|
||||||
|
}
|
||||||
|
discConf.Calls[name] = dc
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析必需的 Register,支持环境变量 fallback
|
||||||
|
registry := Config.Register
|
||||||
|
if registry == "" {
|
||||||
|
registry = os.Getenv("DISCOVER_REGISTRY")
|
||||||
|
}
|
||||||
if registry == "" {
|
if registry == "" {
|
||||||
registry = "127.0.0.1:6379::15" // Default fallback
|
registry = "127.0.0.1:6379::15" // Default fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
as.discoverer = discover.Start(registry, appName, discoverAddr, logger, discConf)
|
as.discoverer = discover.Start(registry, appName, discoverAddr, log.DefaultLogger, discConf)
|
||||||
GlobalDiscoverer = as.discoverer
|
GlobalDiscoverer = as.discoverer
|
||||||
if as.discoverer != nil {
|
if as.discoverer != nil {
|
||||||
log.DefaultLogger.Info("discover registered", "app", appName, "addr", discoverAddr)
|
log.DefaultLogger.Info("discover registered", "app", appName, "addr", discoverAddr)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user