railsをApache + mod_proxy_balancer + mongrel_rails_cluster で運用
[Ruby]
Ruby on Railsアプリケーションの負荷を分散させて運用するため,
Apacheのモジュールであるmod_proxy_balancerを入り口として
RubyのHTTPサーバmongrel上でRoRアプリを動作させるようにしました.
Apacheのバージョンは2.2系を利用し,
http://sub.mydomain.com/ のような仮想サーバ上で運用します.
まずは,mongrel,mongrel_clusterのインストールから.
# gem install mongrel --include-dependencies
# gem install mongrel_cluster --include-dependencies
いくつのmongrelサーバを起動するか設定します.
# mongrel_rails cluster::configure -e production -p 4000 -N 3
サーバの起動
# mongrel_rails cluster::start
次に,Apacheの設定ファイルを編集します.
仮想サーバの設定と負荷分散の設定を行います.
/etc/httpd/conf/httpd.conf 辺りに以下を追加しました.
mydomain.com辺りは自分の環境に置き換えてください.
NameVirtualHost *:80<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName www.mydomain.com
</VirtualHost><VirtualHost *:80>
ProxyPreserveHost On
ServerName sub.mydomain.com
ProxyRequests Off<Proxy *>
Order deny,allow
Allow from all
</Proxy>ProxyPass / balancer://test/
ProxyPassReverse / balancer://test/
<Proxy balancer://test/>
BalancerMember http://127.0.0.1:4000 loadfactor=10
BalancerMember http://127.0.0.1:4001 loadfactor=10
BalancerMember http://127.0.0.1:4002 loadfactor=10
</Proxy>
</VirtualHost>
Apacheの設定ファイルを読み込みます.
# /etc/rc.d/init.d/httpd reload
これで,http://sub.mydomain.com/にアクセスすると
RoRアプリが起動します.
自分の場合,Routing Errorが出て悩みました.
Apache設定ファイルの
「BalancerMember http://127.0.0.1:4000」を
「BalancerMember http://127.0.0.1:4000/」と書いていたのが原因.
スラッシュ一つで動かなくなるので注意.
[Ruby]
RubyからMySQLを使いましょう。
これも超簡単です。
require 'mysql'
my = Mysql::new("localhost","user","pass","dbname")
my.query("クエリ文")
これだけですね?
データの挿入例
my.query("INSERT INTO `table` (`name`, `address` , `sex` , `birth` ) VALUES ('#{name}' , '#{address}', '#{sex}', '#{birth}');")
2005,07,24 : 23:47 | 修正 | トラックバック (0)
[Ruby]
Rubyはライブラリが豊富で、非常に簡単にプログラムを組めます。
CGIはPerlよりも使いやすいと思います。
流行のオブジェクト指向にもしっかり対応してますし。
Ruby Tips
1.HTTP通信
require 'net/http'
http = Net::HTTP.new('www.somewhere.com',80).start
res = http.get("/target.html")
print res.body
なんとこれだけ!
2.文字列のコード変換
require 'kconv'
print string.toeuc
なんとこれだけ!文字列クラスのtoeucメソッドを実行するだけ。
3.ファイルへ書き込み
f = File.open("data.txt","w+")
f.print(value)
Rubyは他の言語に比べても非常にソースが短くてすみます。
Perlよりは間違いなく使いやすくなっているでしょう。
初心者にはRubyがオススメです。
2005,07,21 : 23:15 | 修正 | トラックバック (0)
[Ruby]
Perl/Tkは日本語に対応していないことが判明したので、
Ruby/Tkを勉強することにする。
また同じ様なブラウザを作ってみた。
ソースコードは以下のようになった。
#!/usr/bin/ruby
require 'tk'
require 'socket'
class HttpConnect
def initialize(host,port)
@host = host
@port = port
@sock = TCPSocket.open(@host,@port)
@buffer = ""
end
def sndmsg(msg)
print @sock.write(msg)
end
def rcvmsg
while @sock.gets;
@buffer = @buffer + $_
end
return @buffer
end
def close
@sock.close
end
end
url = TkVariable.new("http://")
TkEntry.new(nil,'width'=>50,'textvariable'=>url).pack
t = TkText.new.pack
TkButton.new(nil,'text'=>'GET','command'=>proc{
/^http:\/\/([^\/]+)\/(.*)/ =~ url.value;
myconnect = HttpConnect.new($1,"http");
myconnect.sndmsg "GET /#{$2} HTTP/1.0\r\n\r\n";
myval = myconnect.rcvmsg;
t.insert('end', myval);
myconnect.close}).pack
Tk.mainloop
2005,07,13 : 23:24 | 修正 | トラックバック (0)
