部署Go语言程序的N种方式
<h1 style="margin: 1.2em auto; padding: 0; font-weight: bold; font-size: 1.8em; color: rgba(0, 150, 136, 1); text-align: center; border-bottom: 1px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">部署Go语言项目</span></h1><p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">本文以部署 Go Web 程序为例,介绍了在 CentOS7 服务器上部署 Go 语言程序的若干方法。</p>
<h2 style="margin: 1em auto; padding: 0 0 0 10px; font-weight: bold; font-size: 22px; color: rgba(0, 150, 136, 1); border-left: 3px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">独立部署</span></h2>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">Go 语言支持跨平台交叉编译,也就是说我们可以在 Windows 或 Mac 平台下编写代码,并且将代码编译成能够在 Linux amd64 服务器上运行的程序。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">对于简单的项目,通常我们只需要将编译后的二进制文件拷贝到服务器上,然后设置为后台守护进程运行即可。</p>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">编译</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">编译可以通过以下命令或编写 makefile 来操作。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o ./bin/bluebell
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">下面假设我们将本地编译好的 bluebell 二进制文件、配置文件和静态文件等上传到服务器的<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/data/app/bluebell</code>目录下。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">补充一点,如果嫌弃编译后的二进制文件太大,可以在编译的时候加上<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">-ldflags "-s -w"</code>参数去掉符号表和调试信息,一般能减小20%的大小。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"-s -w"</span> -o ./bin/bluebell
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">如果还是嫌大的话可以继续使用 upx 工具对二进制可执行文件进行压缩。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">我们编译好 bluebell 项目后,相关必要文件的目录结构如下:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">├── bin
│ └── bluebell
├── conf
│ └── config.yaml
├── static
│ ├── css
│ │ └── app.0afe9dae.css
│ ├── favicon.ico
│ ├── img
│ │ ├── avatar.7b0a9835.png
│ │ ├── iconfont.cdbe38a0.svg
│ │ ├── logo.da56125f.png
│ │ └── search.8e85063d.png
│ └── js
│ ├── app.9f3efa6d.js
│ ├── app.9f3efa6d.js.map
│ ├── chunk-vendors.57f9e9d6.js
│ └── chunk-vendors.57f9e9d6.js.map
└── templates
└── index.html
</code></pre>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">nohup</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">nohup 用于在系统后台<strong style="font-weight: bold; color: rgba(0, 0, 0, 1)">不挂断</strong>地运行命令,不挂断指的是退出执行命令的终端也不会影响程序的运行。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">我们可以使用 nohup 命令来运行应用程序,使其作为后台守护进程运行。由于在主流的 Linux 发行版中都会默认安装 nohup 命令工具,我们可以直接输入以下命令来启动我们的项目:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo nohup ./bin/bluebell conf/config.yaml > nohup_bluebell.log 2>&1 &
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">其中:</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc" data-tool="mdnice编辑器">
<li><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">./bluebell conf/config.yaml</code>是我们应用程序的启动命令</li>
<li><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">nohup ... &</code>表示在后台不挂断的执行上述应用程序的启动命令</li>
<li><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">> nohup_bluebell.log</code>表示将命令的标准输出重定向到 nohup_bluebell.log 文件</li>
<li><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">2>&1</code>表示将标准错误输出也重定向到标准输出中,结合上一条就是把执行命令的输出都定向到 nohup_bluebell.log 文件</li>
</ul>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">上面的命令执行后会返回进程 id</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"> 6338
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">当然我们也可以通过以下命令查看 bluebell 相关活动进程:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">ps -ef | grep bluebell
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">输出:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">root 633840480 08:43 pts/0 00:00:00 ./bin/bluebell conf/config.yaml
root 637640480 08:43 pts/0 00:00:00 grep --color=auto bluebell
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">此时就可以打开浏览器输入<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">http://服务器公网ip:端口</code>查看应用程序的展示效果了。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920091536683.png" alt="bluebell效果" style="display: block; margin: 0 auto; max-width: 100%">bluebell效果</p>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">supervisor</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">Supervisor 是业界流行的一个通用的进程管理程序,它能将一个普通的命令行进程变为后台守护进程,并监控该进程的运行状态,当该进程异常退出时能将其自动重启。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">首先使用 yum 来安装 supervisor:</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">如果你还没有安装过 EPEL,可以通过运行下面的命令来完成安装,如果已安装则跳过此步骤:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo yum install epel-release
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">安装 supervisor</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo yum install supervisor
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">Supervisor 的配置文件为:<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/supervisord.conf</code> ,Supervisor 所管理的应用的配置文件放在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/supervisord.d/</code> 目录中,这个目录可以在 supervisord.conf 中的<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">include</code>配置。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">
files = /etc/supervisord.d/*.conf
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">启动supervisor服务:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo supervisord -c /etc/supervisord.conf
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">我们在<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/supervisord.d</code>目录下创建一个名为<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">bluebell.conf</code>的配置文件,具体内容如下。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">;程序名称
user=root;执行程序的用户
<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">command</span>=/data/app/bluebell/bin/bluebell /data/app/bluebell/conf/config.yaml;执行的命令
directory=/data/app/bluebell/ ;命令执行的目录
stopsignal=TERM;重启时发送的信号
autostart=<span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span>
autorestart=<span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span>;是否自动重启
stdout_logfile=/var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-stdout.log;标准输出日志位置
stderr_logfile=/var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-stderr.log;标准错误日志位置
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">创建好配置文件之后,重启supervisor服务</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo supervisorctl update <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 更新配置文件并重启相关的程序</span>
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">查看bluebell的运行状态:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo supervisorctl status bluebell
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">输出:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">bluebell RUNNING pid 10918, uptime 0:05:46
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">最后补充一下常用的supervisr管理命令:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">supervisorctl status <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 查看所有任务状态</span>
supervisorctl shutdown <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 关闭所有任务</span>
supervisorctl start 程序名<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 启动任务</span>
supervisorctl stop 程序名 <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 关闭任务</span>
supervisorctl reload <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 重启supervisor</span>
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">接下来就是打开浏览器查看网站是否正常了。</p>
<h2 style="margin: 1em auto; padding: 0 0 0 10px; font-weight: bold; font-size: 22px; color: rgba(0, 150, 136, 1); border-left: 3px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">搭配nginx部署</span></h2>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">在需要静态文件分离、需要配置多个域名及证书、需要自建负载均衡层等稍复杂的场景下,我们一般需要搭配第三方的web服务器(Nginx、Apache)来部署我们的程序。</p>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">正向代理与反向代理</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">正向代理可以简单理解为客户端的代理,你访问墙外的网站用的那个属于正向代理。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920002334065.png" alt="正向代理" style="display: block; margin: 0 auto; max-width: 100%">正向代理</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">反向代理可以简单理解为服务器的代理,通常说的 Nginx 和 Apache 就属于反向代理。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920002443846.png" alt="反向代理" style="display: block; margin: 0 auto; max-width: 100%">反向代理</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">Nginx 是一个免费的、开源的、高性能的 HTTP 和反向代理服务,主要负责负载一些访问量比较大的站点。Nginx 可以作为一个独立的 Web 服务,也可以用来给 Apache 或是其他的 Web 服务做反向代理。相比于 Apache,Nginx 可以处理更多的并发连接,而且每个连接的内存占用的非常小。</p>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">使用yum安装nginx</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">EPEL 仓库中有 Nginx 的安装包。如果你还没有安装过 EPEL,可以通过运行下面的命令来完成安装:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo yum install epel-release
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">安装nginx</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo yum install nginx
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">安装完成后,执行下面的命令设置Nginx开机启动:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo systemctl <span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">enable</span> nginx
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">启动Nginx</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo systemctl start nginx
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">查看Nginx运行状态:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">sudo systemctl status nginx
</code></pre>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">Nginx配置文件</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">通过上面的方法安装的 nginx,所有相关的配置文件都在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/nginx/</code> 目录中。Nginx 的主配置文件是 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/nginx/nginx.conf</code>。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">默认还有一个<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">nginx.conf.default</code>的配置文件示例,可以作为参考。你可以为多个服务创建不同的配置文件(建议为每个服务(域名)创建一个单独的配置文件),每一个独立的 Nginx 服务配置文件都必须以 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">.conf</code>结尾,并存储在 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">/etc/nginx/conf.d</code> 目录中。</p>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">Nginx常用命令</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">补充几个 Nginx 常用命令。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">nginx -s stop <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 停止 Nginx 服务</span>
nginx -s reload<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 重新加载配置文件</span>
nginx -s quit <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 平滑停止 Nginx 服务</span>
nginx -t <span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 测试配置文件是否正确</span>
</code></pre>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">Nginx反向代理部署</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">我们推荐使用 nginx 作为反向代理来部署我们的程序,按下面的内容修改 nginx 的配置文件。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server {
listen 80;
server_namelocalhost;
access_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-access.log;
error_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-error.log;
location / {
proxy_pass http://127.0.0.1:8084;
proxy_redirect off;
proxy_set_header Host <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$host</span>;
proxy_set_header X-Real-IP <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$remote_addr</span>;
proxy_set_header X-Forwarded-For<span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$proxy_add_x_forwarded_for</span>;
}
}
}
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">执行下面的命令检查配置文件语法:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">nginx -t
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">执行下面的命令重新加载配置文件:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">nginx -s reload
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">接下来就是打开浏览器查看网站是否正常了。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">当然我们还可以使用 nginx 的 upstream 配置来添加多个服务器地址实现负载均衡。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
upstream backend {
server 127.0.0.1:8084;
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 这里需要填真实可用的地址,默认轮询</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">#server backend1.example.com;</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">#server backend2.example.com;</span>
}
server {
listen 80;
server_namelocalhost;
access_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-access.log;
error_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-error.log;
location / {
proxy_pass http://backend/;
proxy_redirect off;
proxy_set_header Host <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$host</span>;
proxy_set_header X-Real-IP <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$remote_addr</span>;
proxy_set_header X-Forwarded-For<span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$proxy_add_x_forwarded_for</span>;
}
}
}
</code></pre>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">Nginx分离静态文件请求</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">上面的配置是简单的使用 nginx 作为反向代理处理所有的请求并转发给我们的 Go 程序处理,其实我们还可以有选择的将静态文件部分的请求直接使用 nginx 处理,而将 API 接口类的动态处理请求转发给后端的 Go 程序来处理。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920002735894.png" alt="分离静态文件请求图示" style="display: block; margin: 0 auto; max-width: 100%">分离静态文件请求图示</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">下面继续修改我们的 nginx 的配置文件来实现上述功能。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch">worker_processes1;
events {
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
sendfile on;
keepalive_timeout65;
server {
listen 80;
server_namebluebell;
access_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-access.log;
error_log /var/<span class="hljs-built_in" style="color: rgba(0, 134, 179, 1); line-height: 26px">log</span>/bluebell-error.log;
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 静态文件请求</span>
location ~ .*\.(gif|jpg|jpeg|png|js|css|eot|ttf|woff|svg|otf)$ {
access_log off;
expires 1d;
root /data/app/bluebell;
}
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># index.html页面请求</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># 因为是单页面应用这里使用 try_files 处理一下,避免刷新页面时出现404的问题</span>
location / {
root /data/app/bluebell/templates;
index index.html;
try_files <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$uri</span> <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$uri</span>/ /index.html;
}
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px"># API请求</span>
location /api {
proxy_pass http://127.0.0.1:8084;
proxy_redirect off;
proxy_set_header Host <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$host</span>;
proxy_set_header X-Real-IP <span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$remote_addr</span>;
proxy_set_header X-Forwarded-For<span class="hljs-variable" style="color: rgba(0, 128, 128, 1); line-height: 26px">$proxy_add_x_forwarded_for</span>;
}
}
}
</code></pre>
<h3 style="margin: 0.6em auto; padding: 0 0 0 10px; font-weight: bold; color: rgba(0, 0, 0, 1); font-size: 20px; border-left: 2px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">前后端分开部署</span></h3>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">前后端的代码没必要都部署到相同的服务器上,也可以分开部署到不同的服务器上,下图是前端服务将 API 请求转发至后端服务的方案。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920003753373.png" alt="前后端分开部署方案1" style="display: block; margin: 0 auto; max-width: 100%">前后端分开部署方案1</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">上面的部署方案中,所有浏览器的请求都是直接访问前端服务,而如果是浏览器直接访问后端API服务的部署模式下,如下图。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">此时前端和后端通常不在同一个域下,我们还需要在后端代码中添加跨域支持。</p>
<p><img src="https://www.liwenzhou.com/images/Go/deploy_go_app/image-20200920003335577.png" alt="前后端分开部署方案2" style="display: block; margin: 0 auto; max-width: 100%">前后端分开部署方案2</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">这里使用github.com/gin-contrib/cors库来支持跨域请求。</p>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">最简单的允许跨域的配置是使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">cors.Default()</code>,它默认允许所有跨域请求。</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span> <span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">main</span><span class="hljs-params" style="line-height: 26px">()</span></span> {
router := gin.Default()
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// same as</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// config := cors.DefaultConfig()</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// config.AllowAllOrigins = true</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// router.Use(cors.New(config))</span>
router.Use(cors.Default())
router.Run()
}
</code></pre>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">此外,还可以使用<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">cors.Config</code>自定义具体的跨域请求相关配置项:</p>
<pre class="custom" data-tool="mdnice编辑器"><code class="hljs" style="overflow-x: auto; padding: 16px; color: rgba(51, 51, 51, 1); background: rgba(248, 248, 248, 1); display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; border-radius: 0; font-size: 12px; -webkit-overflow-scrolling: touch"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">package</span> main
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">import</span> (
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"time"</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"github.com/gin-contrib/cors"</span>
<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"github.com/gin-gonic/gin"</span>
)
<span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span> <span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">main</span><span class="hljs-params" style="line-height: 26px">()</span></span> {
router := gin.Default()
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// CORS for https://foo.com and https://github.com origins, allowing:</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// - PUT and PATCH methods</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// - Origin header</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// - Credentials share</span>
<span class="hljs-comment" style="color: rgba(153, 153, 136, 1); font-style: italic; line-height: 26px">// - Preflight requests cached for 12 hours</span>
router.Use(cors.New(cors.Config{
AllowOrigins: []<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">string</span>{<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"https://foo.com"</span>},
AllowMethods: []<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">string</span>{<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"PUT"</span>, <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"PATCH"</span>},
AllowHeaders: []<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">string</span>{<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"Origin"</span>},
ExposeHeaders: []<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">string</span>{<span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"Content-Length"</span>},
AllowCredentials: <span class="hljs-literal" style="color: rgba(0, 128, 128, 1); line-height: 26px">true</span>,
AllowOriginFunc: <span class="hljs-function" style="line-height: 26px"><span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">func</span><span class="hljs-params" style="line-height: 26px">(origin <span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">string</span>)</span> <span class="hljs-title" style="color: rgba(153, 0, 0, 1); font-weight: bold; line-height: 26px">bool</span></span> {
<span class="hljs-keyword" style="color: rgba(51, 51, 51, 1); font-weight: bold; line-height: 26px">return</span> origin == <span class="hljs-string" style="color: rgba(221, 17, 68, 1); line-height: 26px">"https://github.com"</span>
},
MaxAge: <span class="hljs-number" style="color: rgba(0, 128, 128, 1); line-height: 26px">12</span> * time.Hour,
}))
router.Run()
}
</code></pre>
<h2 style="margin: 1em auto; padding: 0 0 0 10px; font-weight: bold; font-size: 22px; color: rgba(0, 150, 136, 1); border-left: 3px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">容器部署</span></h2>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">容器部署方案可参照我之前的博客:使用Docker和Docker Compose部署Go Web应用,这里就不再赘述了。</p>
<h2 style="margin: 1em auto; padding: 0 0 0 10px; font-weight: bold; font-size: 22px; color: rgba(0, 150, 136, 1); border-left: 3px solid rgba(0, 150, 136, 1)" data-tool="mdnice编辑器"><span class="content">关于 bluebell</span></h2>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">上文提及的 bluebell 项目是我个人录制的 Go Web开发进阶项目实战课程中的项目,目前该课程已经录制完成,共计80课时。想要了解该课程的同学欢迎猛戳以下链接:</p>
<ul style="margin-top: 8px; margin-bottom: 8px; padding-left: 25px; color: rgba(0, 0, 0, 1); list-style-type: disc" data-tool="mdnice编辑器">
<li>网易云课堂课程链接</li>
<li>51CTO课程链接</li>
</ul>
<p style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">或者可以关注我的个人微信公众号:<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">李文周</code>,回复<code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27, 31, 35, 0.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: rgba(0, 150, 136, 1)">bluebell</code>获取相关课程链接。</p>
<p id="nice-suffix-juejin-container" class="nice-suffix-juejin-container" style="font-size: 16px; padding-top: 8px; padding-bottom: 8px; margin-top: 20px !important; margin-right: 0; margin-bottom: 0; margin-left: 0; line-height: 26px; color: rgba(0, 0, 0, 1); text-align: justify" data-tool="mdnice编辑器">本文首发于我的个人博客:liwenzhou.com,点击查看最新的免费Go语言教程。</p><br><br>
来源:https://www.cnblogs.com/liwenzhou/p/13699228.html
頁:
[1]