在公司使用Rails写的Redmine系统,当连接人员数量较多的时候,系统性能严重恶化。
Google之后了解的到Rails server本身的性能有问题,可以通过使用unicorn web server解决大量并发的问题。
试了一下,安装unicorn还是非常简单的。
1. Rails App的目标下安装 unicorn 模块: gem install unicorn
2. 修改Gemfile,加入unicorn: gem "unicorn"
3. 在configure目录下,创建unicorn.rb 文件
4.unicorn.rb文件中配置unicorn Web server
--------------------------
# config/unicorn.rb
worker_processes 3
timeout 30
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to sent QUIT'
end
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
5. 启动unicorn Server
bundle exec unicorn -p $PORT -c ./config/unicorn.rb