Go 語言基于 Go kit 開發(fā) Web 項(xiàng)目
1.介紹
我們?cè)谏弦黄恼隆??Golang 微服務(wù)工具包 Go kit??」介紹了 Go 語言工具包 Go kit,本文我們介紹怎么基于 Go kit 開發(fā) Web 項(xiàng)目。在閱讀上篇文章后,我們已經(jīng)知道 Go kit 服務(wù)分為三層,分別是 transport、endpoint 和 service。
其中,service 層定義業(yè)務(wù)接口并實(shí)現(xiàn)接口方法。
endpoint 層接收請(qǐng)求參數(shù)并返回響應(yīng)結(jié)果,需要注意的是,在 endpoint 層,給業(yè)務(wù)接口方法構(gòu)建 endpoint.Endpoint。
因?yàn)?endpoint.Endpoint 是函數(shù)類型,封裝一層,方便我們使用 endpoint 裝飾器,給 endpoint.Endpoint 添加功能,例如日志、限流、負(fù)載均衡、鏈路追蹤等。
endpoint 層使用構(gòu)建的 endpoint.Endpoint 調(diào)用 service 層接口的方法處理請(qǐng)求。
transport 層對(duì)外提供調(diào)用接口(http 或 rpc)。
2.基于 Go kit 開發(fā) Web 項(xiàng)目
我們基于 Go kit 開發(fā)一個(gè)用戶中心項(xiàng)目,主要包含注冊(cè)和登錄的功能。
目錄結(jié)構(gòu)如下:
.
├── endpoint # 接收請(qǐng)求,構(gòu)建 endpoint.Endpoint 調(diào)用 service 層的接口方法,處理請(qǐng)求參數(shù),返回響應(yīng)結(jié)果給 transport 層
│ └── user.go
├── go.mod
├── go.sum
├── main.go
├── service # 定義業(yè)務(wù)接口并實(shí)現(xiàn)接口方法
│ └── user.go
└── transport # 對(duì)外提供調(diào)用接口(http 或 rpc)
└── http.go
- service 包定義服務(wù)(user 服務(wù))的接口,并實(shí)現(xiàn)接口方法。
...
type IUser interface {
Register(ctx context.Context, req *RegisterRequest) (*User, error)
Login(ctx context.Context, email, password string) (*User, error)
}
...
- endpoint 包為接口方法構(gòu)建 endpoint.Endpoint,將請(qǐng)求參數(shù)轉(zhuǎn)換為接口方法可以處理的參數(shù),并將返回的響應(yīng)結(jié)果封裝為對(duì)應(yīng)的 response 結(jié)構(gòu)體,返回給 transport 包。
...
type RegisterRequest struct {
UserName string
Email string
Password string
}
type RegisterResponse struct {
User *service.User
}
func MakeRegisterEndpoint(iUser service.IUser) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(*RegisterRequest)
user, err := iUser.Register(ctx, &service.RegisterRequest{
UserName: req.UserName,
Email: req.Email,
Password: req.Password,
})
return &RegisterResponse{User: user}, err
}
}
...
- transport 包把構(gòu)建的 endpoint.Endpoint 提供給調(diào)用方。
...
func NewHttpHandler(ctx context.Context, endpoints *endpoint.Endpoints) http.Handler {
r := http.NewServeMux()
r.Handle("/register", kitHttp.NewServer(endpoints.RegisterEndpoint, decRegisterRequest, encResponse))
return r
}
...
- 在 main 函數(shù)中,創(chuàng)建 service、endpoint 和 transport,并啟動(dòng) Web 服務(wù)器。
func main() {
ctx := context.Background()
userService := service.NewUserService()
endpoints := &endpoint.Endpoints{
RegisterEndpoint: endpoint.MakeRegisterEndpoint(userService),
LoginEndpoint: endpoint.MakeLoginEndpoint(userService),
}
r := transport.NewHttpHandler(ctx, endpoints)
err := http.ListenAndServe(":8080", r)
if err != nil {
log.Fatal(err)
return
}
}
- 使用 go run 命令啟動(dòng),并使用 cURL 調(diào)用 http 接口。
go run main.go
curl -X POST http://localhost:8080/register \
-d 'email=gopher@88.com&password=123456&username=gopher'
3.總結(jié)
本文我們通過一個(gè)簡(jiǎn)單的用戶中心項(xiàng)目,介紹如何基于 Go kit 開發(fā) Web 項(xiàng)目,為了方便讀者朋友們理解代碼,項(xiàng)目代碼中未使用其他組件,感興趣的讀者朋友可以嘗試完善,例如添加操作數(shù)據(jù)庫的代碼。