Golang制作web应用程序配置 [英] Golang production web application configuration

查看:136
本文介绍了Golang制作web应用程序配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些在生产中运行Go后端的人:



运行Go Web应用程序的堆栈/配置是什么?



除了使用标准库net / http包来保持服务器运行的人之外,我还没有在这个话题上看到太多内容。我读过使用Nginx将请求传递给Go服务器 - nginx with转到



这对我来说似乎有点脆弱。例如,如果机器重新启动(无需其他配置脚本),服务器将不会自动重新启动。



是否有更稳定的生产设置?



除了我的意图 - 我计划为我的下一个项目提供一个Go功能的REST后端服务器,并且希望确保Go在我投资之前能够实时启动项目很多。

解决方案

Go程序可以侦听端口80并直接提供HTTP请求。相反,您可能希望在Go程序前面使用反向代理,以便在端口80上侦听,并在端口4000上连接到您的程序。执行后者的原因很多:无需运行你的Go程序作为root用户,为同一主机上的其他网站/服务提供服务,SSL终止,负载均衡,日志记录等。

HAProxy 在前面。任何反向代理都可以工作。 Nginx也是一个很好的选择(比HAProxy更受欢迎,能够做更多的事情)。



如果您阅读了它的文档 HTML版本)。我的整个 haproxy.cfg 文件适用于我的一个Go项目,以防您需要一个开始的pont。

 全局
日志127.0.0.1 local0
maxconn 10000
用户haproxy
组haproxy
守护进程

默认值
日志全局
模式http
选项httplog
选项dontlognull
重试3
超时连接5000
超时客户端50000
超时服务器50000

前端的http
绑定:80
ACL is_stats HDR(主机)-i hastats.myapp.com
use_backend统计如果is_stats
default_backend MyApp的
捕获请求头主机len 20
捕获请求头Referer len 50

后端myapp
服务器主127.0.0.1:4000

后端统计数据
mo de http
stats enable
stats scope http
stats scope myapp
stats realm Haproxy\ Statistics
stats uri /
stats auth username:password

Nginx更容易。

关于服务控制,我将我的Go程序作为系统服务运行。我想每个人都这样做。我的服务器运行Ubuntu,所以它使用Upstart。我已经把它放在 /etc/init/myapp.conf 上,以控制我的程序:

 启动运行级别[2345] 
上的运行级别停止[!2345]

CHDIR /家庭/ MYAPP / MYAPP
setgid的MYAPP
的setuid MyApp的
EXEC ./myapp开始1 GT;> _logs / stdout.log 2 - ;> _logs / stderr.log

另一个方面是部署。一种选择是通过发送程序的二进制文件和必要的资源进行部署。这是一个非常好的IMO解决方案。我使用其他选项:在服务器上编译。 (当我建立所谓的持续集成/部署系统时,我将切换到使用二进制文件进行部署。)



我有一个小的shell脚本服务器,它从远程Git仓库为我的项目提供代码,使用Go构建它,将二进制文件和其他资产复制到〜/ myapp / ,然后重新启动服务。总的来说,整个事情与任何其他服务器设置并没有太大的区别:你必须有一种方法来运行代码并让它服务于HTTP请求。在实践中,Go已被证明对这件事非常稳定。


For those of you running Go backends in production:

What is your stack / configuration for running a Go web application?

I haven't seen much on this topic besides people using the standard library net/http package to keep a server running. I read using Nginx to pass requests to a Go server - nginx with Go

This seems a little fragile to me. For instance, the server would not automatically restart if the machine was restarted (without additional configuration scripts).

Is there a more solid production setup?

An aside about my intent - I'm planning out a Go powered REST backend server for my next project and want to make sure Go is going to be viable for launching the project live before I invest too much into it.

解决方案

Go programs can listen on port 80 and serve HTTP requests directly. Instead, you may want to use a reverse proxy in front of your Go program, so that it listens on port 80 and and connects to your program on port, say, 4000. There are many reason for doing the latter: not having to run your Go program as root, serving other websites/services on the same host, SSL termination, load balancing, logging, etc.

I use HAProxy in front. Any reverse proxy could work. Nginx is also a great option (much more popular than HAProxy and capable of doing more).

HAProxy is very easy to configure if you read its documentation (HTML version). My whole haproxy.cfg file for one of my Go projects follows, in case you need a starting pont.

global
        log     127.0.0.1       local0
        maxconn 10000
        user    haproxy
        group   haproxy
        daemon

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        timeout connect 5000
        timeout client  50000
        timeout server  50000

frontend http
        bind :80
        acl  is_stats  hdr(host)       -i      hastats.myapp.com
        use_backend    stats   if      is_stats
        default_backend        myapp
        capture        request header Host     len     20
        capture        request header Referer  len     50

backend myapp
        server  main    127.0.0.1:4000

backend stats
       mode     http
       stats    enable
       stats    scope   http
       stats    scope   myapp
       stats    realm   Haproxy\ Statistics
       stats    uri     /
       stats    auth    username:password

Nginx is even easier.

Regarding service control, I run my Go program as a system service. I think everybody does that. My server runs Ubuntu, so it uses Upstart. I have put this at /etc/init/myapp.conf for Upstart to control my program:

start on runlevel [2345]
stop on runlevel [!2345]

chdir /home/myapp/myapp
setgid myapp
setuid myapp
exec ./myapp start 1>>_logs/stdout.log 2>>_logs/stderr.log

Another aspect is deployment. One option is to deploy by just sending binary file of the program and necessary assets. This is a pretty great solution IMO. I use the other option: compiling on server. (I’ll switch to deploying with binary files when I set up a so-called "Continuous Integration/Deployment" system.)

I have a small shell script on the server that pulls code for my project from a remote Git repository, builds it with Go, copies the binaries and other assets to ~/myapp/, and restarts the service.

Overall, the whole thing is not very different from any other server setup: you have to have a way to run your code and have it serve HTTP requests. In practice, Go has proved to be very stable for this stuff.

这篇关于Golang制作web应用程序配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆