feat 基础模块拆分为雪花和日志模块
This commit is contained in:
@@ -37,6 +37,10 @@ func GetClient() *Client {
|
|||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) Raw() *clientv3.Client {
|
||||||
|
return c.cli
|
||||||
|
}
|
||||||
|
|
||||||
// Get 获取数据
|
// Get 获取数据
|
||||||
func (c *Client) Get(key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
|
func (c *Client) Get(key string, opts ...clientv3.OpOption) (*clientv3.GetResponse, error) {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
|||||||
@@ -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,7 +14,7 @@ import (
|
|||||||
// Prometheus 普罗米修斯模块
|
// Prometheus 普罗米修斯模块
|
||||||
type Prometheus struct {
|
type Prometheus struct {
|
||||||
DefaultModule
|
DefaultModule
|
||||||
MetricCfg *config.MetricConfig
|
Cfg *config.MetricConfig
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
server *http.Server
|
server *http.Server
|
||||||
}
|
}
|
||||||
@@ -29,7 +29,7 @@ func (m *Prometheus) Start(ready *sync.WaitGroup) error {
|
|||||||
go func() {
|
go func() {
|
||||||
defer m.wg.Done()
|
defer m.wg.Done()
|
||||||
m.server = &http.Server{
|
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(),
|
Handler: promhttp.Handler(),
|
||||||
}
|
}
|
||||||
ready.Done()
|
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 链路追踪模块
|
// Tracer 链路追踪模块
|
||||||
type Tracer struct {
|
type Tracer struct {
|
||||||
DefaultModule
|
DefaultModule
|
||||||
MetricCfg *config.MetricConfig
|
Cfg *config.MetricConfig
|
||||||
ServiceName string
|
ServiceName string
|
||||||
tp *sdktrace.TracerProvider
|
tp *sdktrace.TracerProvider
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ func (m *Tracer) Init() error {
|
|||||||
exporter, err := otlptracegrpc.New(
|
exporter, err := otlptracegrpc.New(
|
||||||
context.Background(),
|
context.Background(),
|
||||||
otlptracegrpc.WithInsecure(),
|
otlptracegrpc.WithInsecure(),
|
||||||
otlptracegrpc.WithEndpoint(m.MetricCfg.Jaeger.Endpoint),
|
otlptracegrpc.WithEndpoint(m.Cfg.Jaeger.Endpoint),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user