如何用go-zero 實(shí)現(xiàn)中臺(tái)系統(tǒng)
最近發(fā)現(xiàn)golang社區(qū)里出了一個(gè)新星的微服務(wù)框架,來自好未來,光看這個(gè)名字,就很有奔頭,之前,也只是玩過go-micro,其實(shí)真正的還沒有在項(xiàng)目中運(yùn)用過,只是覺得 微服務(wù),grpc 這些很高大尚,還沒有在項(xiàng)目中,真正的玩過,我看了一下官方提供的工具真的很好用,只需要定義好,舒適文件jia結(jié)構(gòu) 都生成了,只需要關(guān)心業(yè)務(wù),
加上最近 有個(gè)投票的活動(dòng),加上最近這幾年中臺(tái)也比較火,所以決定玩一下,
先聊聊中臺(tái)架構(gòu)思路吧,look 先看架
中臺(tái)的概念大概就是把一個(gè)一個(gè)的app 統(tǒng)一起來,反正我是這樣理解的
先聊用戶服務(wù)吧,現(xiàn)在一個(gè)公司有很多的公眾號(hào),小程序,微信的,支付寶的,還有xxx xxx ,很多的平臺(tái),每次開發(fā)的時(shí)候,我們總是需要做用戶登陸的服務(wù),不停的復(fù)制代碼,然后我們就在思考能不能有一套獨(dú)立的用戶服務(wù),
只需要告訴我你需要傳個(gè)你要登陸的平臺(tái)(比如微信),微信登陸,需要的是客戶端返回給服務(wù)端一個(gè)code ,然后服務(wù)端拿著這個(gè)code去微信獲取用戶信息,反正大家都明白,
我們決定,將所有的信息 弄到 配置公共服務(wù)中去,里面在存,微信,支付寶,以及其它平臺(tái)的 appid ,appkey,還有支付的appid,appkey,
這樣就寫一套
--------------------------------------------------------------------------------------------
go-zerio: https://github.com/tal-tech/go-zero
最后說說實(shí)現(xiàn)吧,整個(gè)就一個(gè)repo
網(wǎng)關(guān),我們用的是: go-zero的Api服務(wù)
其它它的是服務(wù),我們就是用的go-zero的rpc服務(wù)
看下目錄結(jié)構(gòu)
整個(gè)項(xiàng)目完成,我一個(gè)人操刀, 寫了1個(gè)來星期,我就實(shí)現(xiàn)了上面的中臺(tái)系統(tǒng);
go-zero作者私聊我說,可不可以寫得豐富點(diǎn),所以我決定把我的源碼也加到文章里面
先看官方文檔https://www.yuque.com/tal-tech/go-zero/yaoehb
我們先把網(wǎng)關(guān)搭建起來
創(chuàng)建datacenter-api服務(wù)
➜ blogs mkdir datacenter && cd datacenter
➜ datacenter go mod init datacenter
go: creating new go.mod: module datacenter
➜ datacenter
查看book目錄
➜ datacenter tree . └── go.mod 0 directories, 1 file
二、創(chuàng)建api文件
➜ datacenter goctl api -o datacenter.api Done. ➜ datacenter tree . ├── datacenter.api └── go.mod
三、定義api服務(wù) 分別包含了上面的 公共服務(wù),用戶服務(wù),和 投票活動(dòng)服務(wù)
info( title: "中臺(tái)系統(tǒng)"http:// TODO: add title desc: "中臺(tái)系統(tǒng)"http:// TODO: add description author: "jackluo" email: "net.webjoy@gmail.com" ) //獲取 應(yīng)用信息 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"` //是否關(guān)閉 Fullwebsite string `json:"fullwebsite"` // 全站名稱 } type SnsReq struct{ Beid Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) BackUrl string `json:"back_url"` //登陸返回的地址 } type SnsResp struct{ Beid Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) Appid string `json:"appid"` //sns 平臺(tái)的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: "獲取站點(diǎn)的信息" ) @handler votesVerification get /MP_verify_NT04cqknJe0em3mT.txt (SnsReq) returns (SnsResp) @handler appInfo get /common/appinfo (Beid) returns (Application) @doc( summary: "獲取站點(diǎn)的社交屬性信息" ) @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) } //注冊(cè)請(qǐng)求 type RegisterReq struct { // TODO: add members here and delete this comment Mobile string `json:"mobile"` //基本一個(gè)手機(jī)號(hào)碼就完事 Password string `json:"password"` Smscode string `json:"smscode"` //短信碼 } //登陸請(qǐng)求 type LoginReq struct{ Mobile string `json:"mobile"` Type int64 `json:"type"` //1.密碼登陸,2.短信登陸 Password string `json:"password"` } //微信登陸 type WxLoginReq struct { Beid int64 `json:"beid"` //應(yīng)用id Code string `json:"code"` //微信登陸密鑰 Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) } //返回用戶信息 type UserReply struct { Auid int64 `json:"auid"` Uid int64 `json:"uid"` Beid int64 `json:"beid"` //應(yīng)用id Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) Username string `json:"username"` Mobile string `json:"mobile"` Nickname string `json:"nickname"` Openid string `json:"openid"` Avator string `json:"avator"` JwtToken } //返回APPUser type AppUser struct{ Uid int64 `json:"uid"` Auid int64 `json:"auid"` Beid int64 `json:"beid"` //應(yīng)用id Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) 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"` //應(yīng)用id Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) 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"` //應(yīng)用id Ptyid int64 `json:"ptyid"` //對(duì)應(yīng)平臺(tái) } 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) } // 投票活動(dòng)api type Actid struct { Actid int64 `json:"actid"` //活動(dòng)id } type VoteReq struct { Aeid int64 `json:"aeid"` // 作品id Actid } type VoteResp struct { VoteReq Votecount int64 `json:"votecount"` //投票票數(shù) Viewcount int64 `json:"viewcount"` //瀏覽數(shù) } // 活動(dòng)返回的參數(shù) type ActivityResp struct { Actid int64 `json:"actid"` Title string `json:"title"` //活動(dòng)名稱 Descr string `json:"descr"` //活動(dòng)描述 StartDate int64 `json:"start_date"` //活動(dòng)時(shí)間 EnrollDate int64 `json:"enroll_date"` //投票時(shí)間 EndDate int64 `json:"end_date"` //活動(dòng)結(jié)束時(shí)間 Votecount int64 `json:"votecount"` //當(dāng)前活動(dòng)的總票數(shù) Viewcount int64 `json:"viewcount"` //當(dāng)前活動(dòng)的總瀏覽數(shù) Type int64 `json:"type"` //投票方式 Num int64 `json:"num"` //投票幾票 } //報(bào)名 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"` //當(dāng)前活動(dòng)的總票數(shù) Viewcount int64 `json:"viewcount"` //當(dāng)前活動(dòng)的總瀏覽數(shù) } @server( group: votes ) service votes-api { @doc( summary: "獲取活動(dòng)的信息" ) @handler activityInfo get /votes/activity/info (Actid) returns (ActivityResp) @doc( summary: "活動(dòng)訪問+1" ) @handler activityIcrView get /votes/activity/view (Actid) returns (ActivityResp) @doc( summary: "獲取報(bào)名的投票作品信息" ) @handler enrollInfo get /votes/enroll/info (VoteReq) returns (EnrollResp) @doc( summary: "獲取報(bào)名的投票作品列表" ) @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服務(wù)
➜ 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.go 14 directories, 43 files
我們打開etc/datacenter-api.yaml 把必要的配置信息加上
Name: datacenter-api Log: Mode: console Host: 0.0.0.0 Port: 8857 Auth: AccessSecret: 你的jwtwon Secret AccessExpire: 86400 CacheRedis: - Host: 127.0.0.1:6379 Pass: 密碼 Type: node UserRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: user.rpc CommonRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: common.rpc VotesRpc: Etcd: Hosts: - 127.0.0.1:2379 Key: votes.rpc
上面的UserRpc,和CommonRpc ,還有VotesRpc 這些我先寫上,后面再來慢慢加
我們先來寫CommonRpc的服務(wù)
新建項(xiàng)目目錄
➜ datacenter mkdir -p common/rpc && cd common/rpc
直接就新建在了,datacenter目錄中,因?yàn)閏ommon 里面,可能以后會(huì)不只會(huì)提供rpc服務(wù),可能還有api的服務(wù),所以又加了rpc目錄
goctl創(chuàng)建模板
➜ rpc goctl rpc template -o=common.proto ➜ rpc ls common.proto
往里面填入內(nèi)容
➜ rpc cat common.proto syntax = "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; } //請(qǐng)求的api message 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服務(wù)
➜ 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/common Done.
➜ 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.go 8 directories, 10 files
基本上,就把所有的目錄規(guī)范和結(jié)構(gòu)的東西都生成了,就不用糾結(jié)項(xiàng)目目錄了,怎么放了,怎么組織了
看一下,配置信息,里面可以寫入mysql和其它redis的信息
Name: common.rpc ListenOn: 127.0.0.1:8081 Mysql: DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8&parseTime=true&loc=Asia%2FShanghai CacheRedis: - Host: 127.0.0.1:6379 Pass: Type: node Etcd: Hosts: - 127.0.0.1:2379 Key: common.rpc
我們?cè)賮砑由蠑?shù)據(jù)庫的服務(wù)
➜ rpc cd .. ➜ common ls rpc ➜ 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 -c Done. ➜ 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.go 10 directories, 12 files
這樣基本的一個(gè)rpc就寫完了,然后我們將rpc 和model 還有api串連起來,這個(gè)官方的文檔已經(jīng)很詳細(xì)了,這里就只是貼一下代碼
➜ common cat rpc/internal/config/config.go package config import ( "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.go package svc import ( "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, } }
上面的代碼已經(jīng)將rpc 和 model 數(shù)據(jù)庫關(guān)聯(lián)起來了,我們現(xiàn)在再將rpc 和 api關(guān)聯(lián)起來
➜ datacenter cat internal/config/config.go package config import ( "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服務(wù)中
➜ datacenter cat internal/svc/servicecontext.go package svc import ( "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("調(diào)用 %s 方法 耗時(shí): %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的文件目錄中調(diào)用了
cat internal/logic/common/appinfologic.go package logic import ( "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 的確香,因?yàn)樗幸粋€(gè)goctl 的工具,他可以自動(dòng)的把代碼結(jié)構(gòu)全部的生成好,我們就不再去糾結(jié),目錄結(jié)構(gòu) ,怎么組織,沒有個(gè)好幾年的架構(gòu)能力是不好實(shí)現(xiàn)的,有什么規(guī)范那些,并發(fā),熔斷,完全不用,考濾其它的,專心的實(shí)現(xiàn)業(yè)務(wù)就好,像微服務(wù),還要有服務(wù)發(fā)現(xiàn),一系列的東西,都不用關(guān)心,因?yàn)間o-zero內(nèi)部已經(jīng)實(shí)現(xiàn)了,我寫代碼也寫了有10多年了,之前一直用的php,比較出名的就 laravel,thinkphp,基本上就是模塊化的,像微服那些實(shí)現(xiàn)直來真的有成本,但是你用上go-zero,你就像調(diào)api接口一樣簡(jiǎn)單的開發(fā),其它什么服務(wù)發(fā)現(xiàn),那些根本就不用關(guān)注了,只需要關(guān)注業(yè)務(wù)。一個(gè)好的語言,框架,他們的底層思維,永遠(yuǎn)都是效率高,不加班的思想,我相信go-zero會(huì)提高你和你團(tuán)隊(duì)或是公司的效率。go-zero的作者說,他們有個(gè)團(tuán)隊(duì)專門整理go-zero框架,目的也應(yīng)該很明顯,那就是提高,他們自己的開發(fā)效率,流程化,標(biāo)準(zhǔn)化,是提高工作效率的準(zhǔn)則,像我們平時(shí)遇到了問題,或是遇到了bug,我第一個(gè)想到的不是怎么去解決我的bug,而是在想我的流程是不是有問題,我的哪個(gè)流程會(huì)導(dǎo)致bug,最后我相信go-zero 能成為 微服務(wù)開發(fā) 的首選框架
最后說說遇到的坑吧:
grpc 本人第一次用,然后就遇到了,有些字符為空時(shí),字段值不顯示的問題:
通過grpc官方庫中的jsonpb
來實(shí)現(xiàn),官方在它的設(shè)定中有一個(gè)結(jié)構(gòu)體用來實(shí)現(xiàn)protoc buffer
轉(zhuǎn)換為JSON結(jié)構(gòu),并可以根據(jù)字段來配置轉(zhuǎn)換的要求
跨域問題:
go-zero的設(shè)置了,感覺沒有效果,大佬說通過nginx 設(shè)置,后面發(fā)現(xiàn)還是不行,最近 ,強(qiáng)行弄到了一個(gè)域名下,后面有時(shí)間再解決
go-zero的sqlx 問題,這個(gè)真的費(fèi)了很長(zhǎng)的時(shí)間,
time.Time 這個(gè)數(shù)據(jù)結(jié)構(gòu),數(shù)據(jù)庫中用的是 timestamp 這個(gè) 比如我的字段 是delete_at 默認(rèn)數(shù)庫設(shè)置的是null ,結(jié)果插入的時(shí)候,
就報(bào)了Incorrect datetime value: '0000-00-00' for column 'deleted_at' at row 1"}這個(gè)錯(cuò),
查詢的時(shí)候報(bào)deleted_at\": unsupported Scan, storing driver.Value type \u003cnil\u003e into type *time.Time"
后面果斷去掉了這個(gè)字段
字段上面加上 .omitempty 這個(gè)標(biāo)簽,好像也有用,`db:".omitempty"`
其次就是這個(gè)Conversion from collation utf8_general_ci into utf8mb4_unicode_ci,這個(gè)導(dǎo)致的大概原因是,現(xiàn)在都喜歡用emj表情了,mysql數(shù)據(jù)識(shí)別不了
最后發(fā)現(xiàn)是數(shù)據(jù)連接問題:
mysql這邊照樣按照原始的方式,將配置文件修改編碼格式
重新創(chuàng)建數(shù)據(jù)庫,并且設(shè)置數(shù)據(jù)庫編碼為utf8mb4 ,排序規(guī)則為utf8mb4_unicode_ci
(這樣的話,所有的表還有string字段都是這個(gè)編碼格式,如果不想所有的都是,可以單獨(dú)設(shè)置,這個(gè)不是重點(diǎn).因?yàn)樵趎avicat上都好設(shè)置,手動(dòng)點(diǎn)一下就行了)
重點(diǎn)來了:golang中使用的是 github.com/go-sql-driver/mysql驅(qū)動(dòng),
將連接mysql的dsn:(因?yàn)槲疫@使用的是gorm,所以dsn可能跟原生的格式不太一樣,不過沒關(guān)系,只需要關(guān)注charset和collation就行了)
root:password@/name?parseTime=True&loc=Local&charset=utf8
修改為:
root:password@/name?parseTime=True&loc=Local&charset=utf8mb4&collation=utf8mb4_unicode_ci
---------------------------------
mark
到此這篇關(guān)于如何用go-zero 實(shí)現(xiàn)中臺(tái)系統(tǒng)的文章就介紹到這了,更多相關(guān)go-zero中臺(tái)系統(tǒng) 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
xorm根據(jù)數(shù)據(jù)庫生成go model文件的操作
這篇文章主要介紹了xorm根據(jù)數(shù)據(jù)庫生成go model文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12Go語言獲取系統(tǒng)性能數(shù)據(jù)gopsutil庫的操作
這篇文章主要介紹了Go語言獲取系統(tǒng)性能數(shù)據(jù)gopsutil庫的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12go語言限制協(xié)程并發(fā)數(shù)的方案詳情
一個(gè)線程中可以有任意多個(gè)協(xié)程,但某一時(shí)刻只能有一個(gè)協(xié)程在運(yùn)行,多個(gè)協(xié)程分享該線程分配到的計(jì)算機(jī)資源,接下來通過本文給大家介紹go語言限制協(xié)程的并發(fā)數(shù)的方案詳情,感興趣的朋友一起看看吧2022-01-01Go語言基礎(chǔ)for循環(huán)語句的用法及示例詳解
這篇文章主要為大家介紹了Go語言基礎(chǔ)for循環(huán)語句的用法及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Go語言中實(shí)現(xiàn)打印堆棧的errors包的用法詳解
因?yàn)镚o語言提供的錯(cuò)誤太簡(jiǎn)單了,以至于簡(jiǎn)單的我們無法更好的處理問題,所以誕生了很多對(duì)錯(cuò)誤處理的庫,github.com/pkg/errors是比較簡(jiǎn)潔的一樣,本文就來聊聊它的具體用法吧2023-07-07