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

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

在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开发万里长征的第一步,山高路远,城高池深,虽然前路艰险,但就算是莽撞地开始,拙劣地完成,也好过眼高手低而不去做,你同意吗?

相关文章
|
1月前
|
开发框架 Go 计算机视觉
纯Go语言开发人脸检测、瞳孔/眼睛定位与面部特征检测插件-助力GoFly快速开发框架
开发纯go插件的原因是因为目前 Go 生态系统中几乎所有现有的人脸检测解决方案都是纯粹绑定到一些 C/C++ 库,如 OpenCV 或 dlib,但通过 cgo 调用 C 程序会引入巨大的延迟,并在性能方面产生显著的权衡。此外,在许多情况下,在各种平台上安装 OpenCV 是很麻烦的。使用纯Go开发的插件不仅在开发时方便,在项目部署和项目维护也能省很多时间精力。
|
2月前
|
缓存 JSON 监控
如何在项目中保证 Web 组件化的性能
保证 Web 组件化的性能需要从多个方面入手,综合运用各种优化方法和策略。通过持续的优化和改进,能够提高组件化的整体性能,为用户提供更好的体验,同时也有助于提高项目的开发效率和质量。
50 8
|
2月前
|
存储 前端开发 JavaScript
如何在项目中高效地进行 Web 组件化开发
高效地进行 Web 组件化开发需要从多个方面入手,通过明确目标、合理规划、规范开发、加强测试等一系列措施,实现组件的高效管理和利用,从而提高项目的整体开发效率和质量,为用户提供更好的体验。
40 7
|
2月前
|
Go API 数据库
Go 语言中常用的 ORM 框架,如 GORM、XORM 和 BeeORM,分析了它们的特点、优势及不足,并从功能特性、性能表现、易用性和社区活跃度等方面进行了比较,旨在帮助开发者根据项目需求选择合适的 ORM 框架。
本文介绍了 Go 语言中常用的 ORM 框架,如 GORM、XORM 和 BeeORM,分析了它们的特点、优势及不足,并从功能特性、性能表现、易用性和社区活跃度等方面进行了比较,旨在帮助开发者根据项目需求选择合适的 ORM 框架。
156 4
|
2月前
|
监控 安全 测试技术
如何在实际项目中应用Python Web开发的安全测试知识?
如何在实际项目中应用Python Web开发的安全测试知识?
40 4
|
2月前
|
中间件 Go API
Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架
本文概述了Go语言中几种流行的Web框架,如Beego、Gin和Echo,分析了它们的特点、性能及适用场景,并讨论了如何根据项目需求、性能要求、团队经验和社区支持等因素选择最合适的框架。
153 1
|
2月前
|
JavaScript 前端开发 开发工具
web项目规范配置(husky、eslint、lint-staged、commit)
通过上述配置,可以确保在Web项目开发过程中自动进行代码质量检查和规范化提交。Husky、ESLint、lint-staged和Commitlint共同作用,使得每次提交代码之前都会自动检查代码风格和语法问题,防止不符合规范的代码进入代码库。这不仅提高了代码质量,还保证了团队协作中的一致性。希望这些配置指南能帮助你建立高效的开发流程。
75 5
|
2月前
|
安全 Go 调度
Go语言中的并发编程:解锁高性能程序设计之门####
探索Go语言如何以简洁高效的并发模型,重新定义现代软件开发的边界。本文将深入剖析Goroutines与Channels的工作原理,揭秘它们为何成为实现高并发、高性能应用的关键。跟随我们的旅程,从基础概念到实战技巧,一步步揭开Go并发编程的神秘面纱,让您的代码在多核时代翩翩起舞。 ####
|
2月前
|
JavaScript 前端开发 数据安全/隐私保护
Web开发者必看:手把手教你如何轻松播放m3u8流地址,解锁视频播放新技能,让你的项目更上一层楼!
【10月更文挑战第23天】随着互联网技术的发展,m3u8格式因良好的兼容性和高压缩率被广泛用于网络流媒体传输。本文介绍如何在Web端播放m3u8流地址,包括引入视频播放器(如Video.js)、创建播放器容器、初始化播放器及播放m3u8流的具体步骤。此外,还涉及处理加密m3u8流的示例。
539 1
|
3月前
|
JSON 搜索推荐 API
Python的web框架有哪些?小项目比较推荐哪个?
【10月更文挑战第15天】Python的web框架有哪些?小项目比较推荐哪个?
88 1