成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Go 語言基于 Go kit 開發(fā) Web 項目

開發(fā) 項目管理
endpoint 層接收請求參數(shù)并返回響應結果,需要注意的是,在 endpoint 層,給業(yè)務接口方法構建 endpoint.Endpoint。

1.介紹

我們在上一篇文章「??Golang 微服務工具包 Go kit??」介紹了 Go 語言工具包 Go kit,本文我們介紹怎么基于 Go kit 開發(fā) Web 項目。在閱讀上篇文章后,我們已經知道 Go kit 服務分為三層,分別是 transport、endpoint 和 service。

其中,service 層定義業(yè)務接口并實現(xiàn)接口方法。

endpoint 層接收請求參數(shù)并返回響應結果,需要注意的是,在 endpoint 層,給業(yè)務接口方法構建 endpoint.Endpoint。

因為 endpoint.Endpoint 是函數(shù)類型,封裝一層,方便我們使用 endpoint 裝飾器,給 endpoint.Endpoint 添加功能,例如日志、限流、負載均衡、鏈路追蹤等。

endpoint 層使用構建的 endpoint.Endpoint 調用 service 層接口的方法處理請求。

transport 層對外提供調用接口(http 或 rpc)。

2.基于 Go kit 開發(fā) Web 項目

我們基于 Go kit 開發(fā)一個用戶中心項目,主要包含注冊和登錄的功能。

目錄結構如下:

.
├── endpoint # 接收請求,構建 endpoint.Endpoint 調用 service 層的接口方法,處理請求參數(shù),返回響應結果給 transport 層
│ └── user.go
├── go.mod
├── go.sum
├── main.go
├── service # 定義業(yè)務接口并實現(xiàn)接口方法
│ └── user.go
└── transport # 對外提供調用接口(http 或 rpc)
└── http.go
  • service 包定義服務(user 服務)的接口,并實現(xiàn)接口方法。
...
type IUser interface {
Register(ctx context.Context, req *RegisterRequest) (*User, error)
Login(ctx context.Context, email, password string) (*User, error)
}
...
  • endpoint 包為接口方法構建 endpoint.Endpoint,將請求參數(shù)轉換為接口方法可以處理的參數(shù),并將返回的響應結果封裝為對應的 response 結構體,返回給 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 包把構建的 endpoint.Endpoint 提供給調用方。
...
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,并啟動 Web 服務器。
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 命令啟動,并使用 cURL 調用 http 接口。
go run main.go

curl -X POST http://localhost:8080/register \
-d 'email=gopher@88.com&password=123456&username=gopher'

3.總結

本文我們通過一個簡單的用戶中心項目,介紹如何基于 Go kit 開發(fā) Web 項目,為了方便讀者朋友們理解代碼,項目代碼中未使用其他組件,感興趣的讀者朋友可以嘗試完善,例如添加操作數(shù)據(jù)庫的代碼。

責任編輯:武曉燕 來源: Golang語言開發(fā)棧
相關推薦

2022-09-04 23:24:45

Go語言監(jiān)控

2023-12-25 09:49:01

Golang架構Go-Kit

2013-03-12 09:50:45

GoRESTful Web

2018-03-12 22:13:46

GO語言編程軟件

2023-11-06 01:39:02

Go語言開發(fā)

2012-03-20 09:20:40

Go語言

2022-03-22 09:22:21

Go kitgRPC網絡傳輸

2023-01-16 00:12:20

Go語言Web

2022-04-13 08:20:32

DockerGo項目

2012-07-06 13:16:37

Google Go

2023-12-26 00:58:53

Web應用Go語言

2021-06-08 10:41:00

Go語言算法

2025-02-05 12:09:12

2024-04-28 10:17:30

gnetGo語言

2012-10-08 09:25:59

GoGo語言開發(fā)語言

2022-01-17 16:09:43

Go語言開發(fā)

2015-01-05 09:44:33

Github

2012-05-19 22:17:30

Android

2013-04-08 15:52:06

Go語言

2024-06-06 09:47:56

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 黄色一级在线播放 | 久久99精品久久 | 日本精品视频 | 91精品国产欧美一区二区 | 91久久精品一区二区二区 | 成人欧美一区二区三区白人 | 国产美女在线观看 | 免费国产视频在线观看 | 午夜电影一区二区 | 久久久久国产精品一区二区 | 亚洲精品成人在线 | 欧美一级黄色网 | 日韩视频在线免费观看 | 一区二区三区视频在线观看 | 国产视频福利在线观看 | 欧美视频第三页 | 欧美一区二区 | 亚洲免费精品 | 三级黄色片在线播放 | 亚洲一区不卡在线 | 国产成人精品久久二区二区 | 极品销魂美女一区二区 | 视频一二三区 | 久精品视频| 日日噜 | 欧美性久久 | 亚洲国产aⅴ精品 | 午夜爽爽爽男女免费观看 | 欧美男人天堂 | 日韩久草 | 国产在线中文字幕 | 国产乱码精品一区二区三区中文 | 国产一级视频免费播放 | 久久男人| 亚洲www啪成人一区二区麻豆 | 国产一区二区三区精品久久久 | 91一区二区 | 中文字幕成人 | 精品一区二区电影 | 日本精品久久 | 国产精品视频一区二区三区四蜜臂 |