快速構建高併發微服務

kevwan發表於2020-08-29

0. 為什麼說做好微服務很難?

要想做好微服務,我們需要理解和掌握的知識點非常多,從幾個維度上來說:

  • 基本功能層面

    1. 併發控制&限流,避免服務被突發流量擊垮
    2. 服務註冊與服務發現,確保能夠動態偵測增減的節點
    3. 負載均衡,需要根據節點承受能力分發流量
    4. 超時控制,避免對已超時請求做無用功
    5. 熔斷設計,快速失敗,保障故障節點的恢復能力
  • 高階功能層面

    1. 請求認證,確保每個使用者只能訪問自己的資料
    2. 鏈路追蹤,用於理解整個系統和快速定位特定請求的問題
    3. 日誌,用於資料收集和問題定位
    4. 可觀測性,沒有度量就沒有優化

對於其中每一點,我們都需要用很長的篇幅來講述其原理和實現,那麼對我們後端開發者來說,要想把這些知識點都掌握並落實到業務系統裡,難度是非常大的,不過我們可以依賴已經被大流量驗證過的框架體系。go-zero 微服務框架就是為此而生。

另外,我們始終秉承工具大於約定和文件的理念。我們希望儘可能減少開發人員的心智負擔,把精力都投入到產生業務價值的程式碼上,減少重複程式碼的編寫,所以我們開發了goctl工具。

下面我通過短鏈微服務來演示通過go-zero快速的建立微服務的流程,走完一遍,你就會發現:原來編寫微服務如此簡單!

1. 什麼是短鏈服務?

短鏈服務就是將長的 URL 網址,通過程式計算等方式,轉換為簡短的網址字串。

寫此短鏈服務是為了從整體上演示 go-zero 構建完整微服務的過程,演算法和實現細節儘可能簡化了,所以這不是一個高階的短鏈服務。

2. 短鏈微服務架構圖

架構圖

  • 這裡把 shorten 和 expand 分開為兩個微服務,並不是說一個遠端呼叫就需要拆分為一個微服務,只是為了最簡演示多個微服務而已
  • 後面的 redis 和 mysql 也是共用的,但是在真正專案裡要儘可能每個微服務使用自己的資料庫,資料邊界要清晰

3. 準備工作

  • 安裝 etcd, mysql, redis
  • 準備 goctl 工具
  • 直接從https://github.com/tal-tech/go-zero/releases下載最新版,後續會加上自動更新
    • 也可以從原始碼編譯,在任意目錄下進行,目的是為了編譯 goctl 工具

1. git clone https://github.com/tal-tech/go-zero 2. 在tools/goctl目錄下編譯 goctl 工具go build goctl.go 3. 將生成的 goctl 放到$PATH下,確保 goctl 命令可執行

  • 建立工作目錄shorturl
  • shorturl目錄下執行go mod init shorturl初始化go.mod

4. 編寫 API Gateway 程式碼

  • 通過 goctl 生成shorturl.api並編輯,為了簡潔,去除了檔案開頭的info,程式碼如下:
type (
  shortenReq struct {
      url string `form:"url"`
  }

  shortenResp struct {
      shortUrl string `json:"shortUrl"`
  }
)

type (
  expandReq struct {
      key string `form:"key"`
  }

  expandResp struct {
      url string `json:"url"`
  }
)

service shorturl-api {
  @server(
      handler: ShortenHandler
  )
  get /shorten(shortenReq) returns(shortenResp)

  @server(
      handler: ExpandHandler
  )
  get /expand(expandReq) returns(expandResp)
}

type 用法和 go 一致,service 用來定義 get/post/head/delete 等 api 請求,解釋如下:

  • service shorturl-api {這一行定義了 service 名字
  • @server部分用來定義 server 端用到的屬性
  • handler定義了服務端 handler 名字
  • get /shorten(shortenReq) returns(shortenResp)定義了 get 方法的路由、請求引數、返回引數等

    • 使用 goctl 生成 API Gateway 程式碼
goctl api go -api shorturl.api -dir api

生成的檔案結構如下:

.
├── api
│   ├── etc
│   │   └── shorturl-api.yaml         // 配置檔案
│   ├── internal
│   │   ├── config
│   │   │   └── config.go             // 定義配置
│   │   ├── handler
│   │   │   ├── expandhandler.go      // 實現expandHandler
│   │   │   ├── routes.go             // 定義路由處理
│   │   │   └── shortenhandler.go     // 實現shortenHandler
│   │   ├── logic
│   │   │   ├── expandlogic.go        // 實現ExpandLogic
│   │   │   └── shortenlogic.go       // 實現ShortenLogic
│   │   ├── svc
│   │   │   └── servicecontext.go     // 定義ServiceContext
│   │   └── types
│   │       └── types.go              // 定義請求、返回結構體
│   └── shorturl.go                   // main入口定義
├── go.mod
├── go.sum
└── shorturl.api
  • 啟動 API Gateway 服務,預設偵聽在 8888 埠
go run api/shorturl.go -f api/etc/shorturl-api.yaml
  • 測試 API Gateway 服務
curl -i "http://localhost:8888/shorten?url=http://www.xiaoheiban.cn"

返回如下:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 27 Aug 2020 14:31:39 GMT
Content-Length: 15

{"shortUrl":""}

可以看到我們 API Gateway 其實啥也沒幹,就返回了個空值,接下來我們會在 rpc 服務裡實現業務邏輯

  • 可以修改internal/svc/servicecontext.go來傳遞服務依賴(如果需要)

  • 實現邏輯可以修改internal/logic下的對應檔案

  • 可以通過goctl生成各種客戶端語言的 api 呼叫程式碼

  • 到這裡,你已經可以通過 goctl 生成客戶端程式碼給客戶端同學並行開發了,支援多種語言,詳見文件

5. 編寫 shorten rpc 服務

  • rpc/shorten目錄下編寫shorten.proto檔案

可以通過命令生成 proto 檔案模板

goctl rpc template -o shorten.proto

修改後檔案內容如下:

syntax = "proto3";

package shorten;

message shortenReq {
    string url = 1;
}

message shortenResp {
    string key = 1;
}

service shortener {
    rpc shorten(shortenReq) returns(shortenResp);
}
  • goctl生成 rpc 程式碼,在rpc/shorten目錄下執行命令
goctl rpc proto -src shorten.proto

檔案結構如下:

rpc/shorten
├── etc
│   └── shorten.yaml               // 配置檔案
├── internal
│   ├── config
│   │   └── config.go              // 配置定義
│   ├── logic
│   │   └── shortenlogic.go        // rpc業務邏輯在這裡實現
│   ├── server
│   │   └── shortenerserver.go     // 呼叫入口, 不需要修改
│   └── svc
│       └── servicecontext.go      // 定義ServiceContext,傳遞依賴
├── pb
│   └── shorten.pb.go
├── shorten.go                     // rpc服務main函式
├── shorten.proto
└── shortener
    ├── shortener.go               // 提供了外部呼叫方法,無需修改
    ├── shortener_mock.go          // mock方法,測試用
    └── types.go                   // request/response結構體定義

直接可以執行,如下:

$ go run shorten.go -f etc/shorten.yaml
Starting rpc server at 127.0.0.1:8080...

etc/shorten.yaml檔案裡可以修改偵聽埠等配置

6. 編寫 expand rpc 服務

  • rpc/expand目錄下編寫expand.proto檔案

可以通過命令生成 proto 檔案模板

goctl rpc template -o expand.proto

修改後檔案內容如下:

syntax = "proto3";

package expand;

message expandReq {
    string key = 1;
}

message expandResp {
    string url = 1;
}

service expander {
    rpc expand(expandReq) returns(expandResp);
}
  • goctl生成 rpc 程式碼,在rpc/expand目錄下執行命令
goctl rpc proto -src expand.proto

檔案結構如下:

rpc/expand
├── etc
│   └── expand.yaml                // 配置檔案
├── expand.go                      // rpc服務main函式
├── expand.proto
├── expander
│   ├── expander.go                // 提供了外部呼叫方法,無需修改
│   ├── expander_mock.go           // mock方法,測試用
│   └── types.go                   // request/response結構體定義
├── internal
│   ├── config
│   │   └── config.go              // 配置定義
│   ├── logic
│   │   └── expandlogic.go         // rpc業務邏輯在這裡實現
│   ├── server
│   │   └── expanderserver.go      // 呼叫入口, 不需要修改
│   └── svc
│       └── servicecontext.go      // 定義ServiceContext,傳遞依賴
└── pb
    └── expand.pb.go

修改etc/expand.yaml裡面的ListenOn的埠為8081,因為8080已經被shorten服務佔用了

修改後執行,如下:

$ go run expand.go -f etc/expand.yaml
Starting rpc server at 127.0.0.1:8081...

etc/expand.yaml檔案裡可以修改偵聽埠等配置

7. 修改 API Gateway 程式碼呼叫 shorten/expand rpc 服務

  • 修改配置檔案shorter-api.yaml,增加如下內容
Shortener:
  Etcd:
    Hosts:
      - localhost:2379
    Key: shorten.rpc
Expander:
  Etcd:
    Hosts:
      - localhost:2379
    Key: expand.rpc

通過 etcd 自動去發現可用的 shorten/expand 服務

  • 修改internal/config/config.go如下,增加 shorten/expand 服務依賴
type Config struct {
  rest.RestConf
  Shortener rpcx.RpcClientConf     // 手動程式碼
  Expander  rpcx.RpcClientConf     // 手動程式碼
}
  • 修改internal/svc/servicecontext.go,如下:
type ServiceContext struct {
  Config    config.Config
  Shortener rpcx.Client                                 // 手動程式碼
  Expander  rpcx.Client                                 // 手動程式碼
}

func NewServiceContext(config config.Config) *ServiceContext {
  return &ServiceContext{
      Config:    config,
      Shortener: rpcx.MustNewClient(config.Shortener),    // 手動程式碼
      Expander:  rpcx.MustNewClient(config.Expander),     // 手動程式碼
  }
}

通過 ServiceContext 在不同業務邏輯之間傳遞依賴

  • 修改internal/logic/expandlogic.go,如下:
type ExpandLogic struct {
  ctx context.Context
  logx.Logger
  expander rpcx.Client            // 手動程式碼
}

func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) ExpandLogic {
  return ExpandLogic{
      ctx:    ctx,
      Logger: logx.WithContext(ctx),
      expander: svcCtx.Expander,    // 手動程式碼
  }
}

func (l *ExpandLogic) Expand(req types.ExpandReq) (*types.ExpandResp, error) {
  // 手動程式碼開始
  resp, err := expander.NewExpander(l.expander).Expand(l.ctx, &expander.ExpandReq{
      Key: req.Key,
  })
  if err != nil {
      return nil, err
  }

  return &types.ExpandResp{
      Url: resp.Url,
  }, nil
  // 手動程式碼結束
}

增加了對expander服務的依賴,並通過呼叫expanderExpand方法實現短鏈恢復到 url

  • 修改internal/logic/shortenlogic.go,如下:
type ShortenLogic struct {
  ctx context.Context
  logx.Logger
  shortener rpcx.Client             // 手動程式碼
}

func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) ShortenLogic {
  return ShortenLogic{
      ctx:    ctx,
      Logger: logx.WithContext(ctx),
      shortener: svcCtx.Shortener,    // 手動程式碼
  }
}

func (l *ShortenLogic) Shorten(req types.ShortenReq) (*types.ShortenResp, error) {
  // 手動程式碼開始
  resp, err := shortener.NewShortener(l.shortener).Shorten(l.ctx, &shortener.ShortenReq{
      Url: req.Url,
  })
  if err != nil {
      return nil, err
  }

  return &types.ShortenResp{
      ShortUrl: resp.Key,
  }, nil
  // 手動程式碼結束
}

增加了對shortener服務的依賴,並通過呼叫shortenerShorten方法實現 url 到短鏈的變換

至此,API Gateway 修改完成,雖然貼的程式碼多,但是期中修改的是很少的一部分,為了方便理解上下文,我貼了完整程式碼,接下來處理 CRUD+cache

8. 定義資料庫表結構,並生成 CRUD+cache 程式碼

  • shorturl 下建立 rpc/model 目錄:mkdir -p rpc/model
  • 在 rpc/model 目錄下編寫建立 shorturl 表的 sql 檔案shorturl.sql,如下:
CREATE TABLE `shorturl`
(
  `shorten` varchar(255) NOT NULL COMMENT 'shorten key',
  `url` varchar(255) NOT NULL COMMENT 'original url',
  PRIMARY KEY(`shorten`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
  • 建立 DB 和 table
create database gozero;
source shorturl.sql;
  • rpc/model目錄下執行如下命令生成 CRUD+cache 程式碼,-c表示使用redis cache
goctl model mysql ddl -c -src shorturl.sql -dir .

也可以用datasource命令代替ddl來指定資料庫連結直接從 schema 生成

生成後的檔案結構如下:

rpc/model
├── shorturl.sql
├── shorturlmodel.go              // CRUD+cache程式碼
└── vars.go                       // 定義常量和變數

9. 修改 shorten/expand rpc 程式碼呼叫 crud+cache 程式碼

  • 修改rpc/expand/etc/expand.yaml,增加如下內容:
DataSource: root:@tcp(localhost:3306)/gozero
Table: shorturl
Cache:
  - Host: localhost:6379

可以使用多個 redis 作為 cache,支援 redis 單點或者 redis 叢集

  • 修改rpc/expand/internal/config.go,如下:
type Config struct {
  rpcx.RpcServerConf
  DataSource string             // 手動程式碼
  Table      string             // 手動程式碼
  Cache      cache.CacheConf    // 手動程式碼
}

增加了 mysql 和 redis cache 配置

  • 修改rpc/expand/internal/svc/servicecontext.go,如下:
type ServiceContext struct {
  c     config.Config
  Model *model.ShorturlModel   // 手動程式碼
}

func NewServiceContext(c config.Config) *ServiceContext {
  return &ServiceContext{
      c:     c,
      Model: model.NewShorturlModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), // 手動程式碼
  }
}
  • 修改rpc/expand/internal/logic/expandlogic.go,如下:
type ExpandLogic struct {
  ctx context.Context
  logx.Logger
  model *model.ShorturlModel          // 手動程式碼
}

func NewExpandLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExpandLogic {
  return &ExpandLogic{
      ctx:    ctx,
      Logger: logx.WithContext(ctx),
      model:  svcCtx.Model,             // 手動程式碼
  }
}

func (l *ExpandLogic) Expand(in *expand.ExpandReq) (*expand.ExpandResp, error) {
  // 手動程式碼開始
  res, err := l.model.FindOne(in.Key)
  if err != nil {
      return nil, err
  }

  return &expand.ExpandResp{
      Url: res.Url,
  }, nil
  // 手動程式碼結束
}
  • 修改rpc/shorten/etc/shorten.yaml,增加如下內容:
DataSource: root:@tcp(localhost:3306)/gozero
Table: shorturl
Cache:
  - Host: localhost:6379

可以使用多個 redis 作為 cache,支援 redis 單點或者 redis 叢集

  • 修改rpc/shorten/internal/config.go,如下:
type Config struct {
  rpcx.RpcServerConf
  DataSource string            // 手動程式碼
  Table      string            // 手動程式碼
  Cache      cache.CacheConf   // 手動程式碼
}

增加了 mysql 和 redis cache 配置

  • 修改rpc/shorten/internal/svc/servicecontext.go,如下:
type ServiceContext struct {
  c     config.Config
  Model *model.ShorturlModel   // 手動程式碼
}

func NewServiceContext(c config.Config) *ServiceContext {
  return &ServiceContext{
      c:     c,
      Model: model.NewShorturlModel(sqlx.NewMysql(c.DataSource), c.Cache, c.Table), // 手動程式碼
  }
}
  • 修改rpc/shorten/internal/logic/shortenlogic.go,如下:
const keyLen = 6

type ShortenLogic struct {
  ctx context.Context
  logx.Logger
  model *model.ShorturlModel          // 手動程式碼
}

func NewShortenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShortenLogic {
  return &ShortenLogic{
      ctx:    ctx,
      Logger: logx.WithContext(ctx),
      model:  svcCtx.Model,             // 手動程式碼
  }
}

func (l *ShortenLogic) Shorten(in *shorten.ShortenReq) (*shorten.ShortenResp, error) {
  // 手動程式碼開始,生成短連結
  key := hash.Md5Hex([]byte(in.Url))[:keyLen]
  _, err := l.model.Insert(model.Shorturl{
      Shorten: key,
      Url:     in.Url,
  })
  if err != nil {
      return nil, err
  }

  return &shorten.ShortenResp{
      Key: key,
  }, nil
  // 手動程式碼結束
}

至此程式碼修改完成,凡事手動修改的程式碼我加了標註

10. 完整呼叫演示

  • shorten api 呼叫
~ curl -i "http://localhost:8888/shorten?url=http://www.xiaoheiban.cn"

返回如下:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 29 Aug 2020 10:49:49 GMT
Content-Length: 21

{"shortUrl":"f35b2a"}
  • expand api 呼叫
curl -i "http://localhost:8888/expand?key=f35b2a"

返回如下:

HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 29 Aug 2020 10:51:53 GMT
Content-Length: 34

{"url":"http://www.xiaoheiban.cn"}

11. Benchmark

因為寫入依賴於 mysql 的寫入速度,就相當於壓 mysql 了,所以壓測只測試了 expand 介面,相當於從 mysql 裡讀取並利用快取,shorten.lua 裡隨機從 db 裡獲取了 100 個熱 key 來生成壓測請求

benchmark

可以看出在我的 MacBook Pro 上能達到 3 萬 + 的 qps。

12. 總結

我們一直強調工具大於約定和文件

go-zero 不只是一個框架,更是一個建立在框架 + 工具基礎上的,簡化和規範了整個微服務構建的技術體系。

我們在保持簡單的同時也儘可能把微服務治理的複雜度封裝到了框架內部,極大的降低了開發人員的心智負擔,使得業務開發得以快速推進。

通過 go-zero+goctl 生成的程式碼,包含了微服務治理的各種元件,包括:併發控制、自適應熔斷、自適應降載、自動快取控制等,可以輕鬆部署以承載巨大訪問量。

12. 微信交流群

wechat

更多原創文章乾貨分享,請關注公眾號
  • 快速構建高併發微服務
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章