背景
使用gin框架编写项目,每次修改完后,想验证效果时,总是忘了重启服务,导致验收时总是质疑人生
据闻beego是有热更新的,不过这里先不展开讨论
预研
于是寻思着是否有现成的办法实现了这个功能呢,毕竟,按理来说,应该不止我一个遇到这个问题,经过若干年的发展,go应该也成熟起来了,至少,应该有不少前人造了类似的轮子了。
百度了一下,说有若干种方法,其中,在github上star比较多的,应数 realize
和 gin
了(这里的gin
不是gin
框架),都看了一下两个的源码,虽然都已经有些年份没有维护更新了,但是realize
的最后维护时间至少比gin
要晚一些些
前期准备-安装realize
go开发前期准备
源码代码仓库:https://github.com/oxequa/realize
示例代码仓库:https://github.com/oxequa/realize-examples
安装指令
安装指令:go install github.com/oxequa/realize
遇到问题
但是!问题来了,安装的时候,会报以下错误:
go: github.com/oxequa/realize imports
gopkg.in/urfave/cli.v2: gopkg.in/urfave/cli.v2@v2.2.0: parsing go.mod:
module declares its path as: github.com/urfave/cli/v2
but was required as: gopkg.in/urfave/cli.v2
问题解决
找了一些代码仓库的issue,其中 Can’t get the realize package #262 中给出了解决方案
add this line in go.mod
replace gopkg.in/urfave/cli.v2 => github.com/urfave/cli/v2 v2.2.0
即:在go.mod文件中加入以下内容:
replace gopkg.in/urfave/cli.v2 => github.com/urfave/cli/v2 v2.2.0
于是先新建代码目录如下:
.
├── testServer
│ └── test.go
├── .realize.yaml
├── go.mod
└── go.sum
修改后的go.mod:
module test
go 1.17
require github.com/gin-gonic/gin v1.7.4
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
replace gopkg.in/urfave/cli.v2 => github.com/urfave/cli/v2 v2.2.0
编写项目代码
在上述所说的项目结构中的test.go
中,输入以下代码:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/test", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "success",
})
})
r.Run(":8111")
}
在.realize.yaml
文件中输入以下内容:
settings:
legacy:
force: false
interval: 100ms
server:
status: false
open: false
host: localhost
port: 4321 # realize服务的端口, 并非启动服务的端口号
schema:
- name: testServer
path: ./testServer # 执行文件的所在路径
commands: # 并非每个command都是必须的, 我一般只用到install和run
clean:
status: false
vet:
status: false
fmt:
status: false
test:
status: false
generate:
status: false
install:
status: false
build:
status: false
method: go build
args:
- -o testServer.exe
run:
status: true
args: # 额外参数, 主要是run的时候用到
- --key1=value1
- --key2=value2
watcher:
paths: # 监听文件变化的路径
- /
extensions: # 监听的文件的文件拓展类型
- go
ignored_paths: # 无需监听的路径
- vendor
scripts: # 执行的脚本
- type: before # 实行脚本的时机, 这里是启动前
command: tskill testServer # 和go.mod里面的package名称一样, 用来杀进程的, 避免端口占用, 重启失败
output: false
其他注意事项
project not found
如果报project not found
的话, 可以看看go.mod中的package名称和.realize.yaml
所在的文件夹名称是否一致, realize会根据当前所在的文件夹的名称去找启动项目, 但如果你的go.mod不一样, realize就找不到启动项目
也就是, 如果文件路径调整成这样:
testServer
├── test.go
├── .realize.yaml
├── go.mod
└── go.sum
则.realize.yaml
调整为
settings:
legacy:
force: false
interval: 100ms
server:
status: false
open: false
host: localhost
port: 4321 # realize服务的端口, 并非启动服务的端口号
schema:
- name: testServer
path: . # 执行文件的所在路径
commands: # 并非每个command都是必须的, 我一般只用到install和run
install:
status: false
run:
status: true
args: # 额外参数, 主要是run的时候用到
- --key1=value1
- --key2=value2
watcher:
paths: # 监听文件变化的路径
- /
extensions: # 监听的文件的文件拓展类型
- go
ignored_paths: # 无需监听的路径
- vendor
scripts: # 执行的脚本
- type: before # 实行脚本的时机, 这里是启动前
command: tskill testServer # 和go.mod里面的package名称一样, 用来杀进程的, 避免端口占用, 重启失败
output: false
go.mod
文件调整如下:
修改后的go.mod:
module testServer //这里调整了
go 1.17
require github.com/gin-gonic/gin v1.7.4
require (
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
replace gopkg.in/urfave/cli.v2 => github.com/urfave/cli/v2 v2.2.0
验证
- 启动服务
realize start
如果没有
.realize.yaml
文件,可以通过执行realize init
初始化 - 浏览器访问服务
http://localhost:8111/test
页面返回如下:
{"message":"success"}
- 修改go文件并保存,等待服务重启
修改test.go
:package main import ( "net/http" "github.com/gin-gonic/gin" ) func main() { r := gin.Default() r.GET("/test", func(c *gin.Context) { c.JSON(200, gin.H{ "message": "oh! success", }) }) r.Run(":8111") }
- 再次访问服务
http://localhost:8111/test
页面返回如下:
{"message":"oh! success"}