From 0ca8a0ccbb149c65648071c32d4047e0c37df4b8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-V763RJ7\\Administrator" <835606593@qq.com> Date: Sat, 7 Feb 2026 13:13:02 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=9F=BA=E7=A1=80=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E6=8B=86=E5=88=86=E4=B8=BA=E9=9B=AA=E8=8A=B1=E5=92=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/etcd/client.go | 4 +++ module/base.go | 22 --------------- module/log.go | 17 ++++++++++++ module/prometheus.go | 8 +++--- module/snowflake.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ module/tracer.go | 4 +-- 6 files changed, 91 insertions(+), 28 deletions(-) delete mode 100644 module/base.go create mode 100644 module/log.go create mode 100644 module/snowflake.go diff --git a/db/etcd/client.go b/db/etcd/client.go index e6c4ca1..3822a81 100644 --- a/db/etcd/client.go +++ b/db/etcd/client.go @@ -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) diff --git a/module/base.go b/module/base.go deleted file mode 100644 index eb053af..0000000 --- a/module/base.go +++ /dev/null @@ -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 -} diff --git a/module/log.go b/module/log.go new file mode 100644 index 0000000..35700f9 --- /dev/null +++ b/module/log.go @@ -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 +} diff --git a/module/prometheus.go b/module/prometheus.go index 02650b8..684451d 100644 --- a/module/prometheus.go +++ b/module/prometheus.go @@ -14,9 +14,9 @@ import ( // Prometheus 普罗米修斯模块 type Prometheus struct { DefaultModule - MetricCfg *config.MetricConfig - wg *sync.WaitGroup - server *http.Server + Cfg *config.MetricConfig + wg *sync.WaitGroup + server *http.Server } func (m *Prometheus) Init() error { @@ -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() diff --git a/module/snowflake.go b/module/snowflake.go new file mode 100644 index 0000000..a2d0a71 --- /dev/null +++ b/module/snowflake.go @@ -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") +} diff --git a/module/tracer.go b/module/tracer.go index 1a2edf9..5d3f449 100644 --- a/module/tracer.go +++ b/module/tracer.go @@ -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