使用twirp搭建rpc框架

1.安裝protobuf complier

鏈接:https://pan.baidu.com/s/1HcCTttxZHJYopu-YwUl6PQ 提取碼:3rub

把下載好的protoc.exe文件放到你的$GOPATH/bin下,並且保證你的$GOPATH/bin目錄已經導入到環境變量中

2.安裝protoc-gen-go

https://github.com/golang/protobuf 把庫文件克隆下來,再執行下面的命令生成protoc-gen-go.exe到$GOPATH/bin中

go get -u github.com/golang/protobuf/protoc-gen-go

3.安裝protoc-gen-twirp

https://github.com/twitchtv/twirp 把庫文件克隆下來,再執行下面的命令生成protoc-gen-twirp.exe到$GOPATH/bin中

go get -u github.com/twitchtv/twirp/protoc-gen-twirp

4.編寫protobuf服務的定義

在剛剛克隆下來的twitch文件夾下創建一個twirpexample文件夾,在twirpexample文件夾下創建rpc/haberdasher/service.proto,編寫service.proto

syntax = "proto3";

package twirp.example.haberdasher;
option go_package = "haberdasher";

// Haberdasher service makes hats for clients.
service Haberdasher {
  // MakeHat produces a hat of mysterious, randomly-selected color!
  rpc MakeHat(Size) returns (Hat);
}

// Size of a Hat, in inches.
message Size {
  int32 inches = 1; // must be > 0
}

// A Hat is a piece of headwear made by a Haberdasher.
message Hat {
  int32 inches = 1;
  string color = 2; // anything but "invisible"
  string name = 3; // i.e. "bowler"
}

5.生成*.pb.go和*.twirp.go

切換到rpc目錄,執行下面的命令

protoc --go_out=.  --proto_path=. ./rpc/haberdasher/service.proto --twirp_out=.

解釋一下,--go_out就是生成的*.pb.go文件存放的位置,--proto_path就是待轉換的*.proto文件存放的位置,--twirp_out就是生成的*.twirp.go文件存放的位置

6.實現服務器的業務邏輯(接口的方法)

在twirpexample文件夾下,創建internal/haberdasherserver/server.go,編寫server.go

package haberdasherserver

import (
    "context"
    "math/rand"

    "github.com/twitchtv/twirp"
    pb "github.com/twitchtv/twirpexample/rpc/haberdasher"
)

// Server implements the Haberdasher service
type Server struct {}

func (s *Server) MakeHat(ctx context.Context, size *pb.Size) (hat *pb.Hat, err error) {
    if size.Inches <= 0 {
        return nil, twirp.InvalidArgumentError("inches", "I can't make a hat that small!")
    }
    return &pb.Hat{
        Inches:  size.Inches,
        Color: []string{"white", "black", "brown", "red", "blue"}[rand.Intn(4)],
        Name:  []string{"bowler", "baseball cap", "top hat", "derby"}[rand.Intn(3)],
    }, nil
}

7.創建提供服務的服務器

在twirpexample文件夾下,創建cmd/server/main.go,編寫main.go

package main

import (
    "net/http"

    "github.com/twitchtv/twirpexample/internal/haberdasherserver"
    "github.com/twitchtv/twirpexample/rpc/haberdasher"
)

func main() {
  server := &haberdasherserver.Server{} // implements Haberdasher interface
  twirpHandler := haberdasher.NewHaberdasherServer(server, nil)

  http.ListenAndServe(":8080", twirpHandler)
}

8.創建客戶端

在twirpexample文件夾下,創建cmd/client/main.go,編寫main.go

package main

import (
    "context"
    "net/http"
    "os"
    "fmt"
    "github.com/twitchtv/twirpexample/rpc/haberdasher"
)

func main() {
    client := haberdasher.NewHaberdasherProtobufClient("http://localhost:8080", &http.Client{})

    hat, err := client.MakeHat(context.Background(), &haberdasher.Size{Inches: 12})
    if err != nil {
        fmt.Printf("oh no: %v", err)
        os.Exit(1)
    }
    fmt.Printf("I have a nice new hat: %+v", hat)
}

9.測試

在cmd/server/main.go中執行go run main.go開啓服務器,在cmd/client/main.go中執行go run main.go開啓客戶端訪問相應的服務

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章