Linux下使用Tomcat7搭建高性能Java服务器 应用服务器LinuxJavaTomcatnginx
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配置:
user www; worker_processes 8; error_log /opt/logs/nginx/default/error/log; pid logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 1024; #有的设置到65535 } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 128k; large_client_header_buffers 8 128k; client_max_body_size 200m; client_body_buffer_size 128k; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; log_format main '$remote_addr $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"'; access_log /opt/logs/nginx/default/access/log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 8; gzip_http_version 1.1; gzip_types text/plain application/xml; gzip_vary on; proxy_temp_path /opt/nginx_cache/proxy_temp_path; proxy_cache_path /opt/nginx_cache/proxy_cache_path levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; upstream testservers { server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:8082 weight=1 max_fails=2 fail_timeout=30s; server 127.0.0.1:8083 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name test.com; index index.html index.htm index.php index.jsp; charset utf-8; location ~ .*\.(js|jpg|JPG|jpeg|JPEG|bmp|gif|GIF)$ { proxy_cache cache_one; proxy_cache_valid 200 304 1h; proxy_cache_valid 301 302 1m; proxy_cache_valid any 1m; proxy_cache_key $host$uri$is_args$args; proxy_pass http://testervers; proxy_redirect default; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header Range $http_range; proxy_next_upstream http_502 http_504 error timeout invalid_header; } location / { proxy_pass http://testservers; proxy_redirect default; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header Range $http_range; proxy_next_upstream http_502 http_504 error timeout invalid_header; }
# 对管理后台进行访问限制 location ^~/manager/ { allow 你的IP; deny all; proxy_pass http://testservers; proxy_redirect default; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; proxy_set_header Range $http_range; proxy_next_upstream http_502 http_504 error timeout invalid_header; } } } }
从上面配置可以看出,nginx会把http请求均匀分发给8080,8081,8082,8083几个端口的后端服务,实现负载均衡的效果。
3. Tomcat 配置
server.xml配置,由于一个<Service></Service>会启动一个实例,所以我们这里要启动4个tomcat实例就设置4个Service :
<?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JasperListener" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina0"> <Connector port="8080" protocol="HTTP/1.1" minSpareThreads="100" acceptCount="1000" connectionTimeout="50000" maxThreads="3000" disableUploadTimeout="true" URIEncoding="UTF-8" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="/opt/www/webapps" unpackWARs="true" autoDeploy="true"> </Host> </Engine> </Service> <Service name="Catalina1"> <Connector port="8081" protocol="HTTP/1.1" minSpareThreads="100" acceptCount="1000" connectionTimeout="50000" maxThreads="3000" disableUploadTimeout="true" URIEncoding="UTF-8" redirectPort="8444" /> <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="/opt/www/webapps" unpackWARs="true" autoDeploy="true"> </Host> </Engine> </Service> <Service name="Catalina2"> <Connector port="8082" protocol="HTTP/1.1" minSpareThreads="100" acceptCount="1000" connectionTimeout="50000" maxThreads="3000" disableUploadTimeout="true" URIEncoding="UTF-8" redirectPort="8445" /> <Connector port="8011" protocol="AJP/1.3" redirectPort="8445" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="/opt/www/webapps" unpackWARs="true" autoDeploy="true"> </Host> </Engine> </Service> <Service name="Catalina3"> <Connector port="8083" protocol="HTTP/1.1" minSpareThreads="100" acceptCount="1000" connectionTimeout="50000" maxThreads="3000" disableUploadTimeout="true" URIEncoding="UTF-8" redirectPort="8446" /> <Connector port="8011" protocol="AJP/1.3" redirectPort="8446" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="/opt/www/webapps" unpackWARs="true" autoDeploy="true"> </Host> </Engine> </Service> </Server>
然后可以设置tomcat为Linux的服务,以便可以自动启动,创建权限为755的配置文件:/etc/init.d/tomcat,内容如下:
#!/bin/bash # # tomcatd This shell script takes care of starting and stopping # standalone tomcat # # chkconfig: 345 91 10 # description: tomcat service # processname: tomcat # config file: # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 prog=tomcat export JAVA_HOME=/opt/jdk export CATALINA_HOME=/opt/tomcat export CATALINA_OPTS='-Xms2048m -Xmx2048m' #初始化JVM内存可以自己根据情况设置
PATH=$PATH:$JAVA_HOME/bin STARTUP=$CATALINA_HOME/bin/startup.sh SHUTDOWN=$CATALINA_HOME/bin/shutdown.sh if [ ! -f $CATALINA_HOME/bin/startup.sh ] then echo "CATALINA_HOME for tomcat not available" exit fi start() { # Start daemons. echo -n $"Startting tomcat service: " daemon $STARTUP RETVAL=$? return $RETVAL } stop() { # Stop daemons. echo -n $"Stoping tomcat service: " $SHUTDOWN RETVAL=$? return $RETVAL } # See how we were called. case "$1" in start) start ;; stop) stop ;; restart|reload) stop start RETVAL=$? ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $0 {start|stop|restart|status}" exit 1 esac 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目录下即可。