作者:Jack
最近发现golang社区里出了一个新星的微服务框架,来自好将来,光看这个名字,就很有奔头,之前,也只是玩过go-micro,其实真正的还没有在我的项目中使用过,只是感觉 微服务,grpc 这些很高大尚,还没有在我的项目中,真正的玩过,我看了一下官网提供的工具真的很好用,只须要定义好,舒服文件jia构造 都生成了,只须要关怀业务,加上最近 有个投票的流动,加上最近这几年中台也比拟火,所以决定玩一下,
开源地址: https://github.com/jackluo201...
先聊聊中台架构思路吧:
中台的概念大略就是把一个一个的app 对立起来,反正我是这样了解的。
先聊用户服务吧,当初一个公司有很多的公众号,小程序,微信的,支付宝的,还有xxx xxx ,很多的平台,每次开发的时候,咱们总是须要做用户登陆的服务,不停的复制代码,而后咱们就在思考能不能有一套独立的用户服务,只须要通知我你须要传个你要登陆的平台(比方微信),微信登陆,须要的是客户端返回给服务端一个code ,而后服务端拿着这个code去微信获取用户信息,反正大家都明确。
咱们决定,将所有的信息 弄到 配置公共服务中去,外面在存,微信,支付宝,以及其它平台的 appid ,appkey,还有领取的appid,appkey,这样就写一套。
最初说说实现吧,整个就一个repo:
- 网关,咱们用的是: go-zero的Api服务
- 其它它的是服务,咱们就是用的go-zero的rpc服务
看下目录构造
整个我的项目实现,我一个人操刀, 写了1个来星期,我就实现了下面的中台零碎。
datacenter-api服务
先看官网文档 https://www.yuque.com/tal-tec...
咱们先把网关搭建起来
➜ 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: "net.webjoy@gmail.com")// 获取 利用信息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
服务。
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
go-zero 我的项目地址
https://github.com/tal-tech/go-zero
欢送点赞????
我的项目地址:
https://github.com/tal-tech/go-zero