Linux下使用Tomcat7搭建高性能Java服务器

1. 架构说明: nginx + 4个tomcat

nginx作为前端代理,并且肩负负载均衡的作用,多个tomcat可以解决单台服务器高并发的性能问题,至于后端放几个tomcat要看你的服务器有多大内存,我的服务器是4核的CPU,8G内存。

 

2. nginx安装简单说明:

编译参数:

--prefix=/opt/mysql/ --enable-assembler --with-extra-charsets=complex \

--enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server \

--enable-local-infile --with-plugins=innobase

可以根据自己的环境修改配置

 

nginx.conf配置:

 

Nginx conf代码 

 收藏代码

  1. user  www;
  2. worker_processes    8;
  3. error_log   /opt/logs/nginx/default/error/log;
  4. pid        logs/nginx.pid;
  5. worker_rlimit_nofile    51200;
  6. events {
  7.     use epoll;
  8.     worker_connections  1024; #有的设置到65535
  9. }
  10. http {
  11.     include mime.types;
  12.     default_type    application/octet-stream;
  13.     server_names_hash_bucket_size   128;
  14.     client_header_buffer_size       128k;
  15.     large_client_header_buffers 8   128k;
  16.     client_max_body_size    200m;
  17.     client_body_buffer_size 128k;
  18.     proxy_connect_timeout   600;
  19.     proxy_read_timeout  600;
  20.     proxy_send_timeout  600;
  21.     proxy_buffer_size   16k;
  22.     proxy_buffers   4   32k;
  23.     proxy_busy_buffers_size 64k;
  24.     log_format main '$remote_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
  25.     access_log   /opt/logs/nginx/default/access/log main;
  26.     sendfile       on;
  27.     tcp_nopush     on;
  28.     tcp_nodelay    on;
  29.     keepalive_timeout   65;
  30.     fastcgi_connect_timeout 300;
  31.     fastcgi_send_timeout 300;
  32.     fastcgi_read_timeout 300;
  33.     fastcgi_buffer_size 64k;
  34.     fastcgi_buffers 4 64k;
  35.     fastcgi_busy_buffers_size 128k;
  36.     fastcgi_temp_file_write_size 128k;
  37.     gzip  on;
  38.     gzip_min_length     1k;
  39.     gzip_buffers        4 16k;
  40.     gzip_comp_level     8;
  41.     gzip_http_version   1.1;
  42.     gzip_types      text/plain  application/xml;
  43.     gzip_vary       on;
  44.     proxy_temp_path /opt/nginx_cache/proxy_temp_path;
  45.     proxy_cache_path  /opt/nginx_cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
  46.     upstream testservers {
  47.         server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
  48.         server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
  49.         server 127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s;
  50.         server 127.0.0.1:8083 weight=1 max_fails=2 fail_timeout=30s;
  51.     }
  52.     server {
  53.         listen  80;
  54.         server_name test.com;
  55.         index index.html index.htm index.php index.jsp;
  56.         charset utf-8;
  57.         location ~ .*\.(js|jpg|JPG|jpeg|JPEG|bmp|gif|GIF)$ {
  58.             proxy_cache cache_one;
  59.             proxy_cache_valid 200 304 1h;
  60.             proxy_cache_valid 301 302 1m;
  61.             proxy_cache_valid any 1m;
  62.             proxy_cache_key $host$uri$is_args$args;
  63.             proxy_pass          http://testervers;
  64.             proxy_redirect      default;
  65.             proxy_set_header    X-Forwarded-For            $proxy_add_x_forwarded_for;
  66.             proxy_set_header    X-Real-IP $remote_addr;
  67.             proxy_set_header    Host $http_host;
  68.             proxy_set_header    Range $http_range;
  69.             proxy_next_upstream http_502 http_504 error timeout invalid_header;
  70.         }
  71.         location / {
  72.             proxy_pass          http://testservers;
  73.             proxy_redirect      default;
  74.             proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  75.             proxy_set_header    X-Real-IP $remote_addr;
  76.             proxy_set_header    Host $http_host;
  77.             proxy_set_header    Range $http_range;
  78.             proxy_next_upstream http_502 http_504 error timeout invalid_header;
  79.         }
Nginx conf代码 

 收藏代码

  1.                 # 对管理后台进行访问限制
  2.         location ^~/manager/ {
  3.             allow 你的IP;
  4.             deny all;
  5.             proxy_pass          http://testservers;
  6.             proxy_redirect      default;
  7.             proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
  8.             proxy_set_header    X-Real-IP $remote_addr;
  9.             proxy_set_header    Host $http_host;
  10.             proxy_set_header    Range $http_range;
  11.             proxy_next_upstream http_502 http_504 error timeout invalid_header;
  12.         }
  13.         }
  14.     }
  15. }

 

从上面配置可以看出,nginx会把http请求均匀分发给8080,8081,8082,8083几个端口的后端服务,实现负载均衡的效果。

 

3.  Tomcat 配置

 

server.xml配置,由于一个<Service></Service>会启动一个实例,所以我们这里要启动4个tomcat实例就设置4个Service :

 

Xml代码 

 收藏代码

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <Server port="8005" shutdown="SHUTDOWN">
  3.   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  4.   <Listener className="org.apache.catalina.core.JasperListener" />
  5.   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  6.   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  7.   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
  8.   <GlobalNamingResources>
  9.     <Resource name="UserDatabase" auth="Container"
  10.               type="org.apache.catalina.UserDatabase"
  11.               description="User database that can be updated and saved"
  12.               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  13.               pathname="conf/tomcat-users.xml" />
  14.   </GlobalNamingResources>
  15.   <Service name="Catalina0">
  16.     <Connector port="8080" protocol="HTTP/1.1"
  17.                minSpareThreads="100"
  18.                acceptCount="1000"
  19.                connectionTimeout="50000"
  20.                maxThreads="3000"
  21.                disableUploadTimeout="true"
  22.                URIEncoding="UTF-8"
  23.                redirectPort="8443" />
  24.     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
  25.     <Engine name="Catalina" defaultHost="localhost">
  26.       <Realm className="org.apache.catalina.realm.LockOutRealm">
  27.         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  28.                resourceName="UserDatabase"/>
  29.       </Realm>
  30.       <Host name="localhost"  appBase="/opt/www/webapps"
  31.             unpackWARs="true" autoDeploy="true">
  32.       </Host>
  33.     </Engine>
  34.   </Service>
  35.   <Service name="Catalina1">
  36.     <Connector port="8081" protocol="HTTP/1.1"
  37.                minSpareThreads="100"
  38.                acceptCount="1000"
  39.                connectionTimeout="50000"
  40.                maxThreads="3000"
  41.                disableUploadTimeout="true"
  42.                URIEncoding="UTF-8"
  43.                redirectPort="8444" />
  44.     <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
  45.     <Engine name="Catalina" defaultHost="localhost">
  46.       <Realm className="org.apache.catalina.realm.LockOutRealm">
  47.         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  48.                resourceName="UserDatabase"/>
  49.       </Realm>
  50.       <Host name="localhost"  appBase="/opt/www/webapps"
  51.             unpackWARs="true" autoDeploy="true">
  52.       </Host>
  53.     </Engine>
  54.   </Service>
  55.   <Service name="Catalina2">
  56.     <Connector port="8082" protocol="HTTP/1.1"
  57.                minSpareThreads="100"
  58.                acceptCount="1000"
  59.                connectionTimeout="50000"
  60.                maxThreads="3000"
  61.                disableUploadTimeout="true"
  62.                URIEncoding="UTF-8"
  63.                redirectPort="8445" />
  64.     <Connector port="8011" protocol="AJP/1.3" redirectPort="8445" />
  65.     <Engine name="Catalina" defaultHost="localhost">
  66.       <Realm className="org.apache.catalina.realm.LockOutRealm">
  67.         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  68.                resourceName="UserDatabase"/>
  69.       </Realm>
  70.       <Host name="localhost"  appBase="/opt/www/webapps"
  71.             unpackWARs="true" autoDeploy="true">
  72.       </Host>
  73.     </Engine>
  74.   </Service>
  75.   <Service name="Catalina3">
  76.     <Connector port="8083" protocol="HTTP/1.1"
  77.                minSpareThreads="100"
  78.                acceptCount="1000"
  79.                connectionTimeout="50000"
  80.                maxThreads="3000"
  81.                disableUploadTimeout="true"
  82.                URIEncoding="UTF-8"
  83.                redirectPort="8446" />
  84.     <Connector port="8011" protocol="AJP/1.3" redirectPort="8446" />
  85.     <Engine name="Catalina" defaultHost="localhost">
  86.       <Realm className="org.apache.catalina.realm.LockOutRealm">
  87.         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
  88.                resourceName="UserDatabase"/>
  89.       </Realm>
  90.       <Host name="localhost"  appBase="/opt/www/webapps"
  91.             unpackWARs="true" autoDeploy="true">
  92.       </Host>
  93.     </Engine>
  94.   </Service>
  95. </Server>

 

然后可以设置tomcat为Linux的服务,以便可以自动启动,创建权限为755的配置文件:/etc/init.d/tomcat,内容如下:

 

Shell代码 

 收藏代码

  1. #!/bin/bash
  2. #
  3. # tomcatd      This shell script takes care of starting and stopping
  4. #             standalone tomcat
  5. #
  6. # chkconfig: 345 91 10
  7. # description:  tomcat service
  8. # processname: tomcat
  9. # config file:
  10. # Source function library.
  11. . /etc/rc.d/init.d/functions
  12. # Source networking configuration.
  13. . /etc/sysconfig/network
  14. # Check that networking is up.
  15. [ ${NETWORKING} = "no" ] && exit 0
  16. prog=tomcat
  17. export JAVA_HOME=/opt/jdk
  18. export CATALINA_HOME=/opt/tomcat
  19. export CATALINA_OPTS='-Xms2048m -Xmx2048m'  #初始化JVM内存可以自己根据情况设置
Shell代码 

 收藏代码

  1. PATH=$PATH:$JAVA_HOME/bin
  2. STARTUP=$CATALINA_HOME/bin/startup.sh
  3. SHUTDOWN=$CATALINA_HOME/bin/shutdown.sh
  4. if [ ! -f $CATALINA_HOME/bin/startup.sh ]
  5. then
  6.         echo "CATALINA_HOME for tomcat not available"
  7.         exit
  8. fi
  9. start() {
  10.         # Start daemons.
  11.         echo -n $"Startting tomcat service: "
  12.         daemon $STARTUP
  13.         RETVAL=$?
  14.         return $RETVAL
  15. }
  16. stop() {
  17.         # Stop daemons.
  18.         echo -n $"Stoping tomcat service: "
  19.         $SHUTDOWN
  20.  RETVAL=$?
  21.         return $RETVAL
  22. }
  23. # See how we were called.
  24. case "$1" in
  25.   start)
  26.         start
  27.         ;;
  28.   stop)
  29.         stop
  30.         ;;
  31.   restart|reload)
  32.         stop
  33.         start
  34.         RETVAL=$?
  35.         ;;
  36.   status)
  37.         status $prog
  38.         RETVAL=$?
  39.         ;;
  40.   *)
  41.         echo $"Usage: $0 {start|stop|restart|status}"
  42.         exit 1
  43. esac
  44. exit $RETVAL

 

添加服务:chkconfig --add tomcat

设置自动启动:chkconfig tomcat on

另外,需要修改tomcat/bin/startup.sh,在#!/bin/sh下面添加:CATALINA_OPTS='-Xms2048m -Xmx2048m'

 

 

4. Tomcat全局filter解决乱码问题:

代码就不写了,说下过程,filter代码网上到处都是,我要说的在tomcat/conf/web.xml中增加filter配置,和在单独应用中增加一样,然后把filter的class打包jar文件放在tomcat/lib目录下即可。

本文摘自网络

标签: Linux下使用Tomcat7, Linux下搭建高性能Java服务器, Tomcat7搭建高性能Java服务器

添加新评论