前言:
系統:CentOS 6.4 x64
JDK:1.7
Tomcat:7.X
伺服器叢集分布:
nginx:192.168.2.132:80
tomcat: 192.168.20.132:8088
192.168.20.132:8099
redis: 192.168.20.132:6379
安裝包:
nginx下載下傳位址:http://nginx.org/en/download.html
redis下載下傳位址:https://redis.io/download
tomcat和Jdk自行下載下傳安裝
搭建叢集所需要的jar包下載下傳位址:https://download.csdn.net/download/dc282614966/10564781
一、Nginx安裝
nginx版本:nginx-1.15.2
安裝方式:源碼編譯安裝
1.安裝必須環境
nginx的編譯需要c++,同時prce(重定向支援)和openssl(https支援)也需要安裝。
[[email protected] ~]# yum install gcc-c++
[[email protected] ~]# yum -y install pcre*
[[email protected] ~]# yum -y install openssl*
2.将下載下傳好的nginx-1.15.2.tar.gz包解壓到/usr/local/下
[[email protected] ~]# tar -zxvf nginx-1.15.2.tar.gz -C /usr/local/
3.進入cd /usr/local/nginx-1.15.2/ 并設定安裝目錄
[roo[email protected] nginx-1.15.2]# ./configure --prefix=/usr/local/nginx
4.如果沒有報錯,開始編譯安裝
[[email protected] nginx-1.15.2]# make
[[email protected] nginx-1.15.2]# install
4.防火牆端口打開80
1)在iptables下添加如下的設定:
[[email protected] nginx-1.15.2]# vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW-m tcp -p tcp --dport 80 -j ACCEPT
2)重新開機iptalbes
service iptables restart
5.啟動nginx服務
進入到cd /usr/local/nginx/sbin目錄下
[[email protected] sbin]# ./nginx
檢視程序,可以看到nginx的master和worker程序
6.可以通過通路ip:80測試,看到頁面這樣的提示就說明安裝成功
重新開機的指令:
[[email protected] sbin]# ./nginx -s reload
二、redis安裝
1.下載下傳安裝包
wget http://download.redis.io/releases/redis-4.0.10.tar.gz或者https://redis.io/download
2.解壓大/usr/local/目錄下
[[email protected] redis-4.0.10]# tar -zxvf redis-4.0.10.tar.gz -C /usr/local/
3.進入到/usr/local/redis-4.0.10/
[[email protected] /]# cd /usr/local/redis-4.0.10/
4.編譯安裝
[[email protected] redis-4.0.10]# make
[ro[email protected] redis-4.0.10]# make PREFIX=/usr/local/redis install
5.啟動
進入到編譯後的檔案目錄下/usr/local/redis/bin/,啟動redis,如下圖:
[[email protected] /]# cd /usr/local/redis/bin/
啟動:
[[email protected] bin]# ./redis-server
6.設定redis背景啟動
将解壓目錄下的redis.conf檔案複制到編譯後的redis檔案下:
[[email protected] redis-4.0.10]# cp redis.conf /usr/local/redis
修改redis.conf檔案,将no改為yes,然後重親redis
重新啟動redis:
[[email protected] redis]# ./bin/redis-server ./redis.conf
檢視程序是否啟動:
[[email protected] bin]# ps -ef|grep redis
三、tomcat配置
1.下載下傳所需的jar,然後拷貝到tomcat的lib下面:
jar包下載下傳位址:https://download.csdn.net/download/dc282614966/10563909
2.改tomcat的context.xml配置檔案,将下面的配置資訊放入context标簽内
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
database="0"
maxInactiveInterval="60"/>
四、nginx配置
這裡由于要做反向代理,需要在nginx的conf目錄下配置nginx.conf的配置檔案。畫紅線部分為添加的内容,修改如下:
這裡指定了本機下的兩個Tomcat執行個體,端口分别為8088,8099,權重都為1,後邊配置Tomcat執行個體,nginx-tomcat-redis這個是我測試項目的項目名,下邊要用到:
upstream nginx-tomcat-redis{
#weigth參數表示權值,權值越高被配置設定到的幾率越大
#本機上的Squid開啟3128端口
server 127.0.0.1:8088 weight=1;
server 127.0.0.1:8099 weight=1;
}
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://nginx-tomcat-redis;
}
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://nginx-tomcat-redis;
}
}
五、測試
1.jsp頁面
在測試項目中寫了個簡單的jsp頁面,在js中調用自定義的一個servlet,然後通過servlet擷取sessionId,将擷取的sessionId傳回到jsp頁面顯示,代碼如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>First Page</title>
<script type="text/javascript" src="./js/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="./js/session.js"></script>
</head>
<body>
<h1>第一個jsp頁面</h1>
<div id="firstSessinId">SessionId:</div>
</body>
</html>
2.js檔案
js中主要是通過ajax調用servlet來擷取sessionId,代碼如下:
$(function() {
$.ajax({
url : 'session',
method : 'get',
success : function(result){
$("#firstSessinId").append(result);
}
});
})
3.servlet檔案
主要是用來擷取sessionId,然後傳回給jsp頁面,代碼如下:
public class SessionShareServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public SessionShareServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getSession().getId();
request.getSession().setAttribute("session", id);
System.out.println(id);
OutputStream out = response.getOutputStream();
out.write(id.getBytes());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.頁面測試效果
通路頁面如下,放反複重新整理頁面頁面時會出現“第一個jsp頁面”和“第二個jsp頁面”交替出現,則證明nginx配置成功,當兩個頁面的SessionId相同則說明共享session配置成功。
5.項目源碼下載下傳位址:https://github.com/dengchao3119/nginx-tomcat-redis.git