Windows下如何玩轉火熱的go-zero
- 2021 年 1 月 14 日
- 筆記
作者:阿啄debugIT
前言
go-zero 是一個集成了各種工程實踐的 web 和 rpc 框架。通過彈性設計保障了大並發服務端的穩定性,經受了充分的實戰檢驗。
go-zero 包含極簡的 API 定義和生成工具 goctl,可以根據定義的 api 文件一鍵生成 Go, iOS, Android,
Kotlin, Dart, TypeScript, JavaScript 代碼,並可直接運行。
這麼牛,怎麼不想去試一下?!
go語言環境搭建
配置GO語言環境
新增go的代理
GOPROXY=//goproxy.io,direct,//mirrors.aliyun.com/goproxy/,//goproxy.cn,//athens.azurefd.net,//gonexus.dev
設置環境變量
環境變量path中添加 %GOROOT%\bin;%GOPATH%\bin
在vscode的配置
安裝插件
關鍵是go語言環境在vscode的配置,如下:
{
"go.goroot": "D:\\go",
"go.gopath": "D:\\go_project",
"go.inferGopath": false,
"go.toolsGopath": "D:\\go_project",
"window.zoomLevel": 0,
"git.autofetch": true,
"terminal.integrated.shell.windows": "powershell.exe", // 也可以使用 cmd.exe
"workbench.colorTheme": "Monokai Pro (Filter Machine)",
"workbench.iconTheme": "Monokai Pro (Filter Machine) Icons",
"editor.renderControlCharacters": false,
"editor.snippetSuggestions": "top",
"editor.suggest.snippetsPreventQuickSuggestions": true,
"breadcrumbs.enabled": true,
"terminal.explorerKind": "external",
"editor.cursorStyle": "block",
"editor.links": false,
"editor.mouseWheelZoom": true,
"editor.renderLineHighlight": "all",
"editor.suggest.shareSuggestSelections": true,
"outline.icons": true,
"search.showLineNumbers": true,
"search.smartCase": true,
// package 查找模式
"go.gocodePackageLookupMode": "go",
"go.gotoSymbol.includeGoroot": true,
"go.gotoSymbol.includeImports": true,
// build 相關
"go.buildOnSave": "off",
"go.gocodeAutoBuild": true,
"go.installDependenciesWhenBuilding": true,
"go.buildFlags": [],
"go.buildTags": "",
"go.coverOnSingleTest": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.useCodeSnippetsOnFunctionSuggestWithoutType": true,
"go.docsTool": "guru",
"go.formatTool": "goimports",
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"go.lintFlags": [
"--fast"
],
"go.formatFlags": [],
"go.vetFlags": [],
"go.vetOnSave": "package",
"go.generateTestsFlags": [],
"go.liveErrors": {
"enabled": true,
"delay": 500
},
"go.gocodeFlags": [
"-builtin",
"-ignore-case",
"-unimported-packages"
],
"go.enableCodeLens": {
"references": true,
"runtest": true
},
"go.delveConfig": {
"dlvLoadConfig": {
"followPointers": true,
"maxVariableRecurse": 1,
"maxStringLen": 64,
"maxArrayValues": 64,
"maxStructFields": -1
},
"apiVersion": 2,
"showGlobalVariables": true
},
"go.editorContextMenuCommands": {
"toggleTestFile": true,
"addTags": true,
"removeTags": true,
"testAtCursor": true,
"testFile": true,
"testPackage": true,
"generateTestForFunction": true,
"generateTestForFile": true,
"generateTestForPackage": true,
"addImport": true,
"testCoverage": true,
"playground": true,
"debugTestAtCursor": true
},
"go.playground": {
"openbrowser": false,
"share": false,
"run": false
},
"go.addTags": {
"tags": "json",
"options": "json=omitempty",
"promptForTags": true,
"transform": "snakecase"
},
"go.removeTags": {
"tags": "",
"options": "",
"promptForTags": false
},
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
},
"go.alternateTools": {
"go-langserver": "gopls",
},
"go.useLanguageServer": false,
"go.languageServerFlags": [],
"go.languageServerExperimentalFeatures": {
"format": true,
"autoComplete": true,
"rename": true,
"goToDefinition": true,
"hover": true,
"signatureHelp": true,
"goToTypeDefinition": true,
"goToImplementation": true,
"documentSymbols": true,
"workspaceSymbols": true,
"findReferences": true,
"diagnostics": false
}
}
參考鏈接://www.cnblogs.com/chnmig/p/10796316.html
克隆go-zero
github地址://github.com/tal-tech/go-zero
學習視頻地址://talkgo.org/t/topic/729
語雀文檔資料://www.yuque.com/tal-tech/go-zero/tdsgkg
玩goctl工具
goctl是go-zero微服務框架下的代碼生成工具,其可以快速提升開發效率,讓開發人員將時間重點放在業務coding上……
查詢很多資料都不說的,不清晰,總是報command not found: goctl錯誤。
我也是將goctl工具安裝到 $GOPATH/bin
目錄下
?x-oss-
環境變量也配置了,最後發現,所有網上博客都沒有說,把 $GOPATH/bin
,加入系統環境path變量。加入以後,重啟電腦,就可以正常使用goctl工具。
接下來,就是體驗goctl工具。
體驗goctl工具
C:\Users\domin>d:
D:\>cd D:\go-learn
D:\go-learn>goctl api new greet
[32mDone.[0m
D:\go-learn>cd greet
D:\go-learn\greet>go mod init
go mod init: go.mod already exists
D:\go-learn\greet>go mod tidy
go: finding module for package github.com/tal-tech/go-zero/core/logx
go: finding module for package github.com/tal-tech/go-zero/rest
go: finding module for package github.com/tal-tech/go-zero/rest/httpx
go: finding module for package github.com/tal-tech/go-zero/core/conf
go: found github.com/tal-tech/go-zero/core/conf in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/rest in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/rest/httpx in github.com/tal-tech/go-zero v1.1.2
go: found github.com/tal-tech/go-zero/core/logx in github.com/tal-tech/go-zero v1.1.2
D:\go-learn\greet>go run greet.go -f etc/greet-api.yaml
Starting server at 0.0.0.0:8888...
{"@timestamp":"2021-01-10T01:04:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:04:05.751+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:05:05.747+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:05:05.751+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:06:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:06:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:07:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:07:05.752+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:08:05.744+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:08:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
{"@timestamp":"2021-01-10T01:09:05.746+08","level":"stat","content":"CPU: 0m, MEMORY: Alloc=0.6Mi, TotalAlloc=0.6Mi, Sys=6.6Mi, NumGC=0"}
{"@timestamp":"2021-01-10T01:09:05.750+08","level":"stat","content":"(api) shedding_stat [1m], cpu: 0, total: 0, pass: 0, drop: 0"}
再另外開一個cmd窗口
C:\Users\domin>d:
D:\>cd D:\go-learn\greet
D:\go-learn\greet>goctl api java -api greet.api -dir greet
[32mDone.[0m
D:\go-learn\greet>curl -i //localhost:8888/from/you
HTTP/1.1 200 OK
Content-Type: application/json
Date: Sat, 09 Jan 2021 17:09:06 GMT
Content-Length: 14
生成的代碼
總結
除了goctl神器,go-zero還有很多的小工具。
- 流數據處理利器:fx。如java8的lambda,,go-zero也有了!fx.Filter().Sort().Head() ,讓數組的複雜處理變得簡單。
- List itemmapReduce降低服務相應時間:mr.Finish(), mr.Map().Reduce(), 跟並發處理waitGroup說拜拜。
- etcd服務發現的集成:p2c的算法發現服務,避免開發人員點對點或nginx的轉發服務,安裝一個etcd就完事了。
- jwt集成api:輕鬆擁有一個jwt的後台服務。
- 集成Prometheus:輕鬆擁有一個帶監控的golang後台服務。
Go語言主要用作服務器端開發,其定位是用來開發「大型軟件」的,適合於很多程序員一起開發大型軟件,並且開發周期長,支持雲計算的網絡服務。Go語言能夠讓程序員快速開發,並且在軟件不斷的增長過程中,它能讓程序員更容易地進行維護和修改。它融合了傳統編譯型語言的高效性和腳本語言的易用性和富於表達性。
Go語言作為服務器編程語言,很適合處理日誌、數據打包、虛擬機處理、文件系統、分佈式系統、數據庫代理等;網絡編程方面,Go語言廣泛應用於Web應用、API應用、下載應用等;除此之外,Go語言還可用於內存數據庫和雲平台領域,目前國外很多雲平台都是採用Go開發。
參考鏈接://www.jianshu.com/p/6fbba7f7ced3
go-zero地址://github.com/tal-tech/go-zero
go-zero 系列文章見『微服務實踐』公眾號