環境(全部軟件統一放到/root/下)javascript
nginx:192.168.154.142;nginx-1.14.0.tar.gz redis:192.168.154.143;redis-4.0.10.tar.gz tomcat1:192.168.154.137;jdk-8u171-linux-x64.tar.gz,apache-tomcat-7.0.86.tar.gz tomcat2:192.168.154.138;jdk-8u171-linux-x64.tar.gz,apache-tomcat-7.0.86.tar.gz mysql:192.168.154.139
拓撲圖css
nginxhtml
yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel groupadd -r www useradd -r -g www -s /sbin/nologin www tar zxf nginx-1.14.0.tar.gz cd nginx-1.14.0/ ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_flv_module make && make install ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vi /usr/local/nginx/conf/nginx.confjava
user www www; worker_processes 4; worker_cpu_affinity 0001 0010 0100 1000; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; worker_rlimit_nofile 10240; pid logs/nginx.pid; events { use epoll; worker_connections 4096; } 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; server_tokens off; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #Compression Settings gzip on; gzip_comp_level 6; gzip_http_version 1.1; gzip_proxied any; gzip_min_length 1k; gzip_buffers 16 8k; gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml; gzip_vary on; #end gzip # http_proxy Settings client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 75; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; #load balance Settings upstream backend_tomcat { server 192.168.154.137:8080 weight=1 max_fails=2 fail_timeout=10s; server 192.168.154.138:8080 weight=1 max_fails=2 fail_timeout=10s; } #virtual host Settings server { listen 80; server_name www.benet.com; charset utf-8; location / { root html; index index.jsp index.html index.htm; } location ~* \.(jsp|do)$ { proxy_pass http://backend_tomcat; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } location /nginx_status { stub_status on; access_log off; allow 192.168.154.0/24; deny all; } } }
起nginx服務,防火牆放端口mysql
nginx -t #沒有報錯繼續 nginx firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload
驗證1,實現nginx靜態web頁面訪問linux
tomcat(兩臺配置同樣,注意只要是文檔裏寫的tomcat配置,兩臺都須要作)c++
tar zxf jdk-8u171-linux-x64.tar.gz tar zxf apache-tomcat-7.0.86.tar.gz mv jdk1.8.0_171/ /usr/local/java mv apache-tomcat-7.0.86 /usr/local/tomcat7
vi /etc/profile(在最後面加上)
web
export JAVA_HOME=/usr/local/java export CATALINA_HOME=/usr/local/tomcat7 export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH
測試java1.8和tomcat7環境。redis
起tomcat服務,放行8080端口
catalina.sh start firewall-cmd --add-port=8080/tcp --permanent firewall-cmd --reload
驗證2,實現tomcat7jsp頁面訪問
192.168.154.137
192.168.154.138
驗證三、nginx的負載均衡
vim /usr/local/tomcat7/conf/server.xml(添加一行)
mkdir -p /web/webapp1 cat > /web/webapp1/index.jsp << EOF <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>tomcat-1</title> </head> <body> <h1><font color="red">Session serviced by tomcat</font></h1> <table aligh="center" border="1"> <tr> <td>Session ID</td> <td><%=session.getId() %></td> <% session.setAttribute("abc","abc");%> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> <html> EOF #爲作區分我將tomcat2的網頁title改爲tomcat-2了,注意看下面測試的截圖 catalina.sh stop catalina.sh start
首先經過tomcat地址登陸網頁,以下
經過nginx地址登陸,以下
刷新
能夠得出,nginx的輪詢調度阮算法的負載均衡沒問題。然而這時候的session id是不同的。生產環境中,每一個用戶訪問網站,都指望保持使用同一個會話。舉個例子,某寶買東西,我不可能刷一次給我換一個會話,那樣個人訪問的數據都沒了,好比我想瀏覽曾經看過的商品,結果刷新一下給我換了一個web服務器提供服務,我以前瀏覽的商品就找不到了,這顯然是有問題的。
這其實就是本文檔實現的最終目的,session共享,其實session的解決方案有多種,感興趣能夠百度一下。
redis上
tar zxf redis-4.0.10.tar.gz cd redis-4.0.10/ make && make install cp redis.conf /etc/redis.conf
vim /etc/redis.conf(添加3行,分別爲redis地址,密碼,後臺運行)
bind 127.0.0.1 192.168.154.143 requirepass pwd@123 daemonize yes
redis-server /etc/redis.conf ss -lnpt |grep redis firewall-cmd --add-port=6379/tcp --permanent firewall-cmd --reload
tomcat上(用redis作session共享須要在tomcat7的lib目錄下放如下3個包)
mv commons-pool2-2.2.jar /usr/local/tomcat7/lib mv commons-pool2-2.2.jar /usr/local/tomcat7/lib mv tomcat-redis-session-manage-tomcat7.jar /usr/local/tomcat7/lib
vim /usr/local/tomcat7/conf/context.xml(添加在倒數第2行,參數有host也就是redis的地址,password密碼,port也就是redis端口)
重啓tomcat7
catalina.sh stop catalina.sh start
驗證四、redis實現的session共享
mysql(mysql的安裝本次文檔不提了,感興趣能夠查看個人其餘文檔,或者yum安裝都是同樣的)
mysql> create database javatest; mysql> use javatest; mysql> create table testdata(id int not null auto_increment primary key,foo varchar(25),bar int); mysql> insert into testdata(foo,bar) values ('hello','123456'),('ok','654321'); mysql> grant all on *.* to javauser@'192.168.154.%' identified by 'javapasswd';
firewall-cmd –-permanent –add-port=3306/tcp firewall-cmd --reload
tomcat7上
mv mysql-connector-java-5.1.22-bin.jar /usr/local/tomcat7/lib
vim /usr/local/tomcat7/conf/context.xml(在倒數第二行插入)
catalina.sh stop catalina.sh start mkdir /web/webapp1/WEB-INF
vim /web/webapp1/WEB-INF/web.xml
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4"> <description>MySQL Test App</description> <resource-ref> <description>DB Connection</description> <res-ref-name>jdbc/TestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> </web-app>
vim /web/webapp1/test.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%> <html> <head> <title>tomcat1</title> </head> <body> tomcat1 connect MySQL<br> <% String driverClass="com.mysql.jdbc.Driver"; String url="jdbc:mysql://192.168.154.139:3306/javatest"; String username = "javauser"; String password = "javapasswd"; Class.forName(driverClass); Connection conn=DriverManager.getConnection(url, username, password); Statement stmt=conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from testdata"); ##注意 while(rs.next()){ out.println("<br>foo:"+rs.getString(2)+"bar:"+rs.getString(3)); } rs.close(); stmt.close(); conn.close(); %> </body></html> #我在tomcat2上寫的標題也有作區分。能夠再截圖中看到。
驗證5:,測試tomcat鏈接數據,本次案例完成。