完成配置replica_id

This commit is contained in:
eson 2023-07-30 19:25:59 +08:00
parent 47ae46c46d
commit 049aedf115
25 changed files with 48 additions and 334 deletions

33
config_replica_id.sh Normal file
View File

@ -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

View File

@ -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()
}

View File

@ -1,7 +0,0 @@
package main
import "testing"
func TestMain(t *testing.T) {
main()
}

View File

@ -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

View File

@ -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
}

View File

@ -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)
}
}
}

View File

@ -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),
},
},
)
}

View File

@ -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(`
<!DOCTYPE html>
<html>
<head>
<title>Redirect</title>
<script type="text/javascript">
window.onload = function() {
window.location = %s;
}
</script>
</head>
<body>
</body>
</html>
`, 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)
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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

View File

@ -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