OpenFire集群配置,Nginx做負載均衡

来源:互联网 发布:c语言字符运算 编辑:程序博客网 时间:2024/05/16 09:52

從網上下載virtualbox並安裝

下載centos作为節點操作系統,我用的CentOS-6.5-x86_64-minimal.iso,這個系統只有398M,不會耽擱太多時間

安裝操作系統,为了好記我用ip地址作为虛擬機名稱251跑nginx,252和253分別跑一個openfire,如果你系統正確安裝後並運行後就應得到這個結果了QQ截圖20131206152402

下面配置每一台機器的ip地址,我們就拿251做为例子。先把這台虛擬機配置成橋接網卡,如圖QQ截圖20131206153830。進入linux系統,編輯/etc/sysconfig/network-scripts/eth0,如圖QQ截圖20131206154748。接下來配置首選DNS,編輯/etc/resolv.conf,如圖QQ截圖20131206155401。保存後執行 service network restart,用ifconfig檢查配置是否正確,如圖QQ截圖20131206154622。你也可以用你的宿主機ping一下這台虛擬機看能否訪問,配置252和253跟配置251是一样的,只是IP不同罷了。

在252和253中裝openfire,去openfire官網上下載最新版的openfire,如何下載有很多方法可以用wget去下載,如果你的系統中沒有wget,可以用yum –y install wget先安裝一個wget,你也可以用你的宿主機去下載然後傳到虛擬機中,怎麼傳到虛擬機中有很多方法ftp,virtualbox共享文件都可以,我用的ftp傳,怎麼在linux中安裝ftp網上文章很多。openfire安裝非常簡單,下載解壓即可。openfire需要有jdk才能運行,所以還需要在252和253中裝jdk,怎麼安裝jdk網上文章很多。jdk和openfire都裝好了後進入openfire安裝目錄的bin目錄下執行./openfire start就启動openfire了,用瀏覽器訪問http://go.rritw.com/192.168.1.252:9090就可以看到設置界面了,不過我們目前不設置,因为要用openfire集群,不能用openfire內置的數據庫,我們還要安裝一個mysql

为了方便,我就把mysql直接安裝在了我的宿主機上ip地址为192.168.1.250,openfire安裝包的\reources\database下有一個openfire_mysql.sql腳本文件,自己建立一個數據庫,名字隨便取,然後在這個數據庫中執行這個sql腳本。为了讓虛擬機能訪問你的宿主機mysql你要開启mysq用戶的遠程訪問權限。

進入http://go.rritw.com/192.168.1.252:9090正式開始配置openfireQQ截圖20131206161751選擇中文 ,QQ截圖20131206161912域名字隨便取不過每個節點的域都要一样,QQ截圖20131206162053如果是集群配置必須選擇標准數據庫,QQ截圖20131206162135配置你的mysql服務器,QQ截圖20131206162333配置管理账號,如果有可以直接跳過,集群配置只需要配置一個账號就行了,其他節點配置直接跳過這一步。如果一切沒問題你就應該用你的管理員账號登錄了,其他節點配置也類似

所有節點的配置都完成了之後接下來就是安裝集群插件了,每個節點安裝openfire插件都是一样的,進入http://go.rritw.com/192.168.1.252:9090QQ截圖20131206162905只需要一個Hazelcast Clustering Plugin就可以完成集群,如果你還想给所有用戶發送廣播信息還可以裝一個Broadcast插件

我只裝了Hazelcast QQ截圖20131206163100有效插件裏有很多免費插件供選擇,點擊+就安裝了,非常簡單,安裝完後QQ截圖20131206163212選擇启用就可以了,稍微等一段時間,如果一直沒有響應重启openfire就可以了,集群中的節點都要安裝Hazelcast Clustering Plugin插件,只要集群配置激活了他會自動掃描網络中安裝了Hazelcast Clustering Plugin插件的openfire並加入集群。集群配置成功後

如圖QQ截圖20131206163529

集群配置是非常簡單的,但是如何把我們的集群暴露给客戶端開發者呢?因为每個節點都有一個ip地址,我們不能在客戶端加入多個ip地址吧,所以我們需要一個負載均衡服務器來分流,客戶端只需要知道這個負載均衡服務器IP就可以了,他們不用關心集群中有多少節點。進入251虛擬機中,下載最新版本的nginx壓縮包,默認nginx只支持http數據包的負載均衡,为了讓nginx支持TCP數據包的負載均衡還需要http://go.rritw.com/github.com/yaoweibin/nginx_tcp_proxy_module這個項目,項目中的readme中說了如何配置,http://go.rritw.com/blog.csdn.net/kylinbl/article/details/8913311說了如何安裝,把nginx安裝好後基本上就算完成了,最後就是配置nginx,如何配置nginx負載均衡網上文章也非常多,我就貼出我的配置,以供参考


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

tcp  {
   timeout 1d;
   proxy_read_timeout 10d;
   proxy_send_timeout 10d;
   proxy_connect_timeout 30;
   upstream openfire5222{

    server 192.168.1.252:5222;
    server 192.168.1.253:5222;
    }
   server{

    listen 5222;
    proxy_pass openfire5222;
      so_keepalive on;
    tcp_nodelay on;
    }
   upstream openfire5223{
   
    server 192.168.1.252:5223;
    server 192.168.1.253:5223;
    }
   server{
    listen 5223;
        proxy_pass openfire5223;
    so_keepalive on;
    tcp_nodelay on;
   }

}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream openfire7070{
    server 192.168.1.252:7070;
    server 192.168.1.253:7070;
    }
    server{
    listen 7070;
    server_name localhost;
        location / {
      proxy_pass http://go.rritw.com/openfire7070;
    }
    }
    upstream openfire9090{
    server 192.168.1.252:9090;
    server 192.168.1.253:9090;
    ip_hash;
    }
    server {
    listen 9090;
    server_name localhost;
        location / {
        proxy_pass http://go.rritw.com/openfire9090;
    }
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

    location /status{
        tcp_check_status;
    }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://go.rritw.com/127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

启動nginx訪問http://go.rritw.com/192.168.1.251:9090看能否登錄到openfire,我們在251上沒有安裝openfire哦,如果能登錄證明9090端口分流正常

暴露给客戶端的接口是http://go.rritw.com/192.168.1.251:5222,用spark客戶端(只要支持XMPP協議的客戶端都可以)登錄一下你的管理員账號,如果看到這個界面就證明你的集群配置已經成功了!QQ截圖20131206165101

 

總結:

到目前为止,我們完成了openfire的集群和nginx的負載均衡,但是數據庫始終只有一個,如果在線用戶非常多的情況下,一個數據庫肯定是不行了

0 0