您现在的位置是:网站首页> 编程资料编程资料
如何用go-zero 实现中台系统_Golang_
2023-05-26
361人已围观
简介 如何用go-zero 实现中台系统_Golang_
最近发现golang社区里出了一个新星的微服务框架,来自好未来,光看这个名字,就很有奔头,之前,也只是玩过go-micro,其实真正的还没有在项目中运用过,只是觉得 微服务,grpc 这些很高大尚,还没有在项目中,真正的玩过,我看了一下官方提供的工具真的很好用,只需要定义好,舒适文件jia结构 都生成了,只需要关心业务,
加上最近 有个投票的活动,加上最近这几年中台也比较火,所以决定玩一下,
先聊聊中台架构思路吧,look 先看架

中台的概念大概就是把一个一个的app 统一起来,反正我是这样理解的
先聊用户服务吧,现在一个公司有很多的公众号,小程序,微信的,支付宝的,还有xxx xxx ,很多的平台,每次开发的时候,我们总是需要做用户登陆的服务,不停的复制代码,然后我们就在思考能不能有一套独立的用户服务,
只需要告诉我你需要传个你要登陆的平台(比如微信),微信登陆,需要的是客户端返回给服务端一个code ,然后服务端拿着这个code去微信获取用户信息,反正大家都明白,
我们决定,将所有的信息 弄到 配置公共服务中去,里面在存,微信,支付宝,以及其它平台的 appid ,appkey,还有支付的appid,appkey,
这样就写一套
--------------------------------------------------------------------------------------------
go-zerio: https://github.com/tal-tech/go-zero
最后说说实现吧,整个就一个repo
网关,我们用的是: go-zero的Api服务
其它它的是服务,我们就是用的go-zero的rpc服务
看下目录结构

整个项目完成,我一个人操刀, 写了1个来星期,我就实现了上面的中台系统;
go-zero作者私聊我说,可不可以写得丰富点,所以我决定把我的源码也加到文章里面
先看官方文档https://www.yuque.com/tal-tech/go-zero/yaoehb
我们先把网关搭建起来
创建datacenter-api服务
➜ 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
二、创建api文件
➜ datacenter goctl api -o datacenter.api Done. ➜ datacenter tree . ├── datacenter.api └── go.mod
三、定义api服务 分别包含了上面的 公共服务,用户服务,和 投票活动服务
info( title: "中台系统"// TODO: add title desc: "中台系统"// TODO: add description 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 } //返回APPUser type 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) } // 投票活动api type 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.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的服务
新建项目目录
➜ datacenter mkdir -p common/rpc && cd common/rpc
直接就新建在了,datacenter目录中,因为common 里面,可能以后会不只会提供rpc服务,可能还有api的服务,所以又加了rpc目录
goctl创建模板
➜ rpc goctl rpc template -o=common.proto ➜ r
相关内容
- golang 将[]byte转成16进制的实现_Golang_
- Golang Socket Server自定义协议的简单实现方案_Golang_
- 使用go读取gzip格式的压缩包的操作_Golang_
- go判断文件夹是否存在并创建的实例_Golang_
- go 协程返回值处理操作_Golang_
- golang实现的文件上传下载小工具_Golang_
- Golang 实现复制文件夹同时复制文件_Golang_
- golang文件读取-按指定BUFF大小读取方式_Golang_
- golang 如何获取pem格式RSA公私钥长度_Golang_
- golang 64位linux环境下编译出32位程序操作_Golang_
点击排行
本栏推荐
