Unicorn 是什么? 1. 為 Rack 應用程序設計的 HTTP server 2. 是一個利用Unix的高級特性開發的 3. 為具備低延遲,高帶寬的連接的客戶服務
特性: 1. 為 Rack, Unix, 快速的客戶端和易調試而設計。 2. 完全兼容 Ruby 1.8 和 1.9。 3. 進程管理:Unicorn 會獲取和重啟因應用程序出錯導致死亡的任務,不需要自己管理多個進程和端口。Unicorn 可以產生和管理任何數量的任務進程。 4. 負載均衡完全由操作系統(Unix)核心完成。在繁忙的任務進程時,請求也不會堆積。 5. 不需要關心應用程序是否是線程安全的,workers 運行在特們自己獨立的地址空間,且一次只為一個客戶端服務。 6. 支持所有的 Rack 應用程序。 7. 使用 USR1 信號來固定重復打開應用程序的所有日志文件。Unicorn 也可以逐步的確定一個請求的多行日志放在同一個文件中。 8. nginx 式的二進制升級,不丟失連接。你可以升級 Unicorn、你的整個應用程序、庫、甚至 Ruby 編輯器而不丟失客戶端連接。 9. 在 fork 進程時如果由特殊需求可以使用 before_fork 和 after_fork 。如果“preload_app“ 為 false 時,則不能使用。 10. 可以使用 copy-on-wirte-friendly 內存管理來節約內容(通過設置 “preload_app" 為 true )。 11. 可以監聽多接口,包括:UNIX sockets,每個 worker process 也可以在簡單調試時通過 after_fork 鉤子綁定到私有的端口。 12. 配置使用簡單易用的 Ruby DSL。
Linux下Unicorn服務器安裝配置:
gem install unicorn
給工程創建一個unicorn配置文件
new_sxcoalts2.0/config/unicorn.rb 內容如下: app_path = "/work/new_sxcoalts2.0" #程序路徑 listen 3000 # 端口號 worker_processes 2 # cpu核數 pid "#{app_path}/tmp/pids/unicorn.pid" stderr_path "#{app_path}/log/unicorn.log" stdout_path "#{app_path}/log/unicorn.log" rails_env = 'production'
啟動: 進入到工程根目錄 cd /work/new_sxcoalts2.0/ unicorn_rails -c /work/new_sxcoalts2.0/config/unicorn.rb 參數-c 意思為執行后面配置文件里的內容
停止服務: 后臺服務: Kill 進程 命令行服務: ctrl + c
建立啟動,關閉服務: 創建工程配置文件夾: /etc/unicorn 在此目錄下添加所有需要的工程配置(可放置多個) 例如:project1.conf 內容為 RAILS_ROOT=/work/project1 RAILS_ENV=production
編寫unicorn 啟動腳本 在/etc/init.d/下建立unicorn_init 內容為 #!/bin/sh # # init.d script for single or multiple unicorn installations. Expects at least one .conf # file in /etc/unicorn # # Modified by jay@gooby.org http://github.com/jaygooby # based on http://gist.github.com/308216 by http://github.com/mguterl # ## A sample /etc/unicorn/new_sxcoalts_2.0.conf ## ## RAILS_ENV=production ## RAILS_ROOT=/var/apps/www/my_app/current # # This configures a unicorn master for your app at /var/apps/www/my_app/current running in # production mode. It will read config/unicorn.rb for further set up. # # You should ensure different ports or sockets are set in each config/unicorn.rb if # you are running more than one master concurrently. # # If you call this script without any config parameters, it will attempt to run the # init command for all your unicorn configurations listed in /etc/unicorn/*.conf # # /etc/init.d/unicorn start # starts all unicorns # # If you specify a particular config, it will only operate on that one # # /etc/init.d/unicorn start /etc/unicorn/new_sxcoalts_2.0.conf
set -e
sig () { test -s "$PID" && kill -$1 `cat "$PID"` }
oldsig () { test -s "$OLD_PID" && kill -$1 `cat "$OLD_PID"` }
cmd () {
case $1 in start) sig 0 && echo >&2 "Already running" && exit 0 echo "Starting" $CMD ;; stop) sig QUIT && echo "Stopping" && exit 0 echo >&2 "Not running" ;; force-stop) sig TERM && echo "Forcing a stop" && exit 0 echo >&2 "Not running" ;; restart|reload) sig USR2 && sleep 5 && oldsig QUIT && echo "Killing old master" `cat $OLD_PID` && exit 0 echo >&2 "Couldn't reload, starting '$CMD' instead" $CMD ;; upgrade) sig USR2 && echo Upgraded && exit 0 echo >&2 "Couldn't upgrade, starting '$CMD' instead" $CMD ;; rotate) sig USR1 && echo rotated logs OK && exit 0 echo >&2 "Couldn't rotate logs" && exit 1 ;; *) echo >&2 "Usage: $0 " exit 1 ;; esac }
setup () {
echo -n "$RAILS_ROOT: " cd $RAILS_ROOT || exit 1 export PID=$RAILS_ROOT/tmp/pids/unicorn.pid export OLD_PID="$PID.oldbin"
CMD="unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D" }
start_stop () {
# either run the start/stop/reload/etc command for every config under /etc/unicorn # or just do it for a specific one
# $1 contains the start/stop/etc command # $2 if it exists, should be the specific config we want to act on if [ $2 ]; then . $2 setup cmd $1 else for CONFIG in /etc/unicorn/*.conf; do # import the variables . $CONFIG setup
# run the start/stop/etc command cmd $1 done fi }
ARGS="$1 $2" start_stop $ARGS
執行命令: /etc/init.d/unicorn_init start/stop/restart
Unicorn 的詳細介紹:請點這里 Unicorn 的下載地址:請點這里
|