业务场景: win10 64位下开发go程序,部署在linux环境下发布.
supervisor和go的相关介绍,在此不再赘述,观看本文的读者,最好有一定的go基础,linux命令基础。
##目录
一.helloworld创建与编译
二.linux安装supervisor
三.上传和发布
四.常见错误
#####一.HelloWorld创建与编译
ide环境:
- goland
- git for windows
- go 1.10
代码
package main
import "github.com/gin-gonic/gin"
func main() {
router:= gin.Default()
router.GET("/sayhello",sayHello)
router.Run(":8899")
}
func sayHello(c *gin.Context){
c.JSON(200,"Hello")
return
}
测试,开启服务,在本地浏览器上输入和返回:
交叉编译
cd 到该项目路径下,git bash here
GOOS表示目标系统,GOARCH是编译的go版本,-o是是指定交叉编译之后的名字
windows下的准备工作基本做完了,没有完全做完,还要有ssh-key需要配,这里先假定已经配好了,有运维的让运维帮你配,自己配请百度
#####二.linux安装supervisor(已经装配好的可以跳过该步骤)
在git中输入
ssh xxx@xxx.xxx.xxx.xx //服务端/虚拟机地址,比如ft@110.110.110.110
sudo yum install supervisor
安装
sudo systemctl enable supervisor.service
开机启动
sudo supervisord -c /etc/supervisord.conf
启动supervisor服务
来适配helloworld吧
cd /etc/supervisord.d/config.d
ll
vim helloworld.ini
//内容修改(添加)为:
[program:helloworld]
directory = /home/tonnn/helloworld/
command = /home/tonnn/helloworld/helloworld
autorestart = true
redirect_stderr = true
stdout_logfile = /data/log/helloworld.log
简约介绍一下,[program:helloworld]是文件头,勿缺,会报错,文件头错误,directory是交叉编译的文件所在路径,command是supervisor管理时执行的命令文件本体,autorestart自动重启,重定向错误,输出日志路径
directory和stdout_logfile 请预先使用mkdir和vim创建好,不然会报错误哦
mkdir /home/tonnn/helloworld
vim /data/log/helloworld.log
//输入一点注释
[log for helloworld]
然后就是最重要的supervisor重启了!
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl -c /etc/supervisor/supervisord.conf
#####三.上传和发布
在交叉编译的helloworld项目路径下,git bash here
scp -P 22 helloworld xx@xxx.xxx.xxx.xxx:/home/tonnn/helloworld
新起一个git bash here
ssh xx@xxx.xxx.xxx.xxx
cd /home/tonnn/helloworld
ll
chmod 777 helloworld
supervisorctl start helloworld
结束!
从本机,访问xxxx.xxxx.xxx.xxx:8899/sayhello
部署成功!!!!!!!!
以后如果有hello2,hello3项目,只需要执行几步:
GOOS=linux GOARCH=amd64 go build -o hellox
scp -P 22 hellox xxxx@xxx.xxx.xxx.xxx:/home/tonnn/projects
ssh xxxx@xxx.xxx.xxx.xxx
cd /home/tonnn/projects
supervisorctl restart hellox
PS
- 对已有项目修改上传时,报无法上传,或xxx占用,建议先上传到/tmp/projects,然后执行,即先上传,再复制覆盖原本的,并修改执行权限
sudo cp -f /tmp/projects/hello /home/tonnn/projects/hello
sudo chmod 777 /home/tonnn/projects/hello
#####四.常见错误 - 执行supervisorctl update 和supervisorctl reload时, 出现
Connection refused: file: /usr/lib64/python2.7/socket.py line: 224
这是因为supervisor还没启动,使用下行启动
supervisord -c /etc/supervisor/supervisord.conf
2. 执行supervisord -c /etc/supervisor/supervisord.conf时,出现
Error: File contains no section headers.
file: /etc/supervisor/config.d/helloworld.ini, line: 1
'directory = /home/tonnn/helloworld/\n'
For help, use /usr/bin/supervisord -h
这是因为,helloworld.ini的头没带上
- 出现文件路径找不到,比如执行supervisord -c /etc/supervisor/supervisord.conf,出现
Error: The directory named as part of the path /data/log/helloworld.log does not exist in section ‘program:helloworld’ (file: ‘/etc/supervisor/config.d/helloworld.ini’)
For help, use /usr/bin/supervisord -h
这是因为helloworld.ini或者helloworld.log找不到该文件,我们在conf.d里的helloworld.ini内容特地强调了log路径和它本身要建好。
4.执行supervisorctl start hello 出现 no process error
这是因为,supervisord还没有启动,依次执行
supervisord -c /etc/supervisor/supervisord.conf
supervisorctl -c /etc/supervisor/supervisord.conf
5.出现各种未知错误,请保持修改之后执行
supervisorctl reload和supervisorctl update的良好习惯。
下一节讲如何将复杂的部署一键化