欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于go-zero單體服務(wù)使用泛型簡(jiǎn)化注冊(cè)Handler路由的問題

 更新時(shí)間:2022年07月27日 16:49:46   作者:修車課代表  
這篇文章主要介紹了go-zero單體服務(wù)使用泛型簡(jiǎn)化注冊(cè)Handler路由,涉及到Golang環(huán)境安裝及配置Go Module的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、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

快速生成 api 服務(wù)

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)文章

  • GoLang逃逸分析講解

    GoLang逃逸分析講解

    我們都知道go語言中內(nèi)存管理工作都是由Go在底層完成的,這樣我們可以不用過多的關(guān)注底層的內(nèi)存問題。本文主要總結(jié)一下?Golang內(nèi)存逃逸分析,需要的朋友可以參考以下內(nèi)容,希望對(duì)大家有幫助
    2022-12-12
  • Golang極簡(jiǎn)入門教程(一):基本概念

    Golang極簡(jiǎn)入門教程(一):基本概念

    這篇文章主要介紹了Golang極簡(jiǎn)入門教程(一):基本概念,本文講解了Golang的基本知識(shí)、基礎(chǔ)語法、相關(guān)術(shù)語等,需要的朋友可以參考下
    2014-10-10
  • Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka)

    Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka)

    消息隊(duì)列是一種異步的服務(wù)間通信方式,適用于無服務(wù)器和微服務(wù)架構(gòu),本文主要介紹了Go操作各大消息隊(duì)列教程(RabbitMQ、Kafka),需要的朋友可以了解一下
    2024-02-02
  • Go語言數(shù)據(jù)結(jié)構(gòu)之插入排序示例詳解

    Go語言數(shù)據(jù)結(jié)構(gòu)之插入排序示例詳解

    這篇文章主要為大家介紹了Go語言數(shù)據(jù)結(jié)構(gòu)之插入排序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • go中for?range的坑以及解決方案

    go中for?range的坑以及解決方案

    相信小伙伴都遇到過以下的循環(huán)變量的問題,本文主要介紹了go中for?range的坑以及解決方案,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • 詳解Golang中Context的原理和使用技巧

    詳解Golang中Context的原理和使用技巧

    Golang?的?Context?包,中文可以稱之為“上下文”,是用來在?goroutine?協(xié)程之間進(jìn)行上下文信息傳遞的,這些上下文信息包括?kv?數(shù)據(jù)、取消信號(hào)、超時(shí)時(shí)間、截止時(shí)間等。本文主要介紹了Context的原理和使用技巧,希望對(duì)大家有所幫助
    2022-11-11
  • 淺析golang的依賴注入

    淺析golang的依賴注入

    這篇文章主要介紹了淺析golang的依賴注入,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • golang qq郵件發(fā)送驗(yàn)證碼功能

    golang qq郵件發(fā)送驗(yàn)證碼功能

    驗(yàn)證碼在多個(gè)場(chǎng)景下發(fā)揮著重要作用,如注冊(cè)/登錄、短信接口保護(hù)、提交/投票、密碼找回和支付驗(yàn)證等,以保障賬號(hào)安全和防止惡意操作,此外,文章還介紹了使用golang通過qq郵件發(fā)送驗(yàn)證碼的實(shí)現(xiàn)過程,包括獲取授權(quán)碼、下載依賴包和編寫代碼,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • 一文詳解Golang中new和make的區(qū)別

    一文詳解Golang中new和make的區(qū)別

    在Go語言中,new和make是兩個(gè)用于創(chuàng)建對(duì)象的內(nèi)建函數(shù)。本文將詳細(xì)介紹new和make的區(qū)別,并通過多個(gè)方面的分析和代碼示例,幫助大家理解它們的使用場(chǎng)景
    2023-05-05
  • golang模板template自定義函數(shù)用法示例

    golang模板template自定義函數(shù)用法示例

    這篇文章主要介紹了golang模板template自定義函數(shù)用法,結(jié)合實(shí)例形式分析了Go語言模板自定義函數(shù)的基本定義與使用方法,需要的朋友可以參考下
    2016-07-07

最新評(píng)論