环境(所有软件统一放到/root/下)
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
拓扑图
nginx
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.conf
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服务,防火墙放端口
nginx -t #没有报错继续 nginx firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload
验证1,实现nginx静态web页面访问
http://192.168.154.142
tomcat(两台配置一样,注意只要是文档里写的tomcat配置,两台都需要做)
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(在最后面加上)
export JAVA_HOME=/usr/local/java export CATALINA_HOME=/usr/local/tomcat7 export PATH=$JAVA_HOME/bin:$CATALINA_HOME/bin:$PATH
测试java1.8和tomcat7环境。
起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
验证3、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"%>tomcat-1 Session serviced by tomcat
Session ID | <%=session.getId() %> | <% session.setAttribute("abc","abc");%>
Created on | <%= session.getCreationTime() %> |
首先通过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
验证4、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
MySQL Test App DB Connection jdbc/TestDB javax.sql.DataSource Container
vim /web/webapp1/test.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%>tomcat1 tomcat1 connect MySQL
<% 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("
foo:"+rs.getString(2)+"bar:"+rs.getString(3)); } rs.close(); stmt.close(); conn.close(); %> #我在tomcat2上写的标题也有做区分。可以再截图中看到。
验证5:,测试tomcat连接数据,本次案例完成。