急如闪电快如风,彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-初始化项目EP00

急如闪电快如风,彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-初始化项目EP00

    在Golang Web编程的世界里,君不言高性能则已,言高性能必称Iris。彩虹女神的名号响彻寰宇、名动江湖,单论一个快字,无人能出其右,就连以简洁轻量著称于世的Gin也难以望其项背,只见彩虹女神Iris回眸一笑撩人心扉:“虽然你们也不是那么慢,但我还是快那么一点点......”,本次就让我们来一睹彩虹女神Iris的芳颜,感受宇宙最快Web框架的神乎其神。

    女神本神(Iris)

    选择一款框架有诸多的参考层面,比如灵活性、扩展性、API友好程度、文档详细程度、项目活跃度、社区贡献等等,但是性能和内存占用绝对是优先参考的一个重要层面,原因无他,天下武功,唯快不破,正所谓一快遮百丑,经济下行,降本增效的大背景之下,高性能框架无疑占据极大的优势,说白了,成本相仿的前提下,我单位时间内网络请求吞吐量是你的一倍,还没用力,你就倒下了,你怎么跟我打?游戏还没开始,就已经结束了。

    空口白牙,不足为据,参见2022年最新请求吞吐量对比图:

    事实上,Iris本质上也是社区驱动的Go语言Web框架,支持http2/3,完备的MVC支持,奉行极简主义风格,轻量化与简明风格比起Gin来说,也不遑多让,与此同时,社区活跃度和文档支持都非常到位,但其拥有的极其恐怖的高性能特性,其他框架则是望尘莫及。在Iris身上,我们可以看到她对性能的近乎于偏执的完美追求,Iris为了优化性能,不惜自己开发和集成最快的组件,比如日志记录内置了golog模块,比如json序列化就选择了第三方库jsoniter,从框架设计的态度上,极尽完美之能事。

    建立项目IrisBlog

    参照Iris官网文档:https://github.com/kataras/iris/blob/master/README_ZH.md,我们借助彩虹女神Iris的垂爱,打造一款史上最快的在线博客系统,首先建立文件夹IrisBlog:

mkdir IrisBlog
cd IrisBlog

    随后通过go mod命令初始化项目:

C:\Users\liuyue\www\iriblog>go mod init IrisBlog
go: creating new go.mod: module IrisBlog

    对于go mod不熟的朋友,请移玉步至层次分明井然有条,Go lang1.18入门精炼教程,由白丁入鸿儒,Go lang包管理机制(package)EP10,关于go mod的使用,这里不再赘述。

    接着,由于诸位可以理解的原因,请确保使用国内的安装源:

go env -w GOPROXY=https://goproxy.cn,direct

    随后安装彩虹女神Iris:

go get github.com/kataras/iris/v12@master

    系统返回:


C:\Users\liuyue\www\iriblog>go get -u github.com/kataras/iris
go: downloading github.com/kataras/iris v0.0.2
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading github.com/kataras/golog v0.0.18
go: downloading github.com/kataras/pio v0.0.8
go: downloading github.com/kataras/sitemap v0.0.5
go: downloading github.com/BurntSushi/toml v1.2.0
go: downloading github.com/kataras/tunnel v0.0.1
go: downloading github.com/kataras/golog v0.1.7
go: downloading github.com/kataras/pio v0.0.10
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
go: downloading github.com/kataras/tunnel v0.0.4
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/Shopify/goreferrer v0.0.0-20181106222321-ec9c9a553398
go: downloading github.com/fatih/structs v1.1.0
go: downloading github.com/andybalholm/brotli v1.0.1-0.20200619015827-c3da72aa01ed
go: downloading github.com/iris-contrib/schema v0.0.2
go: downloading github.com/andybalholm/brotli v1.0.4
go: downloading github.com/iris-contrib/schema v0.0.6
go: downloading github.com/json-iterator/go v1.1.10
go: downloading github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06
go: downloading github.com/klauspost/compress v1.10.10
go: downloading github.com/klauspost/compress v1.15.9
go: downloading github.com/microcosm-cc/bluemonday v1.0.3
go: downloading github.com/russross/blackfriday/v2 v2.0.1
go: downloading github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
go: downloading golang.org/x/net v0.0.0-20200707034311-ab3426394381
go: downloading github.com/russross/blackfriday v1.5.2
go: downloading github.com/vmihailenco/msgpack v4.0.4+incompatible
go: downloading github.com/microcosm-cc/bluemonday v1.0.19
go: downloading github.com/vmihailenco/msgpack/v5 v5.3.5
go: downloading golang.org/x/text v0.3.3
go: downloading golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
go: downloading github.com/russross/blackfriday/v2 v2.1.0
go: downloading github.com/russross/blackfriday v1.6.0
go: downloading google.golang.org/protobuf v1.25.0
go: downloading golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
go: downloading golang.org/x/net v0.0.0-20220812174116-3211cb980234
go: downloading google.golang.org/protobuf v1.28.1
go: downloading golang.org/x/text v0.3.7
go: downloading golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
go: downloading golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9
go: downloading github.com/schollz/closestmatch v2.1.0+incompatible
go: downloading golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
go: downloading golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
go: downloading gopkg.in/ini.v1 v1.57.0
go: downloading gopkg.in/ini.v1 v1.67.0
go: downloading github.com/ryanuber/columnize v2.1.0+incompatible
go: downloading github.com/CloudyKit/jet/v4 v4.1.0
go: downloading github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
go: downloading github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
go: downloading github.com/ryanuber/columnize v2.1.2+incompatible
go: downloading github.com/iris-contrib/jade v1.1.4
go: downloading github.com/CloudyKit/jet v2.1.2+incompatible
go: downloading github.com/iris-contrib/pongo2 v0.0.1
go: downloading github.com/kataras/blocks v0.0.2
go: downloading github.com/yosssi/ace v0.0.5
go: downloading github.com/kataras/blocks v0.0.6
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading github.com/chris-ramon/douceur v0.2.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/vmihailenco/tagparser v0.1.1
go: downloading google.golang.org/appengine v1.6.5
go: downloading github.com/vmihailenco/tagparser v0.1.2
go: downloading github.com/shurcooL/sanitized_anchor_name v1.0.0
go: downloading google.golang.org/appengine v1.6.7
go: downloading github.com/google/uuid v1.1.2-0.20200519141726-cb32006e483f
go: downloading github.com/google/uuid v1.3.0
go: downloading github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53
go: downloading github.com/valyala/bytebufferpool v1.0.0
go: downloading github.com/aymerick/douceur v0.2.0
go: downloading github.com/gorilla/css v1.0.0
go: downloading github.com/golang/protobuf v1.4.1
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/vmihailenco/tagparser/v2 v2.0.0
go: added github.com/BurntSushi/toml v1.2.0
go: added github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53
go: added github.com/CloudyKit/jet/v4 v4.1.0
go: added github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06
go: added github.com/andybalholm/brotli v1.0.4
go: added github.com/aymerick/douceur v0.2.0
go: added github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible
go: added github.com/chris-ramon/douceur v0.2.0
go: added github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385
go: added github.com/fatih/structs v1.1.0
go: added github.com/golang/protobuf v1.5.2
go: added github.com/google/uuid v1.3.0
go: added github.com/gorilla/css v1.0.0
go: added github.com/iris-contrib/jade v1.1.4
go: added github.com/iris-contrib/pongo2 v0.0.1
go: added github.com/iris-contrib/schema v0.0.6
go: added github.com/json-iterator/go v1.1.12
go: added github.com/kataras/blocks v0.0.6
go: added github.com/kataras/golog v0.1.7
go: added github.com/kataras/iris v0.0.2
go: added github.com/kataras/pio v0.0.10
go: added github.com/kataras/sitemap v0.0.5
go: added github.com/kataras/tunnel v0.0.4
go: added github.com/klauspost/compress v1.15.9
go: added github.com/microcosm-cc/bluemonday v1.0.19
go: added github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: added github.com/modern-go/reflect2 v1.0.2
go: added github.com/russross/blackfriday/v2 v2.1.0
go: added github.com/ryanuber/columnize v2.1.2+incompatible
go: added github.com/schollz/closestmatch v2.1.0+incompatible
go: added github.com/shurcooL/sanitized_anchor_name v1.0.0
go: added github.com/valyala/bytebufferpool v1.0.0
go: added github.com/vmihailenco/msgpack/v5 v5.3.5
go: added github.com/vmihailenco/tagparser v0.1.2
go: added github.com/vmihailenco/tagparser/v2 v2.0.0
go: added github.com/yosssi/ace v0.0.5
go: added golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
go: added golang.org/x/net v0.0.0-20220812174116-3211cb980234
go: added golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab
go: added golang.org/x/text v0.3.7
go: added golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9
go: added google.golang.org/appengine v1.6.7
go: added google.golang.org/protobuf v1.28.1
go: added gopkg.in/ini.v1 v1.67.0
go: added gopkg.in/yaml.v3 v3.0.1

    安装完毕之后,可以打开项目中go.mod文件查看Iris的依赖列表:

module IrisBlog

go 1.18

require (
github.com/BurntSushi/toml v1.2.0 // indirect
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
github.com/CloudyKit/jet/v4 v4.1.0 // indirect
github.com/CloudyKit/jet/v6 v6.1.0 // indirect
github.com/Shopify/goreferrer v0.0.0-20220729165902-8cddb4f5de06 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible // indirect
github.com/chris-ramon/douceur v0.2.0 // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/flosch/pongo2/v4 v4.0.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/iris-contrib/jade v1.1.4 // indirect
github.com/iris-contrib/pongo2 v0.0.1 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kataras/blocks v0.0.6 // indirect
github.com/kataras/golog v0.1.7 // indirect
github.com/kataras/iris v0.0.2 // indirect
github.com/kataras/iris/v12 v12.2.0-beta4.0.20220813060700-f91269130ed3 // indirect
github.com/kataras/pio v0.0.10 // indirect
github.com/kataras/sitemap v0.0.5 // indirect
github.com/kataras/tunnel v0.0.4 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/mailgun/raymond/v2 v2.0.46 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/microcosm-cc/bluemonday v1.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/columnize v2.1.2+incompatible // indirect
github.com/schollz/closestmatch v2.1.0+incompatible // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/tdewolff/minify/v2 v2.12.0 // indirect
github.com/tdewolff/parse/v2 v2.6.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yosssi/ace v0.0.5 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.0.0-20220812174116-3211cb980234 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

    接着在项目的根目录建立main入口文件:

package main

import "github.com/kataras/iris/v12"

func main() {
app := iris.New()
app.Use(iris.Compression)

app.Get("/", func(ctx iris.Context) {
ctx.HTML("你好 <strong>%s</strong>!", "女神")
})

app.Listen(":5000")
}

    随后在终端启动Iris服务:

go run main.go

    系统返回:

Iris Version: 12.2.0-beta4

Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.

    还等什么?访问http://localhost:5000

    万唤千呼始出来。

    使用快捷键control+C可以终止服务,随后可以再次运行go run main.go来重新编译启动服务。

    Iris项目热重启机制:fresh

    众所周知,由于Go lang是编译型语言,每次修改代码之后都需要重新编译,Iris目前没有内置代码热更新的工具,这里我们可以使用三方包:fresh,如此,可以大幅提高我们的Iris开发效率,在非项目目录执行命令:

go get github.com/pilu/fresh

    注意,这里一定不能在项目的目录中执行安装命令,因为go mod模式会认为是项目依赖包,而不会在当前系统的bin目录下生成可执行命令fresh。

    随后进入项目目录:

cd IrisBlog

    使用fresh命令启动Iris服务:

C:\Users\liuyue\www\iriblog>fresh
0:19:33 runner | InitFolders
0:19:33 runner | mkdir ./tmp
0:19:33 watcher | Watching .
0:19:33 main | Waiting (loop 1)...
0:19:33 main | receiving first event /
0:19:33 main | sleeping for 600 milliseconds
0:19:33 main | flushing events
0:19:33 main | Started! (5 Goroutines)
0:19:33 main | remove tmp\runner-build-errors.log: The system cannot find the file specified.
0:19:33 build | Building...
0:19:44 runner | Running...
0:19:45 main | --------------------
0:19:45 main | Waiting (loop 2)...
0:19:46 app | Iris Version: 12.2.0-beta4
0:19:46 app |
0:19:46 app | Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.

    此时,项目内所有包文件都会被监控,当代码被修改后,会自动触发编译动作,原理大概相当于Python中Tornado框架的事件循环机制。

    当我们修改代码之后,fresh会监控到修改动作,然后立刻build:

Application started. Press CTRL+C to shut down.
0:28:02 watcher | sending event ".\\main.go": MODIFY
0:28:02 watcher | sending event ".\\main.go": MODIFY
0:28:02 main | receiving first event ".\\main.go": MODIFY
0:28:02 main | sleeping for 600 milliseconds
0:28:02 main | flushing events
0:28:02 main | receiving event ".\\main.go": MODIFY
0:28:02 main | Started! (8 Goroutines)
0:28:02 main | remove tmp\runner-build-errors.log: The system cannot find the file specified.
0:28:02 build | Building...
0:28:10 runner | Running...
0:28:10 runner | Killing PID 11276
0:28:11 main | --------------------
0:28:11 main | Waiting (loop 3)...
0:28:12 app | Iris Version: 12.2.0-beta4
0:28:12 app |
Now listening on: http://localhost:5000
Application started. Press CTRL+C to shut down.

    如此,就不需要手动触发代码的编译了,简单方便。

    如果有定制化需求,可以为当前项目添加配置文件runner.conf:

root:              .
tmp_path: ./fresh
build_name: runner_build
build_log: runner_build_errors.log
valid_ext: .go, .tpl, .tmpl, .html, .md, .log
no_rebuild_ext: .tpl, .tmpl, .html
ignored: assets, tmp, log
build_delay: 3000
colors: 1
log_color_main: cyan
log_color_build: yellow
log_color_runner: green
log_color_watcher: magenta
log_color_app: red

    可以定制化诸如监听的文件、编译日志、忽略文件和目录,编译延迟等等操作。

    修改定制化配置文件后,针对配置文件启动fresh服务:

fresh -c runner.conf

    如此,fresh服务会根据配置文件来进行监听编译动作。

    结语

    最低的系统资源开销,最高的单位时间网络请求吞吐量,这是彩虹女神Iris对开发者们最好的馈赠,当我们安装好Iris并且配置好热重启机制时,我们也就走下了Go语言web开发万里长征的第一步,山高路远,城高池深,虽然前路艰险,但就算是莽撞地开始,拙劣地完成,也好过眼高手低而不去做,你同意吗?