• 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. #预编译Nginx

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

    11. #.configure预编译成功后,执行make命令进行编译

    12. make

    13. #make执行成功后,执行make install 正式安装

    14. make install

    15. #自此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. default_type application/octet-stream;

    15. fastcgi_intercept_errors on;

    16. charset utf-8;

    17. server_names_hash_bucket_size 128;

    18. client_header_buffer_size 4k;

    19. large_client_header_buffers 4 32k;

    20. client_max_body_size 300m;

    21. sendfile on;

    22. tcp_nopush on;

    23. keepalive_timeout 60;

    24. tcp_nodelay on;

    25. client_body_buffer_size 512k;

    26. proxy_connect_timeout 5;

    27. proxy_read_timeout 60;

    28. proxy_send_timeout 5;

    29. proxy_buffer_size 16k;

    30. proxy_buffers 4 64k;

    31. proxy_busy_buffers_size 128k;

    32. proxy_temp_file_write_size 128k;

    33. gzip on;

    34. gzip_min_length 1k;

    35. gzip_buffers 4 16k;

    36. gzip_http_version 1.1;

    37. gzip_comp_level 2;

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

    39. gzip_vary on;

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

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

    42. '$status $body_bytes_sent "$http_referer" '

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

    44. #这里为后端服务器wugk应用集群配置,根据后端实际情况修改即可,tdt_wugk为负载均衡名称,可以任意指定

    45. #但必须跟vhosts.conf虚拟主机的pass段一致,否则不能转发后端的请求。

    46. upstream tdt_wugk {

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

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

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

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

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

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

    53. }

    54. #这里为后端APP应用负载均衡配置,根据后端实际情况修改即可。tdt_app为负载均衡名称,可以任意指定

    55. upstream tdt_app {

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

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

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

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

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

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

    62. }

    63. #include引用vhosts.conf,该文件主要用于配置Nginx 虚拟主机

    64. include vhosts.conf;

    65. }

       如上nginx.conf配置完毕,继续配置nginx虚拟主机,继续在当前目录创建vhosts.conf

       vi vhosts.conf 内容如下:

    1. ####www.wuguangke.cn

    2. server

    3. {

    4. listen 80;

    5. server_name www.wuguangke.cn;

    6. index index.html index.htm;

    7. #配置发布目录为/data/www/wugk

    8. root /data/www/wugk;

    9. location /

    10. {

    11. proxy_next_upstream http_502 http_504 error timeout invalid_header;

    12. proxy_set_header Host $host;

    13. proxy_set_header X-Real-IP $remote_addr;

    14. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    15. proxy_pass http://tdt_wugk;

    16. expires 3d;

    17. }

    18. #动态页面交给http://tdt_wugk,也即我们之前在nginx.conf定义的upstream tdt_wugk 均衡

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

    20. {

    21. proxy_set_header Host $host;

    22. proxy_set_header X-Real-IP $remote_addr;

    23. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    24. proxy_pass http://tdt_wugk;

    25. }

    26. #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。

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

    28. {

    29. root /data/www/wugk;

    30. #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力

    31. expires 3d;

    32. }

    33. #定义Nginx输出日志的路径

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

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

    36. }

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

    38. server

    39. {

    40. listen 80;

    41. server_name chinaapp.sinaapp.com;

    42. index index.html index.htm;

    43. root /data/www;

    44. location /

    45. {

    46. proxy_next_upstream http_502 http_504 error timeout invalid_header;

    47. proxy_set_header Host $host;

    48. proxy_set_header X-Real-IP $remote_addr;

    49. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    50. proxy_pass http://tdt_app;

    51. expires 3d;

    52. }

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

    54. {

    55. proxy_set_header Host $host;

    56. proxy_set_header X-Real-IP $remote_addr;

    57. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    58. proxy_pass http://tdt_app;

    59. }

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

    61. {

    62. root /data/www/app;

    63. expires 3d;

    64. }

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

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

    67. }

    六、部署测试:

       后端配置好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

  • 相关阅读:
    vue 下拉框单选、多选以及默认值
    python 查询每周最后一个工作日
    微信开发
    win7 实用
    A Mixed Flash Translation Layer Structure for SLC-MLC Combined Flash Memory System
    暑假--升级攻击家庭wifi
    A New 3-bit Programming Algorithm using SLC-to-TLC Migration for 8MBs High Performance TLC NAND Flash Memory
    FTL方面综述
    Linux 脚本
    FTL-SLC&MTC&TLC
  • 原文地址:https://www.cnblogs.com/qmfsun/p/5403395.html
Copyright © 2020-2023  润新知