好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > 如何用go-zero 實現中臺系統

如何用go-zero 實現中臺系統

熱門標簽:400開頭電話怎樣申請 官渡電銷外呼管理系統怎么收費 貴州電話智能外呼系統 谷歌美發店地圖標注 赤峰電銷 杭州人工智能電銷機器人費用 江蘇呼叫中心外呼系統有效果嗎 地圖區域圖標注后導出 利用地圖標注位置

最近發現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: "中臺系統"http:// TODO: add title
  desc: "中臺系統"http:// 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
➜ rpc ls
common.proto

往里面填入內容

➜ 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;
}
//請求的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服務

➜ 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

基本上,就把所有的目錄規范和結構的東西都生成了,就不用糾結項目目錄了,怎么放了,怎么組織了

看一下,配置信息,里面可以寫入mysql和其它redis的信息

Name: common.rpc
ListenOn: 127.0.0.1:8081
Mysql:
 DataSource: root:admin@tcp(127.0.0.1:3306)/datacenter?charset=utf8parseTime=trueloc=Asia%2FShanghai
CacheRedis:
- Host: 127.0.0.1:6379
 Pass:
 Type: node 
Etcd:
 Hosts:
 - 127.0.0.1:2379
 Key: common.rpc

我們再來加上數據庫的服務

➜ 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

這樣基本的一個rpc就寫完了,然后我們將rpc 和model 還有api串連起來,這個官方的文檔已經很詳細了,這里就只是貼一下代碼

➜ 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,
  }
}

上面的代碼已經將rpc 和 model 數據庫關聯起來了,我們現在再將rpc 和 api關聯起來

➜ 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服務中

➜ 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("調用 %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.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 的確香,因為它有一個goctl 的工具,他可以自動的把代碼結構全部的生成好,我們就不再去糾結,目錄結構 ,怎么組織,沒有個好幾年的架構能力是不好實現的,有什么規范那些,并發,熔斷,完全不用,考濾其它的,專心的實現業務就好,像微服務,還要有服務發現,一系列的東西,都不用關心,因為go-zero內部已經實現了,我寫代碼也寫了有10多年了,之前一直用的php,比較出名的就 laravel,thinkphp,基本上就是模塊化的,像微服那些實現直來真的有成本,但是你用上go-zero,你就像調api接口一樣簡單的開發,其它什么服務發現,那些根本就不用關注了,只需要關注業務。一個好的語言,框架,他們的底層思維,永遠都是效率高,不加班的思想,我相信go-zero會提高你和你團隊或是公司的效率。go-zero的作者說,他們有個團隊專門整理go-zero框架,目的也應該很明顯,那就是提高,他們自己的開發效率,流程化,標準化,是提高工作效率的準則,像我們平時遇到了問題,或是遇到了bug,我第一個想到的不是怎么去解決我的bug,而是在想我的流程是不是有問題,我的哪個流程會導致bug,最后我相信go-zero 能成為 微服務開發 的首選框架

最后說說遇到的坑吧:

grpc 本人第一次用,然后就遇到了,有些字符為空時,字段值不顯示的問題:

通過grpc官方庫中的jsonpb來實現,官方在它的設定中有一個結構體用來實現protoc buffer轉換為JSON結構,并可以根據字段來配置轉換的要求

跨域問題:

go-zero的設置了,感覺沒有效果,大佬說通過nginx 設置,后面發現還是不行,最近 ,強行弄到了一個域名下,后面有時間再解決

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=Trueloc=Localcharset=utf8
修改為:
root:password@/name?parseTime=Trueloc=Localcharset=utf8mb4collation=utf8mb4_unicode_ci

---------------------------------

mark

到此這篇關于如何用go-zero 實現中臺系統的文章就介紹到這了,更多相關go-zero中臺系統 內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 利用go-zero在Go中快速實現JWT認證的步驟詳解
  • go-zero 應對海量定時/延遲任務的技巧

標簽:黔西 松原 鷹潭 河池 保定 武漢 宜春 泰安

巨人網絡通訊聲明:本文標題《如何用go-zero 實現中臺系統》,本文關鍵詞  如,何用,go-zero,實現,中臺,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《如何用go-zero 實現中臺系統》相關的同類信息!
  • 本頁收集關于如何用go-zero 實現中臺系統的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 色哺乳XXXXHD国产| 搡逼| 久久久久久69精品久久久学生| 高h各种姿势调教np| 国产老女人| 亚洲精品成人区在线观看| 黄色软件大全app| 少妇开后菊痛哭哀求| 中文字幕avv| 好男人好资源视频高清| 欧美另类Z0Z??X在WWE| 亚洲AV秘?无码一区桃乃| 91自产国偷拍在线| 美女隐私视频在线观看| 成+人+黄+色+免费观看| 淑女的欲望电影| 坐他脸上磨b| 情趣刺激蕾丝透明女性睡衣| 亚洲欧美日韩专区一| 女领导脱了内裤让我添| 国产AV一区二区三区色在线看| 撕开丝袜裆部啪啪| 92午夜免费福利757永久| 亚欧色| 51区亚洲精品一区二区三区| 欧美侏儒A片| 啊啊啊嗯嗯用力| 美女gif跪趴式抽搐动态图| 国产丶欧美丶日韩丶不卡影视| 嗯~好大?用力?深一点孕妇| 国产在线欧美精品卡通动漫| 炮灰他不想回豪门(穿书)| 小妇人电影免费完整观看2021| 国产91精品秘?入口17c| 好妞操| 狠狠色噜噜狠狠狠97影音先锋| 啊啊好多水| 九九久久精品无码专区免费| 西西人体大胆4444w?w张筱雨| 一区国产视频| 女人解开乳罩给男人吃奶|