最近發現golang社群裡出了一個新星的微服務框架,來自好未來,光看這個名字,就很有奔頭,之前,也只是玩過go-micro,其實真正的還沒有在專案中運用過,只是覺得 微服務,grpc 這些很高大尚,還沒有在專案中,真正的玩過,我看了一下官方提供的工具真的很好用,只需要定義好,舒適檔案jia結構 都生成了,只需要關心業務,加上最近 有個投票的活動,加上最近這幾年中臺也比較火,所以決定玩一下,
先聊聊中臺架構思路吧:
中臺的概念大概就是把一個一個的app 統一起來,反正我是這樣理解的。
最後說說實現吧,整個就一個repo:
閘道器,我們用的是: go-zero的Api服務
其它它的是服務,我們就是用的go-zero的rpc服務
看下目錄結構
整個專案完成,我一個人操刀, 寫了1個來星期,我就實現了上面的中臺系統。
我們先把閘道器搭建起來
➜ blogs mkdir datacenter && cd datacenter➜ datacenter go mod init datacentergo: creating new go.mod: module datacenter➜ datacenter
檢視book目錄:
➜ datacenter tree.└── go.mod0 directories, 1 file
建立api檔案
➜ datacenter goctl api -o datacenter.apiDone.➜ datacenter tree.├── datacenter.api└── go.mod
定義api服務
分別包含了上面的 公共服務,使用者服務,投票活動服務
info( title: "中臺系統" desc: "中臺系統" author: "jackluo" email: "[email protected]")// 獲取 應用資訊type Beid struct { Beid int64 `json:"beid"`}type Token struct{ Token string `json:"token"`}type WxTicket struct{ Ticket string `json:"ticket"`}type Application struct { Sname string `json:"Sname"` //名稱 Logo string `json:"logo"` // login Isclose int64 `json:"isclose"` //是否關閉 Fullwebsite string `json:"fullwebsite"` // 全站名稱}type SnsReq struct{ Beid Ptyid int64 `json:"ptyid"` //對應平臺 BackUrl string `json:"back_url"` //登陸返回的地址}type SnsResp struct{ Beid Ptyid int64 `json:"ptyid"` //對應平臺 Appid string `json:"appid"` //sns 平臺的id Title string `json:"title"` //名稱 LoginUrl string `json:"login_url"` //微信登陸的地址}type WxShareResp struct { Appid string `json:"appid"` Timestamp int64 `json:"timestamp"` Noncestr string `json:"noncestr"` Signature string `json:"signature"`}@server( group: common)service datacenter-api { @doc( summary: "獲取站點的資訊" ) @handler votesVerification get /MP_verify_NT04cqknJe0em3mT.txt (SnsReq) returns (SnsResp) @handler appInfo get /common/appinfo (Beid) returns (Application) @doc( summary: "獲取站點的社交屬性資訊" ) @handler snsInfo post /common/snsinfo (SnsReq) returns (SnsResp) // 獲取分享的 @handler wxTicket post /common/wx/ticket (SnsReq) returns (WxShareResp)}// 上傳需要登陸@server( jwt: Auth group: common)service datacenter-api { @doc( summary: "七牛上傳憑證" ) @handler qiuniuToken post /common/qiuniu/token (Beid) returns (Token)}// 註冊請求type RegisterReq struct { // TODO: add members here and delete this comment Mobile string `json:"mobile"` // 基本一個手機號碼就完事 Password string `json:"password"` Smscode string `json:"smscode"` // 簡訊碼}// 登陸請求type LoginReq struct{ Mobile string `json:"mobile"` Type int64 `json:"type"` // 1.密碼登陸,2.簡訊登陸 Password string `json:"password"`}// 微信登陸type WxLoginReq struct { Beid int64 `json:"beid"` // 應用id Code string `json:"code"` // 微信登陸金鑰 Ptyid int64 `json:"ptyid"` // 對應平臺}//返回使用者資訊type UserReply struct { Auid int64 `json:"auid"` Uid int64 `json:"uid"` Beid int64 `json:"beid"` // 應用id Ptyid int64 `json:"ptyid"` // 對應平臺 Username string `json:"username"` Mobile string `json:"mobile"` Nickname string `json:"nickname"` Openid string `json:"openid"` Avator string `json:"avator"` JwtToken}// 返回APPUsertype AppUser struct{ Uid int64 `json:"uid"` Auid int64 `json:"auid"` Beid int64 `json:"beid"` // 應用id Ptyid int64 `json:"ptyid"` // 對應平臺 Nickname string `json:"nickname"` Openid string `json:"openid"` Avator string `json:"avator"`}type LoginAppUser struct{ Uid int64 `json:"uid"` Auid int64 `json:"auid"` Beid int64 `json:"beid"` // 應用id Ptyid int64 `json:"ptyid"` // 對應平臺 Nickname string `json:"nickname"` Openid string `json:"openid"` Avator string `json:"avator"` JwtToken}type JwtToken struct { AccessToken string `json:"access_token,omitempty"` AccessExpire int64 `json:"access_expire,omitempty"` RefreshAfter int64 `json:"refresh_after,omitempty"`}type UserReq struct{ Auid int64 `json:"auid"` Uid int64 `json:"uid"` Beid int64 `json:"beid"` // 應用id Ptyid int64 `json:"ptyid"` // 對應平臺}type Request { Name string `path:"name,options=you|me"`}type Response { Message string `json:"message"`}@server( group: user)service user-api { @handler ping post /user/ping () @handler register post /user/register (RegisterReq) returns (UserReply) @handler login post /user/login (LoginReq) returns (UserReply) @handler wxlogin post /user/wx/login (WxLoginReq) returns (LoginAppUser) @handler code2Session get /user/wx/login () returns (LoginAppUser)}@server( jwt: Auth group: user middleware: Usercheck)service user-api { @handler userInfo get /user/dc/info (UserReq) returns (UserReply)}// 投票活動apitype Actid struct { Actid int64 `json:"actid"` //活動id}type VoteReq struct { Aeid int64 `json:"aeid"` // 作品id Actid}type VoteResp struct { VoteReq Votecount int64 `json:"votecount"` //投票票數 Viewcount int64 `json:"viewcount"` //瀏覽數}// 活動返回的引數type ActivityResp struct { Actid int64 `json:"actid"` Title string `json:"title"` //活動名稱 Descr string `json:"descr"` //活動描述 StartDate int64 `json:"start_date"` //活動時間 EnrollDate int64 `json:"enroll_date"` //投票時間 EndDate int64 `json:"end_date"` //活動結束時間 Votecount int64 `json:"votecount"` //當前活動的總票數 Viewcount int64 `json:"viewcount"` //當前活動的總瀏覽數 Type int64 `json:"type"` //投票方式 Num int64 `json:"num"` //投票幾票}//報名type EnrollReq struct { Actid Name string `json:"name"` // 名稱 Address string `json:"address"` //地址 Images []string `json:"images"` //作品圖片 Descr string `json:"descr"` // 作品描述}// 作品返回type EnrollResp struct { Actid Aeid int64 `json:"aeid"` // 作品id Name string `json:"name"` // 名稱 Address string `json:"address"` //地址 Images []string `json:"images"` //作品圖片 Descr string `json:"descr"` // 作品描述 Votecount int64 `json:"votecount"` //當前活動的總票數 Viewcount int64 `json:"viewcount"` //當前活動的總瀏覽數}@server( group: votes)service votes-api { @doc( summary: "獲取活動的資訊" ) @handler activityInfo get /votes/activity/info (Actid) returns (ActivityResp) @doc( summary: "活動訪問+1" ) @handler activityIcrView get /votes/activity/view (Actid) returns (ActivityResp) @doc( summary: "獲取報名的投票作品資訊" ) @handler enrollInfo get /votes/enroll/info (VoteReq) returns (EnrollResp) @doc( summary: "獲取報名的投票作品列表" ) @handler enrollLists get /votes/enroll/lists (Actid) returns(EnrollResp)}@server( jwt: Auth group: votes middleware: Usercheck)service votes-api { @doc( summary: "投票" ) @handler vote post /votes/vote (VoteReq) returns (VoteResp) @handler enroll post /votes/enroll (EnrollReq) returns (EnrollResp)}
上面基本上寫就寫的API及文件的思路
生成datacenter api服務➜ datacenter goctl api go -api datacenter.api -dir .Done.➜ datacenter tree.├── datacenter.api├── etc│ └── datacenter-api.yaml├── go.mod├── internal│ ├── config│ │ └── config.go│ ├── handler│ │ ├── common│ │ │ ├── appinfohandler.go│ │ │ ├── qiuniutokenhandler.go│ │ │ ├── snsinfohandler.go│ │ │ ├── votesverificationhandler.go│ │ │ └── wxtickethandler.go│ │ ├── routes.go│ │ ├── user│ │ │ ├── code2sessionhandler.go│ │ │ ├── loginhandler.go│ │ │ ├── pinghandler.go│ │ │ ├── registerhandler.go│ │ │ ├── userinfohandler.go│ │ │ └── wxloginhandler.go│ │ └── votes│ │ ├── activityicrviewhandler.go│ │ ├── activityinfohandler.go│ │ ├── enrollhandler.go│ │ ├── enrollinfohandler.go│ │ ├── enrolllistshandler.go│ │ └── votehandler.go│ ├── logic│ │ ├── common│ │ │ ├── appinfologic.go│ │ │ ├── qiuniutokenlogic.go│ │ │ ├── snsinfologic.go│ │ │ ├── votesverificationlogic.go│ │ │ └── wxticketlogic.go│ │ ├── user│ │ │ ├── code2sessionlogic.go│ │ │ ├── loginlogic.go│ │ │ ├── pinglogic.go│ │ │ ├── registerlogic.go│ │ │ ├── userinfologic.go│ │ │ └── wxloginlogic.go│ │ └── votes│ │ ├── activityicrviewlogic.go│ │ ├── activityinfologic.go│ │ ├── enrollinfologic.go│ │ ├── enrolllistslogic.go│ │ ├── enrolllogic.go│ │ └── votelogic.go│ ├── middleware│ │ └── usercheckmiddleware.go│ ├── svc│ │ └── servicecontext.go│ └── types│ └── types.go└── datacenter.go14 directories, 43 files
我們開啟 etc/datacenter-api.yaml
把必要的配置資訊加上
Name: datacenter-apiLog: Mode: consoleHost: 0.0.0.0Port: 8857Auth: AccessSecret: 你的jwtwon Secret AccessExpire: 86400CacheRedis:- Host: 127.0.0.1:6379 Pass: 密碼 Type: node UserRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: user.rpcCommonRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: common.rpcVotesRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: votes.rpc
上面的 UserRpc
, CommonRpc
,還有 VotesRpc
這些我先寫上,後面再來慢慢加。
我們先來寫 CommonRpc
服務。
➜ datacenter mkdir -p common/rpc && cd common/rpc
直接就新建在了,datacenter目錄中,因為common 裡面,可能以後會不只會提供rpc服務,可能還有api的服務,所以又加了rpc目錄
goctl建立模板➜ rpc goctl rpc template -o=common.proto➜ rpc lscommon.proto
往裡面填入內容:
➜ rpc cat common.protosyntax = "proto3";package common;message BaseAppReq{ int64 beid=1;}message BaseAppResp{ int64 beid=1; string logo=2; string sname=3; int64 isclose=4; string fullwebsite=5;}// 請求的apimessage AppConfigReq { int64 beid=1; int64 ptyid=2;}// 返回的值message AppConfigResp { int64 id=1; int64 beid=2; int64 ptyid=3; string appid=4; string appsecret=5; string title=6;}service Common { rpc GetAppConfig(AppConfigReq) returns(AppConfigResp); rpc GetBaseApp(BaseAppReq) returns(BaseAppResp);}
gotcl生成rpc服務➜ rpc goctl rpc proto -src common.proto -dir .protoc -I=/Users/jackluo/works/blogs/datacenter/common/rpc common.proto --go_out=plugins=grpc:/Users/jackluo/works/blogs/datacenter/common/rpc/commonDone.
➜ rpc tree.├── common│ └── common.pb.go├── common.go├── common.proto├── commonclient│ └── common.go├── etc│ └── common.yaml└── internal├── config│ └── config.go├── logic│ ├── getappconfiglogic.go│ └── getbaseapplogic.go├── server│ └── commonserver.go└── svc└── servicecontext.go8 directories, 10 files
基本上,就把所有的目錄規範和結構的東西都生成了,就不用糾結專案目錄了,怎麼放了,怎麼組織了。
看一下,配置資訊,裡面可以寫入mysql和其它redis的資訊:
Name: common.rpcListenOn: 127.0.0.1:8081Mysql: DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8&parseTime=true&loc=Asia%2FShanghaiCacheRedis:- Host: 127.0.0.1:6379 Pass: Type: node Etcd: Hosts: - 127.0.0.1:2379 Key: common.rpc
我們再來加上資料庫服務:
➜ rpc cd ..➜ common lsrpc➜ common pwd/Users/jackluo/works/blogs/datacenter/common➜ common goctl model mysql datasource -url="root:admin@tcp(127.0.0.1:3306)/datacenter" -table="base_app" -dir ./model -cDone.➜ common tree.├── model│ ├── baseappmodel.go│ └── vars.go└── rpc ├── common │ └── common.pb.go ├── common.go ├── common.proto ├── commonclient │ └── common.go ├── etc │ └── common.yaml └── internal ├── config │ └── config.go ├── logic │ ├── getappconfiglogic.go │ └── getbaseapplogic.go ├── server │ └── commonserver.go └── svc └── servicecontext.go10 directories, 12 files
這樣基本的一個 rpc
就寫完了,然後我們將rpc 和model 還有api串連起來,這個官方的文件已經很詳細了,這裡就只是貼一下程式碼:
➜ common cat rpc/internal/config/config.gopackage configimport ( "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/zrpc")type Config struct { zrpc.RpcServerConf Mysql struct { DataSource string } CacheRedis cache.ClusterConf}
再在svc中修改:
➜ common cat rpc/internal/svc/servicecontext.gopackage svcimport ( "datacenter/common/model" "datacenter/common/rpc/internal/config" "github.com/tal-tech/go-zero/core/stores/sqlx")type ServiceContext struct { c config.Config AppConfigModel model.AppConfigModel BaseAppModel model.BaseAppModel}func NewServiceContext(c config.Config) *ServiceContext { conn := sqlx.NewMysql(c.Mysql.DataSource) apm := model.NewAppConfigModel(conn, c.CacheRedis) bam := model.NewBaseAppModel(conn, c.CacheRedis) return &ServiceContext{ c: c, AppConfigModel: apm, BaseAppModel: bam, }}
上面的程式碼已經將 rpc
和 model
資料庫關聯起來了,我們現在再將 rpc
和 api
關聯起來:
➜ datacenter cat internal/config/config.gopackage configimport ( "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/rest" "github.com/tal-tech/go-zero/zrpc")type Config struct { rest.RestConf Auth struct { AccessSecret string AccessExpire int64 } UserRpc zrpc.RpcClientConf CommonRpc zrpc.RpcClientConf VotesRpc zrpc.RpcClientConf CacheRedis cache.ClusterConf}
加入 svc
服務中:
➜ datacenter cat internal/svc/servicecontext.gopackage svcimport ( "context" "datacenter/common/rpc/commonclient" "datacenter/internal/config" "datacenter/internal/middleware" "datacenter/shared" "datacenter/user/rpc/userclient" "datacenter/votes/rpc/votesclient" "fmt" "net/http" "time" "github.com/tal-tech/go-zero/core/logx" "github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/redis" "github.com/tal-tech/go-zero/core/syncx" "github.com/tal-tech/go-zero/rest" "github.com/tal-tech/go-zero/zrpc" "google.golang.org/grpc")type ServiceContext struct { Config config.Config GreetMiddleware1 rest.Middleware GreetMiddleware2 rest.Middleware Usercheck rest.Middleware UserRpc userclient.User //使用者 CommonRpc commonclient.Common VotesRpc votesclient.Votes Cache cache.Cache RedisConn *redis.Redis}func timeInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { stime := time.Now() err := invoker(ctx, method, req, reply, cc, opts...) if err != nil { return err } fmt.Printf("呼叫 %s 方法 耗時: %v\n", method, time.Now().Sub(stime)) return nil}func NewServiceContext(c config.Config) *ServiceContext { ur := userclient.NewUser(zrpc.MustNewClient(c.UserRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) cr := commonclient.NewCommon(zrpc.MustNewClient(c.CommonRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) vr := votesclient.NewVotes(zrpc.MustNewClient(c.VotesRpc, zrpc.WithUnaryClientInterceptor(timeInterceptor))) //快取 ca := cache.NewCache(c.CacheRedis, syncx.NewSharedCalls(), cache.NewCacheStat("dc"), shared.ErrNotFound) rcon := redis.NewRedis(c.CacheRedis[0].Host, c.CacheRedis[0].Type, c.CacheRedis[0].Pass) return &ServiceContext{ Config: c, GreetMiddleware1: greetMiddleware1, GreetMiddleware2: greetMiddleware2, Usercheck: middleware.NewUserCheckMiddleware().Handle, UserRpc: ur, CommonRpc: cr, VotesRpc: vr, Cache: ca, RedisConn: rcon, }}
這樣基本上,我們就可以在 logic
的檔案目錄中呼叫了:
cat internal/logic/common/appinfologic.gopackage logicimport ( "context" "datacenter/internal/svc" "datacenter/internal/types" "datacenter/shared" "datacenter/common/model" "datacenter/common/rpc/common" "github.com/tal-tech/go-zero/core/logx")type AppInfoLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext}func NewAppInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) AppInfoLogic { return AppInfoLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, }}func (l *AppInfoLogic) AppInfo(req types.Beid) (appconfig *common.BaseAppResp, err error) { //檢查 快取中是否有值 err = l.svcCtx.Cache.GetCache(model.GetcacheBaseAppIdPrefix(req.Beid), appconfig) if err != nil && err == shared.ErrNotFound { appconfig, err = l.svcCtx.CommonRpc.GetBaseApp(l.ctx, &common.BaseAppReq{ Beid: req.Beid, }) if err != nil { return } err = l.svcCtx.Cache.SetCache(model.GetcacheBaseAppIdPrefix(req.Beid), appconfig) } return}
這樣,基本就連線起來了,其它基本上就不用改了,UserRPC
, VotesRPC
類似,這裡就不在寫了。
go-zero
的確香,因為它有一個 goctl
的工具,他可以自動的把程式碼結構全部的生成好,我們就不再去糾結,目錄結構 ,怎麼組織,沒有個好幾年的架構能力是不好實現的,有什麼規範那些,併發,熔斷,完全不用,考濾其它的,專心的實現業務就好,像微服務,還要有服務發現,一系列的東西,都不用關心,因為 go-zero
內部已經實現了。
我寫程式碼也寫了有10多年了,之前一直用的 php,比較出名的就 laravel,thinkphp,基本上就是模組化的,像微服那些實現直來真的有成本,但是你用上go-zero,你就像調api介面一樣簡單的開發,其它什麼服務發現,那些根本就不用關注了,只需要關注業務。
一個好的語言,框架,他們的底層思維,永遠都是效率高,不加班的思想,我相信go-zero會提高你和你團隊或是公司的效率。go-zero的作者說,他們有個團隊專門整理go-zero框架,目的也應該很明顯,那就是提高,他們自己的開發效率,流程化,標準化,是提高工作效率的準則,像我們平時遇到了問題,或是遇到了bug,我第一個想到的不是怎麼去解決我的bug,而是在想我的流程是不是有問題,我的哪個流程會導致bug,最後我相信 go-zero
能成為 微服務開發 的首選框架。
最後說說遇到的坑吧:
grpc
grpc
本人第一次用,然後就遇到了,有些字元為空時,欄位值不顯示的問題:
透過 grpc
官方庫中的 jsonpb
來實現,官方在它的設定中有一個結構體用來實現 protoc buffer
轉換為JSON結構,並可以根據欄位來配置轉換的要求。
go-zero
中設定了,感覺沒有效果,大佬說透過nginx 設定,後面發現還是不行,最近強行弄到了一個域名下,後面有時間再解決。
sqlx
go-zero
的 sqlx
問題,這個真的費了很長的時間:
time.Time
這個資料結構,資料庫中用的是 timestamp 這個 比如我的欄位 是delete_at 預設數庫設定的是null ,結果插入的時候,就報了 Incorrect datetime value: '0000-00-00' for column 'deleted_at' at row 1"}
這個錯,查詢的時候報 deleted_at\": unsupported Scan, storing driver.Value type \u003cnil\u003e into type *time.Time"
後面果斷去掉了這個欄位,欄位上面加上 .omitempty
這個標籤,好像也有用,db:".omitempty"
其次就是這個 Conversion from collation utf8_general_ci into utf8mb4_unicode_ci
,這個導致的大概原因是,現在都喜歡用emj表情了,mysql資料識別不了。
mysql
這邊照樣按照原始的方式,將配置檔案修改編碼格式,重新建立資料庫,並且設定資料庫編碼為utf8mb4,排序規則為 utf8mb4_unicode_ci
。
這樣的話,所有的表還有string欄位都是這個編碼格式,如果不想所有的都是,可以單獨設定,這個不是重點.因為在navicat上都好設定,手動點一下就行了。
重點來了:golang中使用的是 github.com/go-sql-driver/mysql
驅動,將連線 mysql
的 dsn
(因為我這使用的是gorm,所以dsn可能跟原生的格式不太一樣,不過沒關係, 只需要關注 charset
和 collation
就行了)
root:password@/name?parseTime=True&loc=Local&charset=utf8
修改為:root:password@/name?parseTime=True&loc=Local&charset=utf8mb4&collation=utf8mb4_unicode_ci