Eli's Blog

1. Go 程序编译

1.1 交叉编译 (Cross Compiler)

在一个平台上,编译生成其他平台的可执行文件

1.2 Windows

1
2
3
4
5
SET GOS=darwin
SET GOS=linux
SET GOARCH=amd64

go build main.go

1.3 MacOS / Linux

1
2
3
4
5
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go

CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go

1.4 支持的操作系统和平台

1
go tool dist list

1.5 环境变量

1
go env

2. 程序部署

2.1 容器部署

2.1.1 编译

1
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o jsonrpc jsonrpc_server.go

2.1.2 Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM loads/alpine:3.8

ENV WORKDIR /var/www/admin

ADD ./jsonrpc $WORKDIR/main
RUN chmod +x $WORKDIR/main

# ADD public $WORKDIR/public
# ADD configs $WORKDIR/configs
# ADD templates $WORKDIR/templates

EXPOSE 8081

WORKDIR $WORKDIR

CMD ./main

2.1.3 构建镜像

1
2
3
docker build -t jsonrpc .

docker run -it jsonrpc /bin/bash

2.1.4 运行镜像

1
docker run --name myjsonrpc  -p 8081:8081 jsonrpc

2.2 独立部署

2.2.1 nohup

1
nohup ./jsonrpc &

2.2.2 tmux

terminal multiplexer(终端复用器)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
yum install -y tmux

# 启动命名tmux窗口
tmux new -s jsonrpc
./jsonrpc

# 分离会话
tmux ls
tmux detach

# 重接会话
tmux attach -t jsonrpc

# 杀死会话
tmux kill-session -t jsonrpc

# 切换会话
tmux switch -t jsonrpc2

# 其他命令
tmux info
tmux list-commands