feat 基础模块拆分为雪花和日志模块

This commit is contained in:
2026-02-07 13:13:02 +08:00
parent 5a0f4b71d4
commit 0ca8a0ccbb
6 changed files with 91 additions and 28 deletions

View File

@@ -37,6 +37,10 @@ func GetClient() *Client {
return instance
}
func (c *Client) Raw() *clientv3.Client {
return c.cli
}
// Get 获取数据
func (c *Client) Get(key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)

View File

@@ -1,22 +0,0 @@
package module
import (
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/utils"
"math/rand"
)
// Base 基础模块
type Base struct {
DefaultModule
Log *config.LogConfig
}
func (m *Base) Init() error {
// 日志
log.Init(m.Log)
// 雪花
utils.InitSnowflake(int64(rand.Intn(1000)))
return nil
}

17
module/log.go Normal file
View File

@@ -0,0 +1,17 @@
package module
import (
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/log"
)
// Log 日志模块
type Log struct {
DefaultModule
Cfg *config.LogConfig
}
func (m *Log) Init() error {
log.Init(m.Cfg)
return nil
}

View File

@@ -14,7 +14,7 @@ import (
// Prometheus 普罗米修斯模块
type Prometheus struct {
DefaultModule
MetricCfg *config.MetricConfig
Cfg *config.MetricConfig
wg *sync.WaitGroup
server *http.Server
}
@@ -29,7 +29,7 @@ func (m *Prometheus) Start(ready *sync.WaitGroup) error {
go func() {
defer m.wg.Done()
m.server = &http.Server{
Addr: fmt.Sprintf("%v:%v", m.MetricCfg.Prometheus.Address, m.MetricCfg.Prometheus.Port),
Addr: fmt.Sprintf("%v:%v", m.Cfg.Prometheus.Address, m.Cfg.Prometheus.Port),
Handler: promhttp.Handler(),
}
ready.Done()

64
module/snowflake.go Normal file
View File

@@ -0,0 +1,64 @@
package module
import (
"context"
"errors"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/etcd"
"git.hlsq.asia/mmorpg/service-common/utils"
clientv3 "go.etcd.io/etcd/client/v3"
"go.etcd.io/etcd/client/v3/concurrency"
"math/rand"
)
// Snowflake 雪花模块
type Snowflake struct {
DefaultModule
snowflakeSession *concurrency.Session
}
func (m *Snowflake) Init() error {
node, session, err := acquire(context.Background(), 1, 1000)
if err != nil {
return err
}
m.snowflakeSession = session
utils.InitSnowflake(node)
return nil
}
func (m *Snowflake) Stop() error {
_ = m.snowflakeSession.Close()
return nil
}
func acquire(ctx context.Context, min, max int) (int64, *concurrency.Session, error) {
nums := rand.Perm(max - min + 1)
for i := range nums {
nums[i] += min
}
for _, n := range nums {
key := fmt.Sprintf("node/num/%d", n)
session, err := concurrency.NewSession(
etcd.GetClient().Raw(),
concurrency.WithContext(ctx),
)
if err != nil {
return 0, nil, utils.ErrorsWrap(fmt.Errorf("etcd NewSession error: %v", err))
}
txnResp, _ := etcd.GetClient().Raw().Txn(ctx).
If(clientv3.Compare(clientv3.CreateRevision(key), "=", 0)).
Then(clientv3.OpPut(key, "", clientv3.WithLease(session.Lease()))).
Commit()
if txnResp.Succeeded {
return int64(n), session, nil
} else {
_ = session.Close()
}
}
return 0, nil, utils.ErrorsWrap(errors.New("etcd num empty"), "acquire error")
}

View File

@@ -15,7 +15,7 @@ import (
// Tracer 链路追踪模块
type Tracer struct {
DefaultModule
MetricCfg *config.MetricConfig
Cfg *config.MetricConfig
ServiceName string
tp *sdktrace.TracerProvider
}
@@ -24,7 +24,7 @@ func (m *Tracer) Init() error {
exporter, err := otlptracegrpc.New(
context.Background(),
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(m.MetricCfg.Jaeger.Endpoint),
otlptracegrpc.WithEndpoint(m.Cfg.Jaeger.Endpoint),
)
if err != nil {
return err