溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)

發(fā)布時(shí)間:2021-10-21 13:52:04 來源:億速云 閱讀:320 作者:iii 欄目:編程語言

本篇內(nèi)容主要講解“怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)”吧!

先聊聊中臺(tái)架構(gòu)思路吧:

怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)

中臺(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,這樣就寫一套。


最后說說實(shí)現(xiàn)吧,整個(gè)就一個(gè)repo:

  • 網(wǎng)關(guān),我們用的是: go-zero的Api服務(wù)

  • 其它它的是服務(wù),我們就是用的go-zero的rpc服務(wù)

看下目錄結(jié)構(gòu)

怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)

整個(gè)項(xiàng)目完成,我一個(gè)人操刀, 寫了1個(gè)來星期,我就實(shí)現(xiàn)了上面的中臺(tái)系統(tǒng)。

datacenter-api服務(wù)

先看官方文檔 https://www.yuque.com/tal-tech/go-zero/yaoehb

我們先把網(wǎng)關(guān)搭建起來

? 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)"
    desc: "中臺(tái)系統(tǒng)"
    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ù)。

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)將 rpcmodel 數(shù)據(jù)庫關(guān)聯(lián)起來了,我們現(xiàn)在再將 rpcapi 關(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
}

這樣,基本就連接起來了,其它基本上就不用改了,UserRPCVotesRPC 類似,這里就不在寫了。

使用心得

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)?go-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

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í)間再解決。

  • sqlx

go-zerosqlx 問題,這個(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í)別不了。

  • 數(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),將連接 mysqldsn(因?yàn)槲疫@使用的是gorm,所以dsn可能跟原生的格式不太一樣,不過沒關(guān)系, 只需要關(guān)注 charsetcollation 就行了)

root:password@/name?parseTime=True&loc=Local&charset=utf8 修改為: root:password@/name?parseTime=True&loc=Local&charset=utf8mb4&collation=utf8mb4_unicode_ci

到此,相信大家對(duì)“怎么用go-zero實(shí)現(xiàn)一個(gè)中臺(tái)系統(tǒng)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI