Linuxの下でTomcat 7を使用して高性能Javaサーバを構築


1.アーキテクチャ説明:nginx+4個tomcat
nginxはフロントエンドエージェントとして、負荷のバランスのとれた役割を果たし、複数のtomcatは1台のサーバの高同時性能の問題を解決することができ、バックエンドにいくつかのtomcatを置くには、あなたのサーバがどれだけのメモリを持っているかを見て、私のサーバは4コアのCPU、8 Gメモリです.
 
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リクエストを80808180828083のいくつかのポートのバックエンドサービスに均一に配布し、負荷分散の効果を実現することがわかります.
 
3.Tomcat構成
 
server.xml構成では、1つのがインスタンスを起動するため、ここで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 tomcaton
またtomcat/bin/startupを変更する必要がある.sh,在#!/bin/sh以下追加:CATALINA_OPTS='-Xms2048m -Xmx2048m'
 
 
4.Tomcatグローバルfilterは文字化けしの問題を解決する:
コードは書かないで、過程を言って、filterコードのネット上は至る所で、私の言うことはtomcat/conf/webです.xmlでfilter構成を増やし、単独アプリケーションで追加するのと同じように、filterのclassパッケージjarファイルをtomcat/libディレクトリの下に置けばいいです.