From 049aedf1155b37d904f23034999b53de01705dfb Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Sun, 30 Jul 2023 19:25:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E9=85=8D=E7=BD=AEreplica=5Fi?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config_replica_id.sh | 33 ++++++++ server/assistant/assistant.go | 36 --------- server/assistant/assistant_test.go | 7 -- server/assistant/etc/assistant.yaml | 8 -- server/assistant/internal/config/config.go | 13 --- .../internal/handler/redirecthandler.go | 35 -------- server/assistant/internal/handler/routes.go | 22 ------ .../assistant/internal/logic/redirectlogic.go | 67 ---------------- .../assistant/internal/svc/servicecontext.go | 67 ---------------- server/assistant/internal/types/types.go | 79 ------------------- server/auth/etc/auth.yaml | 1 + server/canteen/etc/canteen.yaml | 1 + server/data-transfer/etc/data-transfer.yaml | 1 + server/home-user-auth/etc/home-user-auth.yaml | 1 + server/inventory/etc/inventory.yaml | 1 + server/map-library/etc/map-library.yaml | 1 + server/orders/etc/orders.yaml | 1 + server/pay/etc/pay.yaml | 1 + .../etc/product-template-tag.yaml | 1 + server/product/etc/product.yaml | 1 + server/render/etc/render.yaml | 1 + .../etc/shopping-cart-confirmation.yaml | 1 + server/upload/etc/upload.yaml | 1 + server/webset/etc/webset.yaml | 1 + server/websocket/etc/websocket.yaml | 1 + 25 files changed, 48 insertions(+), 334 deletions(-) create mode 100644 config_replica_id.sh delete mode 100644 server/assistant/assistant.go delete mode 100644 server/assistant/assistant_test.go delete mode 100644 server/assistant/etc/assistant.yaml delete mode 100644 server/assistant/internal/config/config.go delete mode 100644 server/assistant/internal/handler/redirecthandler.go delete mode 100644 server/assistant/internal/handler/routes.go delete mode 100644 server/assistant/internal/logic/redirectlogic.go delete mode 100644 server/assistant/internal/svc/servicecontext.go delete mode 100644 server/assistant/internal/types/types.go diff --git a/config_replica_id.sh b/config_replica_id.sh new file mode 100644 index 00000000..60e604e2 --- /dev/null +++ b/config_replica_id.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +replica_id=5 + +change_config(){ + + server_name=$1 + config_file="server/${server_name}/etc/${server_name}.yaml" + + # 无论ReplicaId存在与否,都重置replica_id + ((replica_id+=5)) + + if ! grep -q "ReplicaId:" "$config_file"; then + + sed -i "s/^Port:.*$/&\nReplicaId: $replica_id/" "$config_file" + echo "Added ReplicaId: $replica_id to $config_file" + + else + + exist_id=$(grep -oP "ReplicaId:\s*\K[0-9]+" "$config_file") + sed -i "s/^ReplicaId:.*$/ReplicaId: $replica_id/" "$config_file" + echo "Updated ReplicaId to $replica_id in $config_file" + + fi + +} + +# 读取server目录,忽略指定的三个目录 +server_dirs=($(cd server && ls | grep -vE "backend|product-template|product-model")) + +for server_dir in "${server_dirs[@]}"; do + change_config $server_dir +done \ No newline at end of file diff --git a/server/assistant/assistant.go b/server/assistant/assistant.go deleted file mode 100644 index 8ae0cf8e..00000000 --- a/server/assistant/assistant.go +++ /dev/null @@ -1,36 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "net/http" - "time" - - "fusenapi/utils/auth" - - "fusenapi/server/assistant/internal/config" - "fusenapi/server/assistant/internal/handler" - "fusenapi/server/assistant/internal/svc" - - "github.com/zeromicro/go-zero/core/conf" - "github.com/zeromicro/go-zero/rest" -) - -var configFile = flag.String("f", "etc/assistant.yaml", "the config file") - -func main() { - flag.Parse() - - var c config.Config - conf.MustLoad(*configFile, &c) - c.Timeout = int64(time.Second * 15) - server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { - })) - defer server.Stop() - - ctx := svc.NewServiceContext(c) - handler.RegisterHandlers(server, ctx) - - fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) - server.Start() -} diff --git a/server/assistant/assistant_test.go b/server/assistant/assistant_test.go deleted file mode 100644 index ef649768..00000000 --- a/server/assistant/assistant_test.go +++ /dev/null @@ -1,7 +0,0 @@ -package main - -import "testing" - -func TestMain(t *testing.T) { - main() -} diff --git a/server/assistant/etc/assistant.yaml b/server/assistant/etc/assistant.yaml deleted file mode 100644 index cda59ed5..00000000 --- a/server/assistant/etc/assistant.yaml +++ /dev/null @@ -1,8 +0,0 @@ -Name: assistant -Host: localhost -Port: 9950 -SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest -Auth: - AccessSecret: fusen2023 - AccessExpire: 2592000 - RefreshAfter: 1592000 \ No newline at end of file diff --git a/server/assistant/internal/config/config.go b/server/assistant/internal/config/config.go deleted file mode 100644 index 3a3ea6ff..00000000 --- a/server/assistant/internal/config/config.go +++ /dev/null @@ -1,13 +0,0 @@ -package config - -import ( - "fusenapi/server/assistant/internal/types" - - "github.com/zeromicro/go-zero/rest" -) - -type Config struct { - rest.RestConf - SourceMysql string - Auth types.Auth -} diff --git a/server/assistant/internal/handler/redirecthandler.go b/server/assistant/internal/handler/redirecthandler.go deleted file mode 100644 index 46cd7929..00000000 --- a/server/assistant/internal/handler/redirecthandler.go +++ /dev/null @@ -1,35 +0,0 @@ -package handler - -import ( - "net/http" - "reflect" - - "fusenapi/utils/basic" - - "fusenapi/server/assistant/internal/logic" - "fusenapi/server/assistant/internal/svc" - "fusenapi/server/assistant/internal/types" -) - -func RedirectHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - - var req types.RequestRedirect - userinfo, err := basic.RequestParse(w, r, svcCtx.SharedState, &req) - if err != nil { - return - } - - // 创建一个业务逻辑层实例 - l := logic.NewRedirectLogic(r.Context(), svcCtx) - - rl := reflect.ValueOf(l) - basic.BeforeLogic(w, r, rl) - - resp := l.Redirect(&req, userinfo) - - if !basic.AfterLogic(w, r, rl, resp) { - basic.NormalAfterLogic(w, r, resp) - } - } -} diff --git a/server/assistant/internal/handler/routes.go b/server/assistant/internal/handler/routes.go deleted file mode 100644 index daeb8d44..00000000 --- a/server/assistant/internal/handler/routes.go +++ /dev/null @@ -1,22 +0,0 @@ -// Code generated by goctl. DO NOT EDIT. -package handler - -import ( - "net/http" - - "fusenapi/server/assistant/internal/svc" - - "github.com/zeromicro/go-zero/rest" -) - -func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { - server.AddRoutes( - []rest.Route{ - { - Method: http.MethodPost, - Path: "/api/assistant/redirect", - Handler: RedirectHandler(serverCtx), - }, - }, - ) -} diff --git a/server/assistant/internal/logic/redirectlogic.go b/server/assistant/internal/logic/redirectlogic.go deleted file mode 100644 index a637f429..00000000 --- a/server/assistant/internal/logic/redirectlogic.go +++ /dev/null @@ -1,67 +0,0 @@ -package logic - -import ( - "fmt" - "fusenapi/utils/auth" - "fusenapi/utils/basic" - "log" - "net/http" - - "context" - - "fusenapi/server/assistant/internal/svc" - "fusenapi/server/assistant/internal/types" - - "github.com/zeromicro/go-zero/core/logx" -) - -type RedirectLogic struct { - logx.Logger - ctx context.Context - svcCtx *svc.ServiceContext - - RedirectUrl string -} - -func NewRedirectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RedirectLogic { - return &RedirectLogic{ - Logger: logx.WithContext(ctx), - ctx: ctx, - svcCtx: svcCtx, - } -} - -// 处理进入前逻辑w,r -// func (l *RedirectLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { -// } - -// 处理逻辑后 w,r 如:重定向 -func (l *RedirectLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { - if l.RedirectUrl != "" { - html := fmt.Sprintf(` - - - - Redirect - - - - - - `, l.RedirectUrl) - fmt.Fprintln(w, html) - } - -} - -func (l *RedirectLogic) Redirect(req *types.RequestRedirect, userinfo *auth.UserInfo) (resp *basic.Response) { - // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) - // userinfo 传入值时, 一定不为null - l.RedirectUrl = req.Url - log.Println(req.Url) - return resp.SetStatus(basic.CodeOK) -} diff --git a/server/assistant/internal/svc/servicecontext.go b/server/assistant/internal/svc/servicecontext.go deleted file mode 100644 index 26f65471..00000000 --- a/server/assistant/internal/svc/servicecontext.go +++ /dev/null @@ -1,67 +0,0 @@ -package svc - -import ( - "errors" - "fmt" - "fusenapi/fsm" - "fusenapi/server/assistant/internal/config" - "fusenapi/utils/autoconfig" - "net/http" - - "fusenapi/initalize" - "fusenapi/model/gmodel" - - "github.com/golang-jwt/jwt" - "gorm.io/gorm" -) - -type ServiceContext struct { - Config config.Config - SharedState *fsm.StateCluster - - MysqlConn *gorm.DB - AllModels *gmodel.AllModelsGen -} - -func NewServiceContext(c config.Config) *ServiceContext { - conn := initalize.InitMysql(c.SourceMysql) - StateServer := fsm.StartNode(c.Name, fmt.Sprintf("%s:%d", c.Host, c.Port-2000), autoconfig.AutoGetAllServerConfig(), conn) - - return &ServiceContext{ - Config: c, - MysqlConn: conn, - SharedState: StateServer, - AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)), - } -} - -func (svcCtx *ServiceContext) ParseJwtToken(r *http.Request) (jwt.MapClaims, error) { - AuthKey := r.Header.Get("Authorization") - if AuthKey == "" { - return nil, nil - } - AuthKey = AuthKey[7:] - - if len(AuthKey) <= 50 { - return nil, errors.New(fmt.Sprint("Error parsing token, len:", len(AuthKey))) - } - - token, err := jwt.Parse(AuthKey, func(token *jwt.Token) (interface{}, error) { - // 检查签名方法是否为 HS256 - if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { - return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) - } - // 返回用于验证签名的密钥 - return []byte(svcCtx.Config.Auth.AccessSecret), nil - }) - if err != nil { - return nil, errors.New(fmt.Sprint("Error parsing token:", err)) - } - - // 验证成功返回 - if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid { - return claims, nil - } - - return nil, errors.New(fmt.Sprint("Invalid token", err)) -} diff --git a/server/assistant/internal/types/types.go b/server/assistant/internal/types/types.go deleted file mode 100644 index bf4044b1..00000000 --- a/server/assistant/internal/types/types.go +++ /dev/null @@ -1,79 +0,0 @@ -// Code generated by goctl. DO NOT EDIT. -package types - -import ( - "fusenapi/utils/basic" -) - -type RequestRedirect struct { - Url string `json:"url"` -} - -type Request struct { -} - -type Response struct { - Code int `json:"code"` - Message string `json:"msg"` - Data interface{} `json:"data"` -} - -type Auth struct { - AccessSecret string `json:"accessSecret"` - AccessExpire int64 `json:"accessExpire"` - RefreshAfter int64 `json:"refreshAfter"` -} - -type File struct { - Filename string `fsfile:"filename"` - Header map[string][]string `fsfile:"header"` - Size int64 `fsfile:"size"` - Data []byte `fsfile:"data"` -} - -type Meta struct { - TotalCount int64 `json:"totalCount"` - PageCount int64 `json:"pageCount"` - CurrentPage int `json:"currentPage"` - PerPage int `json:"perPage"` -} - -// Set 设置Response的Code和Message值 -func (resp *Response) Set(Code int, Message string) *Response { - return &Response{ - Code: Code, - Message: Message, - } -} - -// Set 设置整个Response -func (resp *Response) SetWithData(Code int, Message string, Data interface{}) *Response { - return &Response{ - Code: Code, - Message: Message, - Data: Data, - } -} - -// SetStatus 设置默认StatusResponse(内部自定义) 默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatus(sr *basic.StatusResponse, data ...interface{}) *Response { - newResp := &Response{ - Code: sr.Code, - } - if len(data) == 1 { - newResp.Data = data[0] - } - return newResp -} - -// SetStatusWithMessage 设置默认StatusResponse(内部自定义) 非默认msg, 可以带data, data只使用一个参数 -func (resp *Response) SetStatusWithMessage(sr *basic.StatusResponse, msg string, data ...interface{}) *Response { - newResp := &Response{ - Code: sr.Code, - Message: msg, - } - if len(data) == 1 { - newResp.Data = data[0] - } - return newResp -} diff --git a/server/auth/etc/auth.yaml b/server/auth/etc/auth.yaml index 52364fc8..17bd9019 100644 --- a/server/auth/etc/auth.yaml +++ b/server/auth/etc/auth.yaml @@ -1,6 +1,7 @@ Name: auth Host: localhost Port: 9980 +ReplicaId: 10 MainAddress: "http://localhost:9900" SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/canteen/etc/canteen.yaml b/server/canteen/etc/canteen.yaml index 2745f38f..7e6d963e 100644 --- a/server/canteen/etc/canteen.yaml +++ b/server/canteen/etc/canteen.yaml @@ -1,6 +1,7 @@ Name: canteen Host: localhost Port: 9902 +ReplicaId: 15 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/data-transfer/etc/data-transfer.yaml b/server/data-transfer/etc/data-transfer.yaml index fc2792fe..a4456c6c 100644 --- a/server/data-transfer/etc/data-transfer.yaml +++ b/server/data-transfer/etc/data-transfer.yaml @@ -1,6 +1,7 @@ Name: data-transfer Host: localhost Port: 9903 +ReplicaId: 20 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/home-user-auth/etc/home-user-auth.yaml b/server/home-user-auth/etc/home-user-auth.yaml index 876d7c70..c1928b60 100644 --- a/server/home-user-auth/etc/home-user-auth.yaml +++ b/server/home-user-auth/etc/home-user-auth.yaml @@ -1,6 +1,7 @@ Name: home-user-auth Host: localhost Port: 9904 +ReplicaId: 25 MainAddress: "http://localhost:9900" SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest diff --git a/server/inventory/etc/inventory.yaml b/server/inventory/etc/inventory.yaml index decc2507..9b76f174 100644 --- a/server/inventory/etc/inventory.yaml +++ b/server/inventory/etc/inventory.yaml @@ -1,6 +1,7 @@ Name: inventory Host: localhost Port: 9905 +ReplicaId: 30 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/map-library/etc/map-library.yaml b/server/map-library/etc/map-library.yaml index c86dc1fe..3c57b532 100644 --- a/server/map-library/etc/map-library.yaml +++ b/server/map-library/etc/map-library.yaml @@ -1,6 +1,7 @@ Name: map-library Host: localhost Port: 9906 +ReplicaId: 35 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/orders/etc/orders.yaml b/server/orders/etc/orders.yaml index d2d92f86..4f730681 100644 --- a/server/orders/etc/orders.yaml +++ b/server/orders/etc/orders.yaml @@ -1,6 +1,7 @@ Name: orders Host: localhost Port: 9907 +ReplicaId: 40 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/pay/etc/pay.yaml b/server/pay/etc/pay.yaml index 841753e3..115a836f 100644 --- a/server/pay/etc/pay.yaml +++ b/server/pay/etc/pay.yaml @@ -1,6 +1,7 @@ Name: pay Host: 0.0.0.0 Port: 9915 +ReplicaId: 45 Timeout: 15000 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: diff --git a/server/product-template-tag/etc/product-template-tag.yaml b/server/product-template-tag/etc/product-template-tag.yaml index 253828fb..8dfed65a 100644 --- a/server/product-template-tag/etc/product-template-tag.yaml +++ b/server/product-template-tag/etc/product-template-tag.yaml @@ -1,6 +1,7 @@ Name: product-template-tag Host: 0.0.0.0 Port: 9916 +ReplicaId: 10 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/product/etc/product.yaml b/server/product/etc/product.yaml index 12cb36a3..5735ca3d 100644 --- a/server/product/etc/product.yaml +++ b/server/product/etc/product.yaml @@ -1,6 +1,7 @@ Name: product Host: localhost Port: 9908 +ReplicaId: 50 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/render/etc/render.yaml b/server/render/etc/render.yaml index c4866d02..bbc02a0c 100644 --- a/server/render/etc/render.yaml +++ b/server/render/etc/render.yaml @@ -1,6 +1,7 @@ Name: render Host: localhost Port: 8888 +ReplicaId: 55 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml index 32f32ad7..f9fba524 100644 --- a/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml +++ b/server/shopping-cart-confirmation/etc/shopping-cart-confirmation.yaml @@ -1,6 +1,7 @@ Name: shopping-cart-confirmation Host: localhost Port: 9911 +ReplicaId: 60 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023 diff --git a/server/upload/etc/upload.yaml b/server/upload/etc/upload.yaml index 1a07e281..36c82319 100644 --- a/server/upload/etc/upload.yaml +++ b/server/upload/etc/upload.yaml @@ -1,6 +1,7 @@ Name: upload Host: localhost Port: 9912 +ReplicaId: 65 SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Env: "test" Auth: diff --git a/server/webset/etc/webset.yaml b/server/webset/etc/webset.yaml index 08d1d41c..fa8374d3 100644 --- a/server/webset/etc/webset.yaml +++ b/server/webset/etc/webset.yaml @@ -1,6 +1,7 @@ Name: webset Host: localhost Port: 9913 +ReplicaId: 70 SourceMysql: "fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest" Auth: AccessSecret: fusen2023 diff --git a/server/websocket/etc/websocket.yaml b/server/websocket/etc/websocket.yaml index 5f58765d..c069c61a 100644 --- a/server/websocket/etc/websocket.yaml +++ b/server/websocket/etc/websocket.yaml @@ -1,6 +1,7 @@ Name: websocket Host: 0.0.0.0 Port: 9914 +ReplicaId: 75 SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest Auth: AccessSecret: fusen2023