在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/ir...\_ZH.md,咱们借助彩虹女神Iris的垂爱,打造一款史上最快的在线博客零碎,首先建设文件夹IrisBlog:
mkdir IrisBlogcd 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 -u github.com/kataras/iris
零碎返回:
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开发万里长征的第一步,山高路远,城高池深,尽管前路艰险,但就算是鲁莽地开始,高明地实现,也好过眼高手低而不去做,你批准吗?