Tomcat+redis+nginx配置

爲客戶開發的一個績效系統,採用了java web的開發方式,使用了一些spring mvc, mybatis之類的框架。相比於oracle ebs的二次開發,這種開發更加靈活,雖然和ebs集成的時候遇到一些問題,可是最後也都解決了。 html

在部署的時候,客戶要求要能同事承受一兩千人在線,相對於客戶公司的總人數(七八萬人),應該足夠了。ebs的二次都是直接部署在oracle ebs的application server上面,以前也沒怎麼關注過程序的部署。此次採用tomcat部署,考慮到單個tomcat的最大也就能承受500左右的在線人數,此次採用了一個小的集羣部署,使用了5個tomcat,反向代理使用的nginx。 java

如今程序基本穩定,壓力測試也都能沒什麼大的問題,趁着有時間,把部署和配置都整理一下。 nginx

準備

apache tomcat 7.0.55 web

nginx 1.7.2 redis

redis 2.8.9 spring

配置環境使用三個tomcat, 三臺tomcat、redis和nginx都在一臺機器上,爲了方便測試和部署。 數據庫

大體的整個配置的架構: apache

tomcat-nginx-redis

在這個圖中,nginx作爲反向代理,將客戶請求根據權重隨機分配給三臺tomcat服務器,redis作爲三臺tomcat的共享session數據服務器。 tomcat

規劃

redis 服務器

localhost:6379

nginx

localhost:80

tomcat

localhost:8081
localhost:8082
localhost:8083

配置

tomcat

修改tomcat文件夾中conf/context.xml文件,在context節點下添加以下配置:

<Valve  className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
     host="localhost" 
     port="6379"
     database="0" 
     maxInactiveInterval="60" />

conf/server.xml文件中的端口根據規劃依次修改。

另外要在tomcat的lib文件夾下分別添加三個jar文件,這個地方jar文件的版本有可能會有衝突,配置的時候須要多嘗試。我這裏的版本以下,是驗證過可使用的,經過maven的庫均可如下載到。

tomcat-redis-session-manager-1.2-tomcat-7.jar

jedis-2.2.0.jar

commons-pool-1.6.jar

nginx

修改nginx文件目中的conf/nginx.conf文件爲:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;

pid        logs/nginx.pid;

events {
	worker_connections  1024;
}


http {
	include       mime.types;
	default_type  application/octet-stream;

	log_format  main  '$remote_addr - $remote_user [$time_local] 	"$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

	access_log  logs/access.log  main;

	sendfile        on;
	#tcp_nopush     on;

	#keepalive_timeout  0;
	keepalive_timeout  65;

	#gzip  on;

	upstream  localhost   {  
          server   localhost:8081 weight=1;  
          server   localhost:8082 weight=2;  
		  server   localhost:8083 weight=3; 
	}  

	server {
    	listen       80;
    	server_name  localhost;

    	#charset koi8-r;

    	#access_log  logs/host.access.log  main;

    	location / {
        	root   html;
        	index  index.html index.htm;
			proxy_pass        http://localhost;  
       	 	proxy_set_header  X-Real-IP  $remote_addr;  
        	client_max_body_size  100m;  
    	}

    	#error_page  404              /404.html;

    	# redirect server error pages to the static page /50x.html
    	#
    	error_page   500 502 503 504  /50x.html;
    	location = /50x.html {
        	root   html;
    	}

	}
}

redis的配置就直接使用默認配置,由於只是測試用,和tomcat同樣沒有作參數優化配置。

運行

分別啓動redis、nginx和三臺tomcat。

redis

nginx

tomcat

測試

在三個tomcat的webapps/ROOT目錄下,分別添加session.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
	<br>session id=<%=session.getId()%>
	<br>tomcat 3
</body>
</html>

注:每一個tomcat下的標示不一樣

tomcat

tomcat

tomcat

從截圖中,能夠看出,分別訪問了不一樣的tomcat,可是獲得的session倒是相同的,說明達到了集羣的目的。

在這個架構中,有個明顯的瓶頸,就是數據庫。由於使用了企業級的oracle數據庫,因此在壓力測試種也沒有出現大的問題。可是做爲後續的能夠優化的地方,數據庫是必定要作讀寫分離的。

相關文章
相關標籤/搜索