42 lines
806 B
Go
42 lines
806 B
Go
package rabbitmq
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/gogf/gf/v2/util/guid"
|
|
)
|
|
|
|
var (
|
|
instanceId string
|
|
instanceOnce sync.Once
|
|
)
|
|
|
|
// getInstanceId 获取当前实例的唯一标识(单例)
|
|
// 格式:{hostname}.{uuid8}
|
|
func getInstanceId() string {
|
|
instanceOnce.Do(func() {
|
|
// 获取主机名
|
|
hostname, err := os.Hostname()
|
|
if err != nil || hostname == "" {
|
|
hostname = "unknown"
|
|
}
|
|
|
|
// 生成8位UUID
|
|
uuid := guid.S()[:8]
|
|
|
|
instanceId = fmt.Sprintf("%s.%s", hostname, uuid)
|
|
})
|
|
return instanceId
|
|
}
|
|
|
|
// GetInstanceQueueName 获取当前实例的响应队列名
|
|
// 格式:{baseQueue}.{hostname}.{uuid8}
|
|
func GetInstanceQueueName(baseQueue string) string {
|
|
if baseQueue == "" {
|
|
baseQueue = "ragflow.response"
|
|
}
|
|
return fmt.Sprintf("%s.%s", baseQueue, getInstanceId())
|
|
}
|