tmaic saas多租戶解決方案
tmaic saas 多租戶(Multi-Tenant ),即一個(gè)Tenant,一個(gè)Database“的數(shù)據(jù)存儲(chǔ)方式。隔離級(jí)別最高、最安全,說到底是要解決數(shù)據(jù)存儲(chǔ)的問題,golang語言saas框架唯數(shù)不多的框架之一。
軟件架構(gòu)
數(shù)據(jù)模型
以下是例子
package models import ( "github.com/pangxianfei/framework/helpers/m" "github.com/pangxianfei/framework/helpers/ptr" "github.com/pangxianfei/framework/helpers/zone" "github.com/pangxianfei/framework/model" ) type User struct { model.BaseModel ID *uint `gorm:"column:user_id;primary_key;auto_increment"` Name *string `gorm:"column:user_name;type:varchar(100)"` Email *string `gorm:"column:user_email;type:varchar(100);unique_index;not null"` Password *string `gorm:"column:user_password;type:varchar(100);not null"` CreatedAt *zone.Time `gorm:"column:user_created_at"` UpdatedAt zone.Time `gorm:"column:user_updated_at"` DeletedAt *zone.Time `gorm:"column:user_deleted_at"` } func (user *User) TableName() string { return user.SetTableName("user") } func (user *User) SetNameAttribute(value interface{}) { user.Name = user.Email } func (user *User) GetUpdatedAtAttribute(value interface{}) interface{} { return user.UpdatedAt //查詢后這里可以其他處理,如格式化處理,如果是時(shí)間time 可以格式成 2020-10-01 18:00:20 }
生成公鑰文件
tmaic.CreateRsaKey()
系統(tǒng)函數(shù)
加密解密函數(shù)
公鑰加密
s,_:= tmaic.Encryption("Golang使用RSA進(jìn)行公鑰加密私鑰解密,私鑰加密公鑰解密的實(shí)現(xiàn)")
私鑰解密
dd,_ := tmaic.Decrypt(s)
私鑰加密
s,_:= tmaic.PrivateEncryption("Golang使用RSA進(jìn)行公鑰加密私鑰解密,私鑰加密公鑰解密的實(shí)現(xiàn)")
公鑰解密
dd,_ := tmaic.PublicDecrypt(s)
校驗(yàn)密碼是否正確
user.Password:用戶密碼(已加密)
requestData.Password:要校驗(yàn)的密碼(明文)
if !crypt.BcryptCheck(user.Password, requestData.Password) error{ return "返回提示信息" }
注意事項(xiàng):如果要使用 NSQ 隊(duì)列的,不用將包注釋
導(dǎo)入包:
"github.com/pangxianfei/framework/queue"
"tmaic/app/events"
"tmaic/app/jobs"
"tmaic/app/listeners"
以下去掉注釋即可:
//queue.Initialize()
//jobs.Initialize()
//events.Initialize()
//listeners.Initialize()
NSQ安裝請(qǐng)參考:https://nsq.io/overview/quick_start.html
nsq啟動(dòng)
1.nsqlookupd
2.nsqd --lookupd-tcp-address=127.0.0.1:4160
3.nsqadmin --lookupd-http-address=127.0.0.1:4161
執(zhí)行以上命令后:http://127.0.0.1:4171 可查看監(jiān)控臺(tái)
隊(duì)列demo (test)
*** 詳情見工程項(xiàng)目下
入列(我們常說的寫入 topic,生產(chǎn)者),以下寫入topic例子
test := events.Test{} testparam := &pbs.Test{ Id: uint32(userId), } test.SetParam(testparam) if errs := hub.Emit(&test); errs != nil { log.Info("user test", tmaic.Output{"event": test, "errors": errs}) }
出列(啟動(dòng)一個(gè)觀察者)
func init() { hub.Register(&Test{}) } type Test struct { user models.User hub.Listen } func (user *Test) Name() hub.ListenerName { return "add-test" //監(jiān)聽器名稱 后面我們會(huì)用到 } func (user *Test) Subscribe() (eventPtrList []hub.Eventer) { log.Debug("Subscribe-test") return []hub.Eventer{ &events.Test{}, } } func (user *Test) Construct(paramPtr proto.Message) error { /** 第一執(zhí)行這里 業(yè)務(wù)代碼 */ log.Debug("Construct-test") return nil } func (user *Test) Handle() error { /**第二執(zhí)行這里 最終實(shí)現(xiàn)業(yè)務(wù)邏輯 :比如發(fā)郵件、消息推送、短信通知等 這些業(yè)務(wù)通常封裝在 service 層,這里只是建議 */ // 更新 t := users.UserService.Get(cast.ToInt64(user.ID)) t.Email = user.Email //test 其實(shí)沒有這個(gè)字段 根據(jù)需要自行組織 t.Name = user.Name //test 其實(shí)沒有這個(gè)字段 根據(jù)需要自行組織 _ = users.UserService.Update(t) log.Debug("Handle-test") return nil }
實(shí)現(xiàn)消費(fèi),在根目錄與main.go同級(jí),
go run artisan.go queue:listen test-add //test-add 是Name() 返回的 配合supervisor進(jìn)程守護(hù)
類型轉(zhuǎn)化助手
ToInt64
cast.ToInt64()
ToInt32
cast.ToInt32()
ToFloat32
cast.ToFloat32()
更多方法 查看cast包
打印
tmaic.Dump(mugs)
mugs := map[string]interface{} { "password2" : map[string]string { "password3" : "The name cannot be empty", }, } //效果 { "password2": { "password3": "The name cannot be empty" } }
性能測(cè)試
虛擬機(jī)環(huán)境下:
壓力測(cè)試
ab -c 1000 -n 5000 -k http://127.0.0.1/all
安裝教程
- git clone [email protected]:pangxianfei/tmaic.git
- go mod init // 初始化go mod 包管理
- go mod tidy // 加載依賴包
- go mod vendor // 將依賴包拷貝到項(xiàng)目目錄中去
- go run main.go
