生产薄的最佳实践

时间:2012-05-09 14:42:16

标签: nginx rack thin faye

我正在使用Thin作为Faye的服务器。要做到这一点,我使用这样的东西:

require 'faye'
bayeux = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)
bayeux.listen(9292)

精简流程由God监控,一切都在开发中。

但是,我不确定这是否是生产配置的正确设置。我想知道的是这个设置(前面没有Nginx或HAProxy)将如何在生产环境中执行。

2 个答案:

答案 0 :(得分:1)

这是我的上帝配置。

#Faye
ports = [9292, 9293, 9294, 9295]
ports.each_with_index do |port, id|
  God.watch do |w|
    w.dir      = "#{rails_root}"
    w.name     = "faye-#{port}"
    w.group    = "faye"
    w.interval = 30.seconds
    w.start    = "thin start -R #{rails_root}/faye.ru -e production -p #{port} -P  #{rails_root}tmp/pids/faye-#{port}.pid"
    w.stop     = "thin stop -P  #{rails_root}tmp/pids/faye-#{port}.pid"
    w.log      = "#{rails_root}/log/god_node.log"

    #w.uid = 'server'
    #w.gid = 'server'

    # restart if memory usage is > 500mb
    w.transition(:up, :restart) do |on|
      on.condition(:memory_usage) do |c|
        c.above = 500.megabytes
        c.times = 2
      end
    end

    # determine the state on startup
    w.transition(:init, { true => :up, false => :start }) do |on|
      on.condition(:process_running) do |c|
        c.running = true
      end
    end

    # determine when process has finished starting
    w.transition([:start, :restart], :up) do |on|
      on.condition(:process_running) do |c|
        c.running = true
        c.interval = 10.seconds
      end

      # failsafe
      on.condition(:tries) do |c|
        c.times = 5
        c.transition = :start
        c.interval = 10.seconds
      end
    end

    # start if process is not running
    w.transition(:up, :start) do |on|
      on.condition(:process_running) do |c|
        c.running = false
      end
    end
  end
end

我正在使用nginx来加载平衡。

答案 1 :(得分:0)

我一直用瘦身来用redis来运行faye。确保设置

Faye::WebSocket.load_adapter('thin')

所有流量都通过haproxy(第一个流量被命名为代理,因为我将所有流量重定向到https)

frontend proxied
    bind 127.0.0.1:81 accept-proxy
    timeout client 86400000
    default_backend nginx_backend
    acl is_websocket hdr(Upgrade) -i WebSocket
    acl is_websocket hdr_beg(Host) -i ws
    use_backend socket_backend if is_websocket


backend nginx_backend
    balance roundrobin
    option forwardfor #except 127.0.0.1 # This sets X-Forwarded-For
    timeout server 30000
    timeout connect 4000
    server nginx1 localhost:8081 weight 1 maxconn 20000 check

backend socket_backend
    balance roundrobin
    option forwardfor except 127.0.0.1 # This sets X-Forwarded-For
    timeout queue 5000
    timeout server 86400000
    timeout connect 86400000
    server socket1 localhost:3100 weight 1 maxconn 20000 check
    server socket2 localhost:3101 weight 1 maxconn 20000 check
    server socket3 localhost:3102 weight 1 maxconn 20000 check
    server socket4 localhost:3103 weight 1 maxconn 20000 check
    ...

如果它是http流量,我通过nginx路由它,如果它包含/ faye路径,它将转发到同一组精简实例。

我不是haproxy专家,但这适用于websocket和长轮询连接。