關(guān)于go-zero單體服務(wù)使用泛型簡(jiǎn)化注冊(cè)Handler路由的問題
一、Golang環(huán)境安裝及配置Go Module
https://go-zero.dev/cn/docs/prepare/golang-install
mac OS安裝Go#
- 下載并安裝Go for Mac
- 驗(yàn)證安裝結(jié)果
$ go version go version go1.15.1 darwin/amd64
linux 安裝Go#
- 下載Go for Linux
- 解壓壓縮包至/usr/local
$ tar -C /usr/local -xzf go1.15.8.linux-amd64.tar.gz
添加/usr/local/go/bin到環(huán)境變量
$ $HOME/.profile $ export PATH=$PATH:/usr/local/go/bin $ source $HOME/.profile
驗(yàn)證安裝結(jié)果
$ go version go version go1.15.1 linux/amd64
Windows安裝Go#
下載并安裝Go for Windows驗(yàn)證安裝結(jié)果
$ go version go version go1.15.1 windows/amd64
MODULE配置
Go Module是Golang管理依賴性的方式,像Java中的Maven,Android中的Gradle類似。
查看GO111MODULE開啟情況
$ go env GO111MODULE on
開啟GO111MODULE,如果已開啟(即執(zhí)行g(shù)o env GO111MODULE結(jié)果為on)請(qǐng)?zhí)^。
$ go env -w GO111MODULE="on"
設(shè)置GOPROXY
$ go env -w GOPROXY=https://goproxy.cn
設(shè)置GOMODCACHE
查看GOMODCACHE
$ go env GOMODCACHE
如果目錄不為空或者/dev/null,請(qǐng)?zhí)^。
go env -w GOMODCACHE=$GOPATH/pkg/mod
二、Goctl 安裝
Goctl在go-zero項(xiàng)目開發(fā)著有著很大的作用,其可以有效的幫助開發(fā)者大大提高開發(fā)效率,減少代碼的出錯(cuò)率,縮短業(yè)務(wù)開發(fā)的工作量,更多的Goctl的介紹請(qǐng)閱讀Goctl介紹
安裝(mac&linux)
### Go 1.15 及之前版本 GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromicro/go-zero/tools/goctl@latest ### Go 1.16 及以后版本 GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/go-zero/tools/goctl@latest
安裝(windows)
go install github.com/zeromicro/go-zero/tools/goctl@latest
環(huán)境變量檢測(cè)(mac&linux)
go get 下載編譯后的二進(jìn)制文件位于 \$GOPATH/bin 目錄下,要確保 $GOPATH/bin已經(jīng)添加到環(huán)境變量。
sudo vim /etc/paths //添加環(huán)境變量
在最后一行添加如下內(nèi)容 //$GOPATH 為你本機(jī)上的文件地址
$GOPATH/bin
安裝結(jié)果驗(yàn)證
$ goctl -v goctl version 1.1.4 darwin/amd64
二、初始化go-zero
goctl api new greet cd greet go mod init go mod tidy go run greet.go -f etc/greet-api.yaml
默認(rèn)偵聽在 8888 端口
偵聽端口可以在greet-api.yaml
配置文件里修改,此時(shí),可以通過 curl 請(qǐng)求,或者直接在瀏覽器中打開http://localhost:8888/from/you
$ curl -i http://localhost:8888/from/you HTTP/1.1 200 OK Content-Type: application/json; charset=utf-8 Traceparent: 00-45fa9e7a7c505bad3a53a024e425ace9-eb5787234cf3e308-00 Date: Thu, 22 Oct 2020 14:03:18 GMT Content-Length: 14 null
greet服務(wù)的目錄結(jié)構(gòu)
$ tree greet greet ├── etc │ └── greet-api.yaml ├── greet.api ├── greet.go └── internal ├── config │ └── config.go ├── handler │ ├── greethandler.go │ └── routes.go ├── logic │ └── greetlogic.go ├── svc │ └── servicecontext.go └── types └── types.go
三、查看注冊(cè)Handler路由流程greet.go
var configFile = flag.String("f", "etc/greet-api.yaml", "the config file") func main() { flag.Parse() var c config.Config conf.MustLoad(*configFile, &c) server := rest.MustNewServer(c.RestConf) defer server.Stop() //上面的都是加載配置什么的 ctx := svc.NewServiceContext(c) handler.RegisterHandlers(server, ctx) //此方法是注冊(cè)路由和路由映射Handler,重點(diǎn)在這里 fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port) server.Start() }
RegisterHandlers在internal\handler\routes.go
中
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { server.AddRoutes( //往rest.Server中添加路由 []rest.Route{ //路由數(shù)組 { Method: http.MethodGet, Path: "/from/:name", //路由 Handler: GreetHandler(serverCtx),//當(dāng)前路由的處理Handler }, }, ) }
GreetHandler在internal\handler\greethandler.go
中
func GreetHandler(ctx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.Request if err := httpx.Parse(r, &req); err != nil { //請(qǐng)求的錯(cuò)誤判斷,這個(gè)可以不用管 httpx.Error(w, err) return } l := logic.NewGreetLogic(r.Context(), ctx) //GreetHandler處理函數(shù)將請(qǐng)求轉(zhuǎn)發(fā)到了GreetLogic中,調(diào)用NewGreetLogic進(jìn)行結(jié)構(gòu)體的初始化 resp, err := l.Greet(req) //然后調(diào)用Greet來進(jìn)行處理請(qǐng)求,所以我們?cè)贕reetLogic.Greet方法中可以看到一句話// todo: add your logic here and delete this line if err != nil { httpx.Error(w, err) } else { httpx.OkJson(w, resp) } } }
四、對(duì)注冊(cè)Handler路由進(jìn)行簡(jiǎn)化
項(xiàng)目文件的增加
在路由注冊(cè)時(shí),我們?nèi)绻?wù)越加越多,那么相對(duì)應(yīng)的func xxxxHandler(ctx *svc.ServiceContext) http.HandlerFunc
就要進(jìn)行多次的添加,并且這個(gè)方法體內(nèi)部1到5行是屬于額外的重復(fù)添加
例如:我們添加一個(gè)customlogic.go
按照命名的正確和規(guī)范性,需要在internal\logic
目錄下添加customlogic.go文件,然后在internal\handler
目錄下添加customhandler.go文件,并且兩個(gè)文件都添加相對(duì)應(yīng)的結(jié)構(gòu)體和函數(shù)等,最后在routes.go
中再添加一次
{ Method: http.MethodGet, Path: "/custom/:name", Handler: CustomHandler(serverCtx), },
此時(shí),我們的文件結(jié)構(gòu)應(yīng)該是這樣
greet ├── etc │ └── greet-api.yaml ├── greet.api ├── greet.go └── internal ├── config │ └── config.go ├── handler │ ├── greethandler.go │ ├── customhandler.go │ ├── ... │ └── routes.go ├── logic │ ├── greetlogic.go │ ├── ... │ └── customlogic.go ├── svc │ └── servicecontext.go └── types └── types.go
當(dāng)單體應(yīng)用達(dá)到一定的數(shù)量級(jí),handler和logic文件夾下將會(huì)同步增加很多的文件
引入泛型概念
自Go1.18開始,go開始使用泛型,泛型的廣泛定義 :是一種把明確類型的工作推遲到創(chuàng)建對(duì)象或者調(diào)用方法的時(shí)候才去明確的特殊的類型。 也就是說在泛型使用過程中,操作的數(shù)據(jù)類型被指定為一個(gè)參數(shù),而這種參數(shù)類型可以用在 類、方法和接口 中,分別被稱為 泛型類 、 泛型方法 、 泛型接口 。
我們可以利用泛型,讓在添加路由時(shí)就要固定死的Handler: GreetHandler(serverCtx)
推遲到后面,去根據(jù)實(shí)際的Logic結(jié)構(gòu)體去判斷需要真正執(zhí)行的logic.NewGreetLogic(r.Context(), ctx)
初始化結(jié)構(gòu)體和l.Greet(req)
邏輯處理方法
如何去做在internal\logic
下添加一個(gè)baselogic.go
文件,參考Go泛型實(shí)戰(zhàn) | 如何在結(jié)構(gòu)體中使用泛型
package logic import ( "greet/internal/svc" "greet/internal/types" "net/http" ) type BaseLogic interface { any Handler(req types.Request, w http.ResponseWriter, r *http.Request, svcCtx *svc.ServiceContext) //每一個(gè)結(jié)構(gòu)體中必須要繼承一下Handler方法,例如customlogic.go和greetlogic.go中的Handler方法 } type logic[T BaseLogic] struct { data T } func New[T BaseLogic]() logic[T] { c := logic[T]{} var ins T c.data = ins return c } func (a *logic[T]) LogicHandler(req types.Request, w http.ResponseWriter, r *http.Request, svcCtx *svc.ServiceContext) { //作為一個(gè)中轉(zhuǎn)處理方法,最終執(zhí)行結(jié)構(gòu)體的Handler a.data.Handler(req, w, r, svcCtx) }
將greethandler.go
文件修改成basehandler.go
,注釋掉之前的GreetHandler
方法
package handler import ( "net/http" "greet/internal/logic" "greet/internal/svc" "greet/internal/types" "github.com/zeromicro/go-zero/rest/httpx" ) // func GreetHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { // return BaseHandlerFunc(svcCtx) // // return func(w http.ResponseWriter, r *http.Request) { // // var req types.Request // // if err := httpx.Parse(r, &req); err != nil { // // httpx.Error(w, err) // // return // // } // // l := logic.NewGreetLogic(r.Context(), svcCtx) // // resp, err := l.Greet(&req) // // if err != nil { // // httpx.Error(w, err) // // } else { // // httpx.OkJson(w, resp) // // } // // } // } func BaseHandlerFunc[T logic.BaseLogic](svcCtx *svc.ServiceContext, t T) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.Request if err := httpx.Parse(r, &req); err != nil { httpx.Error(w, err) return } //通過泛型動(dòng)態(tài)調(diào)用不同結(jié)構(gòu)體的Handler方法 cc := logic.New[T]() cc.LogicHandler(req, w, r, svcCtx) } }
在internal\logic\greetlogic.go
中增加一個(gè)Handler
方法
package logic import ( "context" "net/http" "greet/internal/svc" "greet/internal/types" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" ) type GreetLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewGreetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GreetLogic { return &GreetLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (a GreetLogic) Handler(req types.Request, w http.ResponseWriter, r *http.Request, svcCtx *svc.ServiceContext) { //新增方法 l := NewGreetLogic(r.Context(), svcCtx) resp, err := l.Greet(&req) if err != nil { httpx.Error(w, err) } else { httpx.OkJson(w, resp) } } func (l *GreetLogic) Greet(req *types.Request) (resp *types.Response, err error) { // todo: add your logic here and delete this line response := new(types.Response) if (*req).Name == "me" { response.Message = "greetLogic: listen to me, thank you." } else { response.Message = "greetLogic: listen to you, thank me." } return response, nil }
然后修改internal\handler\routes.go
下面的server.AddRoutes
部分
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { server.AddRoutes( //往rest.Server中添加路由 []rest.Route{ //路由數(shù)組 { Method: http.MethodGet, Path: "/from/:name", //路由 Handler: BaseHandlerFunc(serverCtx,logic.GreetLogic{}), }, }, ) }
現(xiàn)在就大功告成了,我們啟動(dòng)一下
go run greet.go -f etc/greet-api.yaml
然后在瀏覽器中請(qǐng)求一下http://localhost:8888/from/you
驗(yàn)證一下新增api路由在internal\logic
下新增一個(gè)customlogic.go
文件
package logic import ( "context" "net/http" "greet/internal/svc" "greet/internal/types" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" ) type CustomLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCustomLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CustomLogic { return &CustomLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (a CustomLogic) Handler(req types.Request, w http.ResponseWriter, r *http.Request, svcCtx *svc.ServiceContext) { l := NewCustomLogic(r.Context(), svcCtx) resp, err := l.Custom(&req) if err != nil { httpx.Error(w, err) } else { httpx.OkJson(w, resp) } } func (l *CustomLogic) Custom(req *types.Request) (resp *types.Response, err error) { //response.Message稍微修改了一下,便于區(qū)分 // todo: add your logic here and delete this line response := new(types.Response) if (*req).Name == "me" { response.Message = "customLogic: listen to me, thank you." } else { response.Message = "customLogic: listen to you, thank me." } return response, nil }
然后修改internal\handler\routes.go
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { server.AddRoutes( //往rest.Server中添加路由 []rest.Route{ //路由數(shù)組 { Method: http.MethodGet, Path: "/from/:name", //路由 Handler: BaseHandlerFunc(serverCtx,logic.GreetLogic{}), }, { Method: http.MethodGet, Path: "/to/:name", //路由 Handler: BaseHandlerFunc(serverCtx,logic.CustomLogic{}), }, }, ) }
其他地方不需要修改
我們啟動(dòng)一下
go run greet.go -f etc/greet-api.yaml
然后在瀏覽器中請(qǐng)求一下http://localhost:8888/from/you
、http://localhost:8888/to/you
、http://localhost:8888/too/you
現(xiàn)在,在添加新的logic做路由映射時(shí),就可以直接簡(jiǎn)化掉添加xxxxhandler.go
文件了,實(shí)際上是將這個(gè)Handler移動(dòng)到了xxxxlogic.go中。
本文代碼放在go-zero-monolithic-service-generics
到此這篇關(guān)于go-zero單體服務(wù)使用泛型簡(jiǎn)化注冊(cè)Handler路由的文章就介紹到這了,更多相關(guān)go-zero單體服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka)
消息隊(duì)列是一種異步的服務(wù)間通信方式,適用于無服務(wù)器和微服務(wù)架構(gòu),本文主要介紹了Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka),需要的朋友可以了解一下2024-02-02Go語言數(shù)據(jù)結(jié)構(gòu)之插入排序示例詳解
這篇文章主要為大家介紹了Go語言數(shù)據(jù)結(jié)構(gòu)之插入排序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08golang模板template自定義函數(shù)用法示例
這篇文章主要介紹了golang模板template自定義函數(shù)用法,結(jié)合實(shí)例形式分析了Go語言模板自定義函數(shù)的基本定義與使用方法,需要的朋友可以參考下2016-07-07