Compare commits
6 Commits
feat/instg
...
b1dfb88f71
| Author | SHA1 | Date | |
|---|---|---|---|
| b1dfb88f71 | |||
| a7266f4bfa | |||
| 2ea538995a | |||
| 174cd09ba6 | |||
| 62ebfa17ea | |||
| 8de7ab4a67 |
@@ -22,7 +22,7 @@ func (p *Program) Init(_ svc.Environment) error {
|
||||
return err
|
||||
}
|
||||
p.moduleList = append(p.moduleList, base)
|
||||
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB, common.KeyDiscoverServiceNameGateway))
|
||||
p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB, config.Get().App.Name))
|
||||
p.moduleList = append(p.moduleList, &ModuleWebServer{})
|
||||
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{})
|
||||
p.moduleList = append(p.moduleList, &ModuleLoginQueue{})
|
||||
|
||||
@@ -13,19 +13,19 @@ metric:
|
||||
address: "0.0.0.0"
|
||||
port: 18504
|
||||
jaeger:
|
||||
endpoint: "localhost:4317"
|
||||
endpoint: "127.0.0.1:4317"
|
||||
|
||||
db:
|
||||
etcd:
|
||||
endpoints: [ "localhost:2379" ]
|
||||
endpoints: [ "127.0.0.1:2379" ]
|
||||
redis:
|
||||
addr: "localhost:6379"
|
||||
addr: "127.0.0.1:6379"
|
||||
password: "lQ7aM8oB6lK0iD5k"
|
||||
db: 0
|
||||
|
||||
serve:
|
||||
grpc:
|
||||
address: "10.0.40.199"
|
||||
address: "127.0.0.1"
|
||||
port: 18500
|
||||
ttl: 20
|
||||
socket:
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
FROM alpine:3.23.2
|
||||
|
||||
ENV ARMS_ENABLE=true
|
||||
ENV ARMS_APP_NAME=gateway
|
||||
ENV ARMS_REGION_ID=cn-chengdu
|
||||
ENV ARMS_LICENSE_KEY=gys9jv7jqe@144a6fd684976b3
|
||||
|
||||
RUN apk add --no-cache tzdata && \
|
||||
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
|
||||
echo "Asia/Shanghai" > /etc/timezone
|
||||
|
||||
9
deploy/Jenkinsfile
vendored
9
deploy/Jenkinsfile
vendored
@@ -19,7 +19,6 @@ pipeline {
|
||||
APP_NAME = 'service-gateway'
|
||||
GO_MOD_CACHE_DIR = '/home/pi/Desktop/docker/jenkins/caches/go-mod'
|
||||
GO_BUILD_CACHE_DIR = '/home/pi/Desktop/docker/jenkins/caches/go-build'
|
||||
INSTGO_CACHE_DIR = '/home/pi/Desktop/docker/jenkins/caches/instgo'
|
||||
}
|
||||
|
||||
options {
|
||||
@@ -54,7 +53,7 @@ pipeline {
|
||||
docker {
|
||||
image 'golang:1.24.0-alpine'
|
||||
reuseNode true
|
||||
args '-u root:root -v $GO_MOD_CACHE_DIR:/go/pkg/mod -v $GO_BUILD_CACHE_DIR:/root/.cache/go-build -v $INSTGO_CACHE_DIR:/root/instgo-caches'
|
||||
args '-u root:root -v $GO_MOD_CACHE_DIR:/go/pkg/mod -v $GO_BUILD_CACHE_DIR:/root/.cache/go-build'
|
||||
}
|
||||
}
|
||||
steps {
|
||||
@@ -63,12 +62,8 @@ pipeline {
|
||||
export CGO_ENABLED=0
|
||||
export GOOS=linux
|
||||
export GOARCH=amd64
|
||||
export INSTGO_CACHE_DIR=/root/instgo-caches
|
||||
|
||||
wget "http://arms-apm-cn-chengdu.oss-cn-chengdu.aliyuncs.com/instgo/instgo-linux-arm64" -O instgo
|
||||
chmod +x instgo
|
||||
./instgo set --licenseKey=gys9jv7jqe@144a6fd684976b3
|
||||
./instgo go build -o ${env.APP_NAME} .
|
||||
go build -o ${env.APP_NAME} .
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
bou.ke/monkey v1.0.2
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed
|
||||
github.com/alicebob/miniredis/v2 v2.35.0
|
||||
github.com/gin-contrib/cors v1.7.6
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
|
||||
4
go.sum
4
go.sum
@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
|
||||
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
|
||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e h1:jcDwaL280D5BVGCfcIC3mQCjmwPz5fxX7NO1qf6nAIo=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260123132258-4d661d08134e/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed h1:O08p0egfekFqQSnc4sfEJUTI5dGiEyiDRNW/VYa/Ce4=
|
||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
|
||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=
|
||||
|
||||
@@ -10,7 +10,7 @@ const (
|
||||
ServiceStatusStopping // 停止中
|
||||
)
|
||||
|
||||
var GatewaySID string
|
||||
var GatewaySID int64
|
||||
|
||||
const (
|
||||
KeyGatewayAccessToken = "gateway:token:access:%v"
|
||||
|
||||
@@ -25,7 +25,7 @@ func (s *Server) ToClient(server grpc_pb.Gateway_ToClientServer) error {
|
||||
if args, err := server.Recv(); err != nil {
|
||||
return
|
||||
} else {
|
||||
if args.USN == "" {
|
||||
if args.USN == -1 {
|
||||
|
||||
//utils.WorkerPool(ws_handler.UserMgr.GetAllInterface(), func(task interface{}) {
|
||||
// client := task.(*ws_handler.Client)
|
||||
|
||||
@@ -24,7 +24,7 @@ type LoginReq struct {
|
||||
}
|
||||
|
||||
type LoginResp struct {
|
||||
USN string `json:"usn"`
|
||||
USN int64 `json:"usn"`
|
||||
Name string `json:"name"`
|
||||
AccessToken string `json:"accessToken"`
|
||||
RefreshToken string `json:"refreshToken"`
|
||||
@@ -43,7 +43,7 @@ func Login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
usn, name := "", ""
|
||||
usn, name := int64(0), ""
|
||||
if req.Phone != "" {
|
||||
// TODO 校验验证码
|
||||
login, err := client.PhoneLogin(c, &grpc_pb.PhoneLoginReq{
|
||||
@@ -117,7 +117,7 @@ func RefreshToken(c *gin.Context) {
|
||||
}))
|
||||
}
|
||||
|
||||
func genToken(ctx context.Context, usn string) (string, string, error) {
|
||||
func genToken(ctx context.Context, usn int64) (string, string, error) {
|
||||
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
@@ -129,7 +129,7 @@ func genToken(ctx context.Context, usn string) (string, string, error) {
|
||||
return at, rt, nil
|
||||
}
|
||||
|
||||
func genTokenOne(ctx context.Context, key string, usn string, ttl time.Duration) (string, error) {
|
||||
func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) {
|
||||
token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -23,13 +23,13 @@ type Client struct {
|
||||
heartBeat time.Time // 最后一次心跳
|
||||
|
||||
Status int32 // 状态:0 登陆中 1 正常 2 离线
|
||||
USN string // 用户ID
|
||||
SceneSID string // 场景服ID
|
||||
USN int64 // 用户ID
|
||||
SceneSID int64 // 场景服ID
|
||||
InstanceID int32 // 副本ID,副本类型
|
||||
UniqueNo string // 副本唯一编号
|
||||
UniqueNo int64 // 副本唯一编号
|
||||
}
|
||||
|
||||
func NewClient(usn string, conn socket.ISocketConn) *Client {
|
||||
func NewClient(usn int64, conn socket.ISocketConn) *Client {
|
||||
client := &Client{
|
||||
USN: usn,
|
||||
conn: conn,
|
||||
|
||||
@@ -75,7 +75,7 @@ func (c *Client) onEnter(msg *ss_pb.C2S_EnterInstance) {
|
||||
}
|
||||
|
||||
func (c *Client) onLeave() {
|
||||
if c.SceneSID == "" {
|
||||
if c.SceneSID == 0 {
|
||||
return
|
||||
}
|
||||
client, err := grpc_client.SceneNewClient(c.SceneSID)
|
||||
@@ -94,7 +94,7 @@ func (c *Client) onLeave() {
|
||||
}
|
||||
|
||||
func (c *Client) onAction(msg *ss_pb.C2S_Action) {
|
||||
if c.SceneSID == "" {
|
||||
if c.SceneSID == 0 {
|
||||
return
|
||||
}
|
||||
if err := grpc_client.SendMessageToScene(c.SceneSID, grpc_client.FunAction, &grpc_pb.ActionReq{
|
||||
|
||||
@@ -8,35 +8,35 @@ import (
|
||||
var UserMgr *userManager
|
||||
|
||||
type userManager struct {
|
||||
userMap map[string]*Client
|
||||
userMap map[int64]*Client
|
||||
sync.RWMutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
UserMgr = &userManager{
|
||||
userMap: make(map[string]*Client),
|
||||
userMap: make(map[int64]*Client),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *userManager) Add(usn string, client *Client) {
|
||||
func (m *userManager) Add(usn int64, client *Client) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
m.userMap[usn] = client
|
||||
global.OnlineUsersGauge.Inc()
|
||||
}
|
||||
|
||||
func (m *userManager) Delete(usn string) {
|
||||
func (m *userManager) Delete(usn int64) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
delete(m.userMap, usn)
|
||||
global.OnlineUsersGauge.Dec()
|
||||
}
|
||||
|
||||
func (m *userManager) GetAll() map[string]*Client {
|
||||
func (m *userManager) GetAll() map[int64]*Client {
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
|
||||
copyMap := make(map[string]*Client, len(m.userMap))
|
||||
copyMap := make(map[int64]*Client, len(m.userMap))
|
||||
for k, v := range m.userMap {
|
||||
copyMap[k] = v
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func (m *userManager) GetAllInterface() []interface{} {
|
||||
return r
|
||||
}
|
||||
|
||||
func (m *userManager) GetByUSN(usn string) *Client {
|
||||
func (m *userManager) GetByUSN(usn int64) *Client {
|
||||
m.RLock()
|
||||
defer m.RUnlock()
|
||||
return m.userMap[usn]
|
||||
|
||||
@@ -161,7 +161,7 @@ func (l *Login) CheckOnline(user *User) string {
|
||||
}
|
||||
|
||||
// KickUser 把玩家踢下线
|
||||
func (l *Login) KickUser(gatewaySID string, usn string) bool {
|
||||
func (l *Login) KickUser(gatewaySID int64, usn int64) bool {
|
||||
gc, err := grpc_client.GatewayNewClient(gatewaySID)
|
||||
if err != nil {
|
||||
log.Errorf("KickUser cannot find gateway client: %v, sid: %v", err, gatewaySID)
|
||||
|
||||
@@ -78,7 +78,7 @@ func (q *QueueUp) Dequeue() (*client.Client, error) {
|
||||
}
|
||||
|
||||
// GetPosition 返回用户前面还有多少人在排队
|
||||
func (q *QueueUp) GetPosition(usn string) (int64, bool) {
|
||||
func (q *QueueUp) GetPosition(usn int64) (int64, bool) {
|
||||
val, ok := q.waiting.Load(usn)
|
||||
if !ok {
|
||||
return 0, false
|
||||
@@ -88,7 +88,7 @@ func (q *QueueUp) GetPosition(usn string) (int64, bool) {
|
||||
}
|
||||
|
||||
// RemoveUser 安全移除用户(标记为取消)
|
||||
func (q *QueueUp) RemoveUser(usn string) bool {
|
||||
func (q *QueueUp) RemoveUser(usn int64) bool {
|
||||
_, loaded := q.waiting.LoadAndDelete(usn)
|
||||
return loaded
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func authJwt() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// 这里将Header写到请求中,grpc-gateway框架会读取然后传给grpc服务
|
||||
c.Request.Header.Set("X-Usn", claims.USN)
|
||||
c.Request.Header.Set("X-Usn", utils.Int64ToString(claims.USN))
|
||||
// 这里写到上下文中,打日志
|
||||
c.Set("usn", claims.USN)
|
||||
c.Next()
|
||||
|
||||
@@ -14,6 +14,7 @@ import (
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func InitServeMux() *runtime.ServeMux {
|
||||
@@ -49,7 +50,13 @@ func InitRouter() *gin.Engine {
|
||||
gin.Recovery(),
|
||||
ginLogger(log.GetLogger().Named("GIN")),
|
||||
cors.New(corsConfig()),
|
||||
otelgin.Middleware(common.KeyDiscoverServiceNameGateway),
|
||||
otelgin.Middleware(
|
||||
common.KeyDiscoverServiceNameGateway,
|
||||
otelgin.WithSpanNameFormatter(func(c *gin.Context) string {
|
||||
method := strings.ToUpper(c.Request.Method)
|
||||
return method + " " + c.Request.URL.Path
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
r.HandleMethodNotAllowed = true
|
||||
|
||||
@@ -44,7 +44,7 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
|
||||
|
||||
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
||||
cli, ok := conn.GetParam("client").(*client.Client)
|
||||
if !ok || cli.USN == "" || cli.Status != 1 {
|
||||
if !ok || cli.USN == 0 || cli.Status != 1 {
|
||||
return socket.Close
|
||||
}
|
||||
cli.OnEvent(&client.ClientEvent{Msg: bytes})
|
||||
@@ -53,7 +53,7 @@ func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socke
|
||||
|
||||
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
||||
cli, ok := conn.GetParam("client").(*client.Client)
|
||||
if !ok || cli.USN == "" {
|
||||
if !ok || cli.USN == 0 {
|
||||
return
|
||||
}
|
||||
cli.OnEvent(&client.PongEvent{})
|
||||
|
||||
Reference in New Issue
Block a user