Nginx+Resin實現高性能JAVA平台搭建


現在流行JavaEE容器有很多:TomcatResin、JBossGlassfish等,我們常用的主要是前三種,那這個java容器性能方面到底誰更穩定,並發能力更強呢?那當屬resin了,resin官方發布了最新版本4.0.33版,號稱並發超越apache,不僅支持jsp語言,還能支持php。

   我們以前大多用過tomcat,知道tomcat如果要在一台機器配置多個服務,只需要拷貝不同的配置文件,然后配置不同的端口就ok,相比較而言,resin要想在一台機器配置多個服務,就比較麻煩了,那今天我們一起來研究一下。

 

一、系統環境

  1. 系統環境:CentOS6.0x86_64

  2. Nginx版本:Nginx-1.2.6

  3. Resin版本:Resin-4.0.33

  4. JDK版本: JDK-1.6.0_18

二、Nginx安裝

  1. #首先下載需要安裝的軟件:【JDK可以去java官網下載並安裝】

  2. wget http://nginx.org/download/nginx-1.2.6.tar.gz

  3. wget http://www.caucho.com/download/resin-4.0.33.tar.gz

  4. tar zxvf nginx-1.2.6.tar.gz

  5. cd nginx-1.2.6/

  6. ./configure--user=www --group=www --add-module=../ngx_cache_purge-1.4

  7. \--prefix=/usr/local/nginx\--with-http_stub_status_module --with-http_ssl_module

  8. make && make install

 

三、Resin安裝

  1. tar –xzvf resin-4.0.33.tar.gz

  2. cd resin-4.0.33 &&./configure --prefix=/usr/local/resin

  3. \--with-resin-log=/data/logs/resin/ --with-java-home=/usr/java/jdk1.6.0_18/

  4. make &&make install

 

四、配置Resin

  1. #cd /usr/local/resin/conf/下

  2. #添加如下代碼,在本機配置兩個實例端口為8080、8081

  3. <clusterid="app1">

  4. <!-- define the servers in the cluster -->

  5. <server-multiid-prefix="app1"address-list="${app1_servers}"port="6800"/> 

  6. <!-- the default host, matching any host name --> 

  7. <hostid=""root-directory="."> 

  8. <web-appid="/"root-directory="/var/www/html/app1"/>

  9. </host> 

  10. </cluster>

  11. <clusterid="app2"> 

  12. <!-- define the servers in the cluster --> 

  13. <server-multiid-prefix="app2"address-list="${app2_servers}"port="6801"/> 

  14. <!-- the default host, matching any host name -->

  15. <hostid=""root-directory=".">

  16. <web-appid="/"root-directory="/var/www/html/app2"/>

  17. </host>

  18. </cluster>

 

修改當前目錄vi  resin.properties 修改如下內容:

  1. # app-tier Triad servers: app-0 app-1 app-2

  2. app1_servers : 127.0.0.1:6800  

  3. app2_servers : 127.0.0.1:6801  

  4. # Use overrides for individual server control, for example: app-0.http : 8081  

  5. app1.http : 8080

  6. app2.http : 8081

 

配置不同的端口,然后啟動resin服務:

  1. /usr/local/resin/bin/resinctl start 即可

  2. 然后查看resin 端口 netstat –tnl 我們會看到8080、8081端口

五、配置Nginx

隨着Nginx高性能Web服務器大量被使用,目前Nginx最新穩定版為1.2.6,張宴兄在實際應用中大量使用Nginx,並分享Nginx高性能Web服務器知識,使得Nginx在國內也是飛速的發展。那今天咱們再來溫習一下Nginx 動靜分離知識,這里僅供參考。

一、實踐環境:

  1. 系統版本:CentOS6.0 X86_64  

  2. Nginx版本:Nginx-1.2.6

  3. Tomcat版本:Tomcat-6.0.18

二、Nginx安裝:

   實際環境中安裝Nginx,首先需要安裝pcre庫,然后再安裝Nginx:

  1. #安裝pcre支持rewrite庫,也可以安裝源碼,注*安裝源碼時,指定pcre路徑為解壓源碼的路徑,而不是編譯后的路徑,否則會報錯。

  2. yum install pcre-devel pcre -y

  3. #下載Nginx源碼包

  4. cd /usr/src ;wget -c http://nginx.org/download/nginx-1.2.6.tar.gz 

  5. #解壓Nginx源碼包

  6. tar -xzf nginx-1.2.6.tar.gz  

  7. #進入解壓目錄,然后sed修改Nginx版本信息為TDTWS

  8. cd nginx-1.2.6 ; sed -i -e 's/1.2.6//g' -e 's/nginx\//TDTWS/g' -e 's/"NGINX"/"TDTWS"/g' src/core/nginx.

  9.  

  10. #預編譯Nginx

  11. ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

  12. #.configure預編譯成功后,執行make命令進行編譯

  13. make

  14. #make執行成功后,執行make install 正式安裝

  15. make install

  16. #自此Nginx安裝完畢!!!

三、配置Nginx:

   這里鑒於我的51CTO博客已經有Tomcat安裝和配置了,這里忽略,只配置Nginx。

  1. #進入Nginx應用目錄

  2. cd /usr/local/nginx/conf

  3. #備份原nginx.conf文件

  4. mv nginx.conf nginx.bak

   創建 vi nginx.conf ,並寫入如下內容:

  1. user www www;

  2. worker_processes 8;

  3. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

  4. pid /usr/local/nginx/nginx.pid;  

  5. worker_rlimit_nofile 102400;

  6. events

  7. {

  8. use epoll;

  9. worker_connections 102400;

  10. }

  11. http

  12. {

  13. include mime.types;

  14.  

  15. default_type application/octet-stream;

  16.  

  17. fastcgi_intercept_errors on;

  18.  

  19. charset utf-8;

  20.  

  21. server_names_hash_bucket_size 128;

  22. client_header_buffer_size 4k;

  23. large_client_header_buffers 4 32k;

  24. client_max_body_size 300m;

  25.  

  26. sendfile on;

  27. tcp_nopush on;

  28.  

  29. keepalive_timeout 60;

  30.  

  31. tcp_nodelay on;

  32.  

  33. client_body_buffer_size 512k;

  34. proxy_connect_timeout 5;

  35. proxy_read_timeout 60;

  36. proxy_send_timeout 5;

  37. proxy_buffer_size 16k;

  38. proxy_buffers 4 64k;

  39. proxy_busy_buffers_size 128k;

  40. proxy_temp_file_write_size 128k;

  41.  

  42. gzip on;

  43. gzip_min_length 1k;

  44. gzip_buffers 4 16k;

  45. gzip_http_version 1.1;

  46. gzip_comp_level 2;

  47. gzip_types text/plain application/x-javascript text/css application/xml;

  48. gzip_vary on;

  49.  

  50.  

  51. ###2012-12-19 change nginx logs

  52.  

  53. log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '

  54. '$status $body_bytes_sent "$http_referer" '

  55. '"$http_user_agent" $request_time $remote_addr';

  56.  

  57. #這里為后端服務器wugk應用集群配置,根據后端實際情況修改即可,tdt_wugk為負載均衡名稱,可以任意指定

  58. #但必須跟vhosts.conf虛擬主機的pass段一致,否則不能轉發后端的請求。

  59. upstream tdt_wugk {

  60. server 10.10.141.30:8080 weight=1max_fails=2fail_timeout=30s;

  61. server 10.10.141.30:8081 weight=1max_fails=2fail_timeout=30s;

  62. server 10.10.141.31:8080 weight=1max_fails=2fail_timeout=30s;

  63. server 10.10.141.31:8081 weight=1max_fails=2fail_timeout=30s;

  64. server 10.10.141.32:8080 weight=1max_fails=2fail_timeout=30s;

  65. server 10.10.141.32:8081 weight=1max_fails=2fail_timeout=30s;

  66. }

  67. #這里為后端APP應用負載均衡配置,根據后端實際情況修改即可。tdt_app為負載均衡名稱,可以任意指定

  68. upstream tdt_app {

  69. server 10.10.141.40:8080 weight=1max_fails=2fail_timeout=30s;

  70. server 10.10.141.40:8081 weight=1max_fails=2fail_timeout=30s;

  71. server 10.10.141.41:8080 weight=1max_fails=2fail_timeout=30s;

  72. server 10.10.141.41:8081 weight=1max_fails=2fail_timeout=30s;

  73. server 10.10.141.42:8080 weight=1max_fails=2fail_timeout=30s;

  74. server 10.10.141.42:8081 weight=1max_fails=2fail_timeout=30s;

  75. }

  76. #include引用vhosts.conf,該文件主要用於配置Nginx 虛擬主機

  77. include vhosts.conf;

  78. }

   如上nginx.conf配置完畢,繼續配置nginx虛擬主機,繼續在當前目錄創建vhosts.conf

   vi vhosts.conf 內容如下:

  1. ####www.wuguangke.cn

  2. server

  3.  

  4. {

  5. listen 80;

  6. server_name www.wuguangke.cn;

  7. index index.html index.htm;

  8. #配置發布目錄為/data/www/wugk

  9. root /data/www/wugk;

  10.  

  11. location /

  12. {

  13. proxy_next_upstream http_502 http_504 error timeout invalid_header;

  14. proxy_set_header Host $host;

  15. proxy_set_header X-Real-IP $remote_addr;

  16. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  17. proxy_pass http://tdt_wugk;

  18. expires 3d;

  19. }

  20. #動態頁面交給http://tdt_wugk,也即我們之前在nginx.conf定義的upstream tdt_wugk 均衡

  21. location ~ .*\.(php|jsp|cgi)?$

  22. {

  23. proxy_set_header Host $host;

  24. proxy_set_header X-Real-IP $remote_addr;

  25. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  26. proxy_pass http://tdt_wugk;

  27. }

  28. #配置Nginx動靜分離,定義的靜態頁面直接從Nginx發布目錄讀取。

  29. location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$

  30. {

  31. root /data/www/wugk;

  32. #expires定義用戶瀏覽器緩存的時間為3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力

  33. expires 3d;

  34. }

  35. #定義Nginx輸出日志的路徑

  36. access_log /data/logs/nginx_wugk/access.log main;

  37. error_log /data/logs/nginx_wugk/error.log crit;

  38. }

  39.  

  40. ##########chinaapp.sinaapp.com 2012-12-19

  41. server

  42.  

  43. {

  44. listen 80;

  45. server_name chinaapp.sinaapp.com;

  46. index index.html index.htm;

  47. root /data/www;

  48.  

  49. location /

  50. {

  51. proxy_next_upstream http_502 http_504 error timeout invalid_header;

  52. proxy_set_header Host $host;

  53. proxy_set_header X-Real-IP $remote_addr;

  54. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  55. proxy_pass http://tdt_app;

  56. expires 3d;

  57. }

  58.  

  59. location ~ .*\.(php|jsp|cgi)?$

  60. {

  61. proxy_set_header Host $host;

  62. proxy_set_header X-Real-IP $remote_addr;

  63. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

  64. proxy_pass http://tdt_app;

  65. }

  66. location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$

  67. {

  68. root /data/www/app;

  69. expires 3d;

  70. }

  71.  

  72. access_log /data/logs/nginx_app/access.log main;

  73. error_log /data/logs/nginx_app/error.log crit;

  74. }

六、部署測試:

   后端配置好Tomcat服務,並啟動,發布的程序需同步到Nginx的/data/www對應的目錄,因為配置動靜分離后,用戶請求你定義的靜態頁面,默認會去nginx的發布目錄請求,而不會到后端請求,所以這時候你要保證后端跟前端的程序保持一致,可以使用Rsync做服務端自動同步。

  1. #檢查Nginx配置文件是否配置正確,提示Ok and successful表示正確,如下:

  2. [root@WEB-11-151 ~]# /usr/local/nginx/sbin/nginx -t

  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  5. #啟動Nginx服務

  6. /usr/local/nginx/sbin/nginx

  7. #查看Nginx進程是否啟動

  8. ps -ef |grep nginx

  本文只是一個簡單的實際案例,里面的配置和參數這里沒有做過多的說明,后期會繼續更新。

本文出自 “吳光科-專注自動化運維” 博客,請務必保留此出處http://wgkgood.blog.51cto.com/1192594/1094236


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM