feat 基础模块拆分为雪花和日志模块
This commit is contained in:
@@ -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
17
module/log.go
Normal 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
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
64
module/snowflake.go
Normal file
64
module/snowflake.go
Normal 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")
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user