2026-05-05 09:42:15 +08:00
|
|
|
|
package discover
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"net"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"os/signal"
|
|
|
|
|
|
"regexp"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"sync"
|
2026-05-05 14:52:32 +08:00
|
|
|
|
"sync/atomic"
|
2026-05-05 09:42:15 +08:00
|
|
|
|
"syscall"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"apigo.cc/go/cast"
|
2026-05-05 14:52:32 +08:00
|
|
|
|
gohttp "apigo.cc/go/http"
|
2026-05-05 09:42:15 +08:00
|
|
|
|
"apigo.cc/go/id"
|
|
|
|
|
|
"apigo.cc/go/log"
|
|
|
|
|
|
"apigo.cc/go/redis"
|
2026-05-09 21:11:46 +08:00
|
|
|
|
"apigo.cc/go/safe"
|
2026-05-05 09:42:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
// Discoverer 发现服务实例
|
|
|
|
|
|
type Discoverer struct {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
config Config
|
|
|
|
|
|
registry string
|
|
|
|
|
|
app string
|
|
|
|
|
|
|
2026-05-05 09:42:15 +08:00
|
|
|
|
serverRedisPool *redis.Redis
|
|
|
|
|
|
clientRedisPool *redis.Redis
|
|
|
|
|
|
pubsubRedisPool *redis.Redis
|
2026-05-05 14:27:15 +08:00
|
|
|
|
isServer bool
|
|
|
|
|
|
isClient bool
|
2026-05-05 17:34:49 +08:00
|
|
|
|
daemonRunning atomic.Bool
|
|
|
|
|
|
myAddr string
|
|
|
|
|
|
logger *log.Logger
|
2026-05-09 21:11:46 +08:00
|
|
|
|
daemonStopSignal chan struct{}
|
2026-05-05 17:34:49 +08:00
|
|
|
|
daemonDoneSignal chan struct{}
|
|
|
|
|
|
appLock sync.RWMutex
|
2026-05-05 14:27:15 +08:00
|
|
|
|
calls map[string]*callInfoType
|
|
|
|
|
|
appNodes map[string]map[string]*NodeInfo
|
|
|
|
|
|
appSubscribed map[string]bool
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
2026-05-05 14:52:32 +08:00
|
|
|
|
appClientPools map[string]*gohttp.Client
|
|
|
|
|
|
appClientPoolsLock sync.RWMutex
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
settedRoute func(*AppClient, *http.Request)
|
|
|
|
|
|
settedLoadBalancer LoadBalancer
|
|
|
|
|
|
}
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
|
|
|
|
|
type callInfoType struct {
|
|
|
|
|
|
Timeout time.Duration
|
|
|
|
|
|
HttpVersion int
|
2026-05-09 21:11:46 +08:00
|
|
|
|
Token *safe.SafeBuf
|
2026-05-05 09:42:15 +08:00
|
|
|
|
SSL bool
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:52:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// IsServer 返回当前节点是否作为服务端运行
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) IsServer() bool { return d.isServer }
|
2026-05-05 13:59:03 +08:00
|
|
|
|
|
|
|
|
|
|
// IsClient 返回当前节点是否作为客户端运行
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) IsClient() bool { return d.isClient }
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) logError(msg string, extra ...any) {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.logger.Error("Discover: "+msg, append(extra, "app", d.app, "addr", d.myAddr)...)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) logInfo(msg string, extra ...any) {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.logger.Info("Discover: "+msg, append(extra, "app", d.app, "addr", d.myAddr)...)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// SetLogger 设置 Discover 使用的全局 Logger
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) SetLogger(logger *log.Logger) {
|
|
|
|
|
|
d.logger = logger
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
// New 创建一个新的发现服务实例
|
|
|
|
|
|
func New(confs ...Config) *Discoverer {
|
|
|
|
|
|
var conf Config
|
|
|
|
|
|
if len(confs) > 0 {
|
|
|
|
|
|
conf = confs[0]
|
2026-05-09 17:31:39 +08:00
|
|
|
|
}
|
2026-05-05 14:52:32 +08:00
|
|
|
|
if conf.CallRetryTimes <= 0 {
|
|
|
|
|
|
conf.CallRetryTimes = 10
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 14:52:32 +08:00
|
|
|
|
if conf.Weight <= 0 {
|
|
|
|
|
|
conf.Weight = 100
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d := &Discoverer{
|
|
|
|
|
|
config: conf,
|
|
|
|
|
|
calls: make(map[string]*callInfoType),
|
|
|
|
|
|
appNodes: make(map[string]map[string]*NodeInfo),
|
|
|
|
|
|
appSubscribed: make(map[string]bool),
|
|
|
|
|
|
appClientPools: make(map[string]*gohttp.Client),
|
|
|
|
|
|
settedLoadBalancer: &DefaultLoadBalancer{},
|
|
|
|
|
|
daemonStopSignal: make(chan struct{}),
|
|
|
|
|
|
daemonDoneSignal: make(chan struct{}),
|
|
|
|
|
|
}
|
|
|
|
|
|
if conf.Logger != nil {
|
|
|
|
|
|
d.logger = conf.Logger
|
|
|
|
|
|
} else {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.logger = log.New(id.MakeID(12))
|
|
|
|
|
|
}
|
2026-05-09 21:11:46 +08:00
|
|
|
|
return d
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// Start 启动服务发现,指定当前节点的外部访问地址
|
2026-05-09 21:11:46 +08:00
|
|
|
|
func Start(registry, app, addr string, confs ...Config) *Discoverer {
|
|
|
|
|
|
d := New(confs...)
|
|
|
|
|
|
d.registry = registry
|
|
|
|
|
|
d.app = app
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.myAddr = addr
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.isServer = d.app != "" && d.config.Weight > 0
|
|
|
|
|
|
if d.isServer && d.registry != "" {
|
|
|
|
|
|
d.serverRedisPool = redis.GetRedis(d.registry, d.logger)
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if d.serverRedisPool.Error != nil {
|
|
|
|
|
|
d.logError(d.serverRedisPool.Error.Error())
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 注册节点
|
2026-05-09 21:11:46 +08:00
|
|
|
|
if d.serverRedisPool.Do("HSET", d.app, addr, d.config.Weight).Error == nil {
|
|
|
|
|
|
d.serverRedisPool.Do("SETEX", d.app+"_"+addr, 10, "1")
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.logInfo("registered")
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.serverRedisPool.PUBLISH("CH_"+d.app, fmt.Sprintf("%s %d", addr, d.config.Weight))
|
2026-05-05 14:52:32 +08:00
|
|
|
|
d.daemonRunning.Store(true)
|
2026-05-05 17:34:49 +08:00
|
|
|
|
d.daemonStopSignal = make(chan struct{})
|
|
|
|
|
|
d.daemonDoneSignal = make(chan struct{})
|
2026-05-05 14:27:15 +08:00
|
|
|
|
go d.daemon()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
} else {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.logError("register failed")
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
calls := d.config.Calls
|
2026-05-05 09:42:15 +08:00
|
|
|
|
if len(calls) > 0 {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
for callApp, c := range calls {
|
|
|
|
|
|
d.addApp(callApp, c, false)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if !d.startSub() {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
return d
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-09 21:11:46 +08:00
|
|
|
|
return d
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Open 启动服务发现仅作为客户端
|
|
|
|
|
|
func Open(registry string, confs ...Config) *Discoverer {
|
|
|
|
|
|
d := New(confs...)
|
|
|
|
|
|
d.registry = registry
|
|
|
|
|
|
calls := d.config.Calls
|
|
|
|
|
|
if len(calls) > 0 {
|
|
|
|
|
|
for callApp, c := range calls {
|
|
|
|
|
|
d.addApp(callApp, c, false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
d.startSub()
|
|
|
|
|
|
return d
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) daemon() {
|
|
|
|
|
|
d.logInfo("daemon thread started")
|
2026-05-05 13:59:03 +08:00
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
2026-05-05 14:52:32 +08:00
|
|
|
|
for d.daemonRunning.Load() {
|
2026-05-05 17:34:49 +08:00
|
|
|
|
select {
|
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
|
if !d.daemonRunning.Load() {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
2026-05-05 17:34:49 +08:00
|
|
|
|
if d.isServer && d.serverRedisPool != nil {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
if !d.serverRedisPool.Do("HEXISTS", d.app, d.myAddr).Bool() {
|
2026-05-05 17:34:49 +08:00
|
|
|
|
d.logInfo("lost app registered info, re-registering")
|
2026-05-09 21:11:46 +08:00
|
|
|
|
if d.serverRedisPool.Do("HSET", d.app, d.myAddr, d.config.Weight).Error == nil {
|
|
|
|
|
|
d.serverRedisPool.Do("SETEX", d.app+"_"+d.myAddr, 10, "1")
|
|
|
|
|
|
d.serverRedisPool.PUBLISH("CH_"+d.app, fmt.Sprintf("%s %d", d.myAddr, d.config.Weight))
|
2026-05-05 17:34:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.serverRedisPool.Do("SETEX", d.app+"_"+d.myAddr, 10, "1")
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-05 17:34:49 +08:00
|
|
|
|
case <-d.daemonStopSignal:
|
|
|
|
|
|
goto done
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-05 17:34:49 +08:00
|
|
|
|
done:
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.logInfo("daemon thread stopped")
|
2026-05-05 17:34:49 +08:00
|
|
|
|
close(d.daemonDoneSignal)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) startSub() bool {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
if d.registry == "" {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appLock.Lock()
|
|
|
|
|
|
if d.clientRedisPool == nil {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.clientRedisPool = redis.GetRedis(d.registry, d.logger)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if d.pubsubRedisPool == nil {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.pubsubRedisPool = redis.GetRedis(d.registry, d.logger.New(id.MakeID(12)))
|
2026-05-05 09:42:15 +08:00
|
|
|
|
// 订阅所有已注册的应用
|
2026-05-05 14:27:15 +08:00
|
|
|
|
for app := range d.appSubscribed {
|
2026-05-05 17:34:49 +08:00
|
|
|
|
d.subscribeApp(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 必须在释放锁之前完成配置,但在释放锁之后启动,避免死锁
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appLock.Unlock()
|
|
|
|
|
|
d.pubsubRedisPool.Start()
|
|
|
|
|
|
d.appLock.Lock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.isClient = true
|
|
|
|
|
|
d.appLock.Unlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 17:34:49 +08:00
|
|
|
|
func (d *Discoverer) subscribeApp(app string) {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
if d.pubsubRedisPool == nil {
|
|
|
|
|
|
d.appSubscribed[app] = true
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.pubsubRedisPool.Subscribe("CH_"+app, func() {
|
|
|
|
|
|
d.fetchApp(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}, func(data []byte) {
|
|
|
|
|
|
a := strings.Split(string(data), " ")
|
|
|
|
|
|
addr := a[0]
|
|
|
|
|
|
weight := 0
|
|
|
|
|
|
if len(a) == 2 {
|
|
|
|
|
|
weight = cast.Int(a[1])
|
|
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.logInfo("received node update", "app", app, "addr", addr, "weight", weight)
|
|
|
|
|
|
d.pushNode(app, addr, weight)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
func (d *Discoverer) subscribeAppWithLock(app string) {
|
|
|
|
|
|
d.appLock.Lock()
|
|
|
|
|
|
defer d.appLock.Unlock()
|
|
|
|
|
|
d.subscribeApp(app)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// Stop 停止 Discover 并从注册中心注销当前节点
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) Stop() {
|
|
|
|
|
|
d.appLock.Lock()
|
|
|
|
|
|
|
2026-05-05 17:34:49 +08:00
|
|
|
|
// 1. 提取需要的状态,提前修改标志位
|
|
|
|
|
|
isClient := d.isClient
|
|
|
|
|
|
pubsub := d.pubsubRedisPool
|
|
|
|
|
|
d.isClient = false
|
|
|
|
|
|
|
|
|
|
|
|
isServer := d.isServer
|
|
|
|
|
|
serverPool := d.serverRedisPool
|
|
|
|
|
|
myAddr := d.myAddr
|
|
|
|
|
|
|
|
|
|
|
|
if isServer {
|
2026-05-05 14:52:32 +08:00
|
|
|
|
d.daemonRunning.Store(false)
|
2026-05-05 17:34:49 +08:00
|
|
|
|
if d.daemonStopSignal != nil {
|
|
|
|
|
|
close(d.daemonStopSignal)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.isServer = false
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 17:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
// 核心修复:在这里尽早释放锁!避免与 pushNode 回调发生死锁
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appLock.Unlock()
|
2026-05-05 14:52:32 +08:00
|
|
|
|
|
2026-05-05 17:34:49 +08:00
|
|
|
|
// 2. 在无锁状态下进行耗时的网络和停止操作
|
|
|
|
|
|
if isClient && pubsub != nil {
|
|
|
|
|
|
pubsub.Stop()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if isServer && serverPool != nil {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
serverPool.Do("HDEL", d.app, myAddr)
|
|
|
|
|
|
serverPool.Do("DEL", d.app+"_"+myAddr)
|
|
|
|
|
|
serverPool.PUBLISH("CH_"+d.app, fmt.Sprintf("%s %d", myAddr, 0))
|
2026-05-05 17:34:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 释放 HTTP 连接池
|
2026-05-05 14:52:32 +08:00
|
|
|
|
d.appClientPoolsLock.Lock()
|
|
|
|
|
|
for _, client := range d.appClientPools {
|
|
|
|
|
|
client.Destroy()
|
|
|
|
|
|
}
|
|
|
|
|
|
d.appClientPools = make(map[string]*gohttp.Client)
|
|
|
|
|
|
d.appClientPoolsLock.Unlock()
|
2026-05-09 21:11:46 +08:00
|
|
|
|
|
|
|
|
|
|
// 4. 重置状态以支持重新启动
|
|
|
|
|
|
d.appLock.Lock()
|
|
|
|
|
|
d.serverRedisPool = nil
|
|
|
|
|
|
d.clientRedisPool = nil
|
|
|
|
|
|
d.pubsubRedisPool = nil
|
|
|
|
|
|
d.appNodes = make(map[string]map[string]*NodeInfo)
|
|
|
|
|
|
d.appSubscribed = make(map[string]bool)
|
|
|
|
|
|
d.appLock.Unlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// Wait 等待守护进程退出
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) Wait() {
|
2026-05-05 17:34:49 +08:00
|
|
|
|
if d.daemonDoneSignal != nil {
|
|
|
|
|
|
<-d.daemonDoneSignal
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
// EasyStart 自动根据本地网卡信息启动 Discover
|
|
|
|
|
|
func EasyStart(registry, app string, confs ...Config) (*Discoverer, string, int) {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
port := 0
|
|
|
|
|
|
if listen := os.Getenv("DISCOVER_LISTEN"); listen != "" {
|
|
|
|
|
|
if _, p, err := net.SplitHostPort(listen); err == nil {
|
|
|
|
|
|
port = cast.Int(p)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
port = cast.Int(listen)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
|
|
if err != nil {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
return nil, "", 0
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
addrInfo := ln.Addr().(*net.TCPAddr)
|
|
|
|
|
|
_ = ln.Close()
|
|
|
|
|
|
port = addrInfo.Port
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
var conf Config
|
|
|
|
|
|
if len(confs) > 0 {
|
|
|
|
|
|
conf = confs[0]
|
|
|
|
|
|
}
|
2026-05-05 09:42:15 +08:00
|
|
|
|
ip := addrInfo.IP
|
|
|
|
|
|
if !ip.IsGlobalUnicast() {
|
|
|
|
|
|
addrs, _ := net.InterfaceAddrs()
|
|
|
|
|
|
for _, a := range addrs {
|
|
|
|
|
|
if an, ok := a.(*net.IPNet); ok {
|
|
|
|
|
|
ip4 := an.IP.To4()
|
|
|
|
|
|
if ip4 == nil || !ip4.IsGlobalUnicast() {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
2026-05-05 14:52:32 +08:00
|
|
|
|
if conf.IpPrefix != "" && strings.HasPrefix(ip4.String(), conf.IpPrefix) {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
ip = ip4
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
if !strings.HasPrefix(ip4.String(), "172.17.") {
|
|
|
|
|
|
ip = ip4
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", ip.String(), port)
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d := Start(registry, app, addr, conf)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
go func() {
|
|
|
|
|
|
<-sigChan
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.Stop()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}()
|
|
|
|
|
|
|
2026-05-09 21:11:46 +08:00
|
|
|
|
return d, ip.String(), port
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// AddExternalApp 动态添加需要发现的外部应用
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) AddExternalApp(app, callConf string) bool {
|
|
|
|
|
|
if d.addApp(app, callConf, true) {
|
|
|
|
|
|
if !d.isClient {
|
|
|
|
|
|
d.startSub()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
} else {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.subscribeAppWithLock(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 17:34:49 +08:00
|
|
|
|
d.fetchApp(app) // 同步拉取一次
|
2026-05-05 09:42:15 +08:00
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
// SetNode 手动设置某个服务的节点信息
|
|
|
|
|
|
func (d *Discoverer) SetNode(app, addr string, weight int) {
|
|
|
|
|
|
d.pushNode(app, addr, weight)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) getCallInfo(app string) *callInfoType {
|
|
|
|
|
|
d.appLock.RLock()
|
|
|
|
|
|
defer d.appLock.RUnlock()
|
|
|
|
|
|
return d.calls[app]
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var numberMatcher = regexp.MustCompile(`^\d+(s|ms|us|µs|ns?)?$`)
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) addApp(app, callConf string, fetch bool) bool {
|
|
|
|
|
|
d.appLock.Lock()
|
2026-05-05 15:03:12 +08:00
|
|
|
|
|
|
|
|
|
|
// 1. 写时复制(Copy-on-Write):创建一个全新的 Map 避免影响读操作
|
|
|
|
|
|
newCalls := make(map[string]string)
|
2026-05-09 21:11:46 +08:00
|
|
|
|
for k, v := range d.config.Calls {
|
2026-05-05 15:03:12 +08:00
|
|
|
|
newCalls[k] = v
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 15:03:12 +08:00
|
|
|
|
|
|
|
|
|
|
if newCalls[app] == callConf && d.appNodes[app] != nil {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appLock.Unlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-05-05 15:03:12 +08:00
|
|
|
|
|
|
|
|
|
|
newCalls[app] = callConf
|
2026-05-09 21:11:46 +08:00
|
|
|
|
d.config.Calls = newCalls // 将新的 Map 赋值给 Config
|
2026-05-05 15:03:12 +08:00
|
|
|
|
|
2026-05-05 09:42:15 +08:00
|
|
|
|
callInfo := &callInfoType{
|
2026-05-09 21:11:46 +08:00
|
|
|
|
|
2026-05-05 09:42:15 +08:00
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
|
|
HttpVersion: 2,
|
|
|
|
|
|
SSL: false,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, v := range cast.Split(callConf, ":") {
|
|
|
|
|
|
switch v {
|
|
|
|
|
|
case "1":
|
|
|
|
|
|
callInfo.HttpVersion = 1
|
|
|
|
|
|
case "2":
|
|
|
|
|
|
callInfo.HttpVersion = 2
|
|
|
|
|
|
case "s", "https":
|
|
|
|
|
|
callInfo.SSL = true
|
|
|
|
|
|
callInfo.HttpVersion = 2
|
|
|
|
|
|
case "http":
|
|
|
|
|
|
callInfo.SSL = false
|
|
|
|
|
|
callInfo.HttpVersion = 1
|
|
|
|
|
|
case "h2c":
|
|
|
|
|
|
callInfo.SSL = false
|
|
|
|
|
|
callInfo.HttpVersion = 2
|
|
|
|
|
|
default:
|
|
|
|
|
|
if numberMatcher.MatchString(v) {
|
|
|
|
|
|
callInfo.Timeout = cast.Duration(v)
|
|
|
|
|
|
} else {
|
2026-05-09 21:11:46 +08:00
|
|
|
|
callInfo.Token = safe.NewSafeBuf([]byte(v))
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.calls[app] = callInfo
|
|
|
|
|
|
if d.appNodes[app] == nil {
|
|
|
|
|
|
d.appNodes[app] = make(map[string]*NodeInfo)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appSubscribed[app] = true
|
|
|
|
|
|
d.appLock.Unlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if fetch && d.isClient {
|
|
|
|
|
|
d.fetchApp(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) fetchApp(app string) {
|
|
|
|
|
|
d.appLock.RLock()
|
|
|
|
|
|
pool := d.clientRedisPool
|
|
|
|
|
|
d.appLock.RUnlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
if pool == nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
results := pool.Do("HGETALL", app).ResultMap()
|
2026-05-05 14:27:15 +08:00
|
|
|
|
|
2026-05-05 09:42:15 +08:00
|
|
|
|
// 检查存活
|
|
|
|
|
|
for addr := range results {
|
|
|
|
|
|
if !pool.Do("EXISTS", app+"_"+addr).Bool() {
|
|
|
|
|
|
pool.Do("HDEL", app, addr)
|
|
|
|
|
|
delete(results, addr)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
currentNodes := d.getAppNodes(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
if currentNodes != nil {
|
|
|
|
|
|
for addr := range currentNodes {
|
|
|
|
|
|
if _, ok := results[addr]; !ok {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.pushNode(app, addr, 0)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for addr, res := range results {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.pushNode(app, addr, res.Int())
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) getAppNodes(app string) map[string]*NodeInfo {
|
|
|
|
|
|
d.appLock.RLock()
|
|
|
|
|
|
defer d.appLock.RUnlock()
|
|
|
|
|
|
if d.appNodes[app] == nil {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
nodes := make(map[string]*NodeInfo)
|
2026-05-05 14:27:15 +08:00
|
|
|
|
for k, v := range d.appNodes[app] {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
nodes[k] = v
|
|
|
|
|
|
}
|
|
|
|
|
|
return nodes
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) getCalls() map[string]string {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
calls := make(map[string]string)
|
2026-05-09 21:11:46 +08:00
|
|
|
|
for k, v := range d.config.Calls {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
calls[k] = v
|
|
|
|
|
|
}
|
|
|
|
|
|
return calls
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 13:59:03 +08:00
|
|
|
|
// GetAppNodes 获取某个应用的所有节点列表
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) GetAppNodes(app string) map[string]*NodeInfo {
|
|
|
|
|
|
return d.getAppNodes(app)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
func (d *Discoverer) pushNode(app, addr string, weight int) {
|
|
|
|
|
|
d.appLock.Lock()
|
|
|
|
|
|
defer d.appLock.Unlock()
|
2026-05-05 09:42:15 +08:00
|
|
|
|
|
|
|
|
|
|
if weight <= 0 {
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if d.appNodes[app] != nil {
|
|
|
|
|
|
delete(d.appNodes[app], addr)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if d.appNodes[app] == nil {
|
|
|
|
|
|
d.appNodes[app] = make(map[string]*NodeInfo)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if node, ok := d.appNodes[app][addr]; ok {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
if node.Weight != weight {
|
2026-05-05 13:59:03 +08:00
|
|
|
|
used := node.UsedTimes.Load()
|
|
|
|
|
|
node.UsedTimes.Store(uint64(float64(used) / float64(node.Weight) * float64(weight)))
|
2026-05-05 09:42:15 +08:00
|
|
|
|
node.Weight = weight
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
var avgUsed uint64 = 0
|
2026-05-05 14:27:15 +08:00
|
|
|
|
if len(d.appNodes[app]) > 0 {
|
2026-05-05 09:42:15 +08:00
|
|
|
|
var totalScore float64
|
2026-05-05 14:27:15 +08:00
|
|
|
|
for _, n := range d.appNodes[app] {
|
2026-05-05 13:59:03 +08:00
|
|
|
|
totalScore += float64(n.UsedTimes.Load()) / float64(n.Weight)
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
avgUsed = uint64(totalScore / float64(len(d.appNodes[app])) * float64(weight))
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 13:59:03 +08:00
|
|
|
|
node := &NodeInfo{
|
|
|
|
|
|
Addr: addr,
|
|
|
|
|
|
Weight: weight,
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
2026-05-05 13:59:03 +08:00
|
|
|
|
node.UsedTimes.Store(avgUsed)
|
2026-05-05 14:27:15 +08:00
|
|
|
|
d.appNodes[app][addr] = node
|
2026-05-05 09:42:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-05-05 14:27:15 +08:00
|
|
|
|
|