uWSGI伺服器集群的實現
實例7 集群的實現
這個實例我們針對的是集群,部署環境如下:
假設我們有兩台uWSGI伺服器,IP地址分別為192.168.3.139和192.168.3.34,監聽埠均9001。它們的應用程序目錄是通過NFS掛接實現的,及無論是IP 192.168.3.139所在的機器還是IP 192.168.3.34所在的機器上的uWSGI伺服器都從共同的NFS掛載目錄上獲取。其中主節點為192.168.3.139,而其它有再多的節點也是成員節點。
這裡需要說的一點是,uWSGI所說的集群和我們在以往討論的集群有著本質的區別,uWSGI所提到的集群是為了解決配置文件修改、應用修改而設計的。
下面我們看一下具體的實現:
兩台uWSGI的應用程序目錄均被掛接到:
/app/my_django
文件uwsgi.xml的內容:
# more uwsgi.xml
<uwsgi>
<listen>20</listen>
<master>true</master>
<uid>uwsgi</uid>
<gid>uwsgi</gid>
<processes>1</processes>
<module>wsgi</module>
<pythonpath>/app/my_django</pythonpath>
<profiler>true</profiler>
<memory-report>true</memory-report>
<enable-threads>true</enable-threads>
<logdate>true</logdate>
<limit-as>48</limit-as>
<daemonize>/usr/local/uwsgi-0.9.8-rc4/log/django.log</daemonize>
</uwsgi>
注意在這配置文件中沒有監聽的IP地址和埠號
在前端的Nginx伺服器上添加配置:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream uwsgicluster {
server 192.168.3.139:9001;
server 192.168.3.34:9001;
}
server {
listen 0.0.0.0:80;
server_name www.xx.com;
location / {
include uwsgi_params;
uwsgi_pass uwsgicluster;
}
}
在192.168.3.139機器:
在該機器上執行:
# ./uwsgi-0.9.8-rc4 -x /app/my_django/uwsgi.xml -s 192.168.3.*:9001 --cluster 225.1.1.1:3333
generate_socket_name(192.168.3.*:9001)
asterisk found
found 192.168.3.139:9001 for 192.168.3.*:9001 on interface eth0
parsing config file /app/my_django/uwsgi.xml
#
在192.168.3.34機器:
# ./uwsgi-0.9.8-rc4 --cluster 225.1.1.1:3333
joining multicast group: 225.1.1.1:3333
JOINED CLUSTER: 225.1.1.1:3333
asking "225.1.1.1:3333" uWSGI cluster for configuration data:
recevied request from 192.168.3.34
RLEN: 15
invalid uwsgi dictionary received, modifier1: 73 modifier2: 0
recevied request from 192.168.3.34
RLEN: 4
invalid uwsgi dictionary
recevied request from 192.168.3..139
RLEN: 340
0xbffe0d64 0xbffe0eb4 336
xmlconfig = /app/my_django/uwsgi.xml
socket = 192.168.3.*:9001
cluster = 225.1.1.1:3333
listen = 20
master = true
uid = uwsgi
gid = uwsgi
processes = 1
module = wsgi
pythonpath = /app/my_django
profiler = true
memory-report = true
enable-threads = true
logdate = true
limit-as = 48
daemonize = /usr/local/uwsgi-0.9.8-rc4/log/django.log
generate_socket_name(192.168.3.*:9001)
asterisk found
found 192.168.3.34:9001 for 192.168.3.*:9001 on interface eth0
這是在該機器上執行集群啟動后的信息,它的大致內容是說,uWSGI實例已經添加到組播地址為225.1.1.1:3333,並且獲取了配置。
在192.168.3.34所在的機器上執行完以上的啟動操作,那麼在192.168.3.139上會出現以下日誌:
Thu Jul 28 16:06:04 2011 - recevied request from 192.168.3.34
Thu Jul 28 16:06:04 2011 - request received 73 0
Thu Jul 28 16:06:04 2011 - new node available: nas
Thu Jul 28 16:06:04 2011 - recevied request from 192.168.3..34
Thu Jul 28 16:06:04 2011 - request received 99 0
Thu Jul 28 16:06:04 2011 - requested configuration data, sending 340 bytes
Thu Jul 28 16:06:04 2011 - recevied request from 192.168.3..139
Thu Jul 28 16:06:04 2011 - request received 99 1
Thu Jul 28 16:06:04 2011 - recevied request from 192.168.3..34
Thu Jul 28 16:06:04 2011 - request received 95 0
Thu Jul 28 16:06:04 2011 - hostname = nas
Thu Jul 28 16:06:04 2011 - address = 192.168.3.34:9001
Thu Jul 28 16:06:04 2011 - workers = 1
Thu Jul 28 16:06:04 2011 - requests = 0
Thu Jul 28 16:06:04 2011 - adding node
Thu Jul 28 16:06:04 2011 - 192.168.3.34
Thu Jul 28 16:06:04 2011 - added node 192.168.3.34:9001
看一下日誌的內容,它的大概意思是說在該機器上收到了192.168.3.34的請求,並且將其作為一個新的節點添加在「uWSGI cluster 225.1.1.1:3333」集群中,然後設定了它的埠和IP地址,以及其它的信息。
訪問測試
訪問http://www.xx.com/hello/,下面的測試效果是我們在測試故意設置的,即使用了不同的目錄,但是在具體使用uWSGI集群時不要這麼做。
這種輪詢的實現是由Nginx的配置實現的,它和uWSGI伺服器無關,我們要測試的是在主節點和成員節點正常啟動后都能夠正常的工作。
[火星人
]
uWSGI伺服器集群的實現已經有492次圍觀
http://coctec.com/docs/service/show-post-13767.html