#####Tomat前端和nginx负载均衡、sessin交叉储存#####

##什么是Tomcat?
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。
可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器
1、安装jdk和tomcat

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具

从官网下载安装包:http://tomcat.apache.org

解压要所包到指定的位置:/usr/local:

[root@server1 lnmp]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 lnmp]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/

做好软连接便于访问

[root@server1 lnmp]# cd /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79/ java
[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat
[root@server1 local]# ls
apache-tomcat-7.0.37  games    jdk1.7.0_79  libexec    sbin   tomcat
bin                   include  lib          lnmp       share
etc                   java     lib64        openresty  src

3)配置环境变量
vim /etc/profile
export JAVA_HOME=/usr/local/java
export CLASSPATH=.: J A V A H O M E / l i b : JAVA_HOME/lib: JAVAHOME/lib:JAVA_HOME/jre/lib
export PATH= P A T H : PATH: PATH:JAVA_HOME/bin

source /etc/profile #加载文件

[root@server1 tomcat]# cat /etc/profile
unset i
unset -f pathmunge
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin

另一种改变配置文件的方式
在这里插入图片描述
测试配置是否正确

[root@server1 ~]# vim test.java
public class test
{
    public static void main(String[] args)
    {
        System.out.println("Hello World1");
    }
}
[root@server1 ~]#  javac test.java 
[root@server1 ~]#  java test
Hello World1 运行正常java环境配置正常

5、开启tomcat程序,测试tomcat是否正常

开启tomcat服务

[root@server1 ~]# cd /usr/local/tomcat
[root@server1 tomcat]]# bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar


物理主机访问:172.25.46.1:8080
显示tomcat测试页面,服务配置正常成功开启。
在这里插入图片描述
在开启一台主机,按照上述步骤,安装jdk和tomcat。
目前:
nginx:172.25.46.1
tomcat1:172.25.46.1
tomcat2:172.25.46.2
server2:

[root@server2 lnmp]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/  
[root@server2 lnmp]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server2 lnmp]#  cd /usr/local/
[root@server2 local]#  ln -s jdk1.7.0_79/ java   ##作链接
[root@server2 local]#  ln -s apache-tomcat-7.0.37/ tomcat
[root@server2 local]# ls
apache-tomcat-7.0.37  games    jdk1.7.0_79  libexec  share
bin                   include  lib          nginx    src
etc                   java     lib64        sbin     tomcat
[root@server2 local]# vim /etc/profile
[root@server2 local]# source /etc/profile  ##加载文件
[root@server2 local]# cd
[root@server2 ~]# ls
lnmp  nginx  test.java
[root@server2 ~]# javac test.java 
[root@server2 ~]# java test
Hello World1
[root@server2 ~]# cd /usr/local/tomcat/
[root@server2 tomcat]# bin/startup.sh  ##打开tomcat服务
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 tomcat]# netstat -antlp   ##查看端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      639/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      891/master          
tcp        0      0 172.25.46.2:22          172.25.46.250:42016     ESTABLISHED 2077/sshd: root@pts 
tcp6       0      0 :::8080                 :::*                    LISTEN      2164/java           
tcp6       0      0 :::22                   :::*                    LISTEN      639/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      891/master          
tcp6       0      0 :::8009                 :::*                    LISTEN      2164/java           

在这里插入图片描述
访问测试:
在这里插入图片描述
####使用tomcat作为web服务器,nginx作为负载均衡###
编辑server1、server2web前端页面:
为了明显的看出负载均衡,不同的后端服务器编写不同的发布页面

[root@server1 ROOT]# vim test.jsp 
[root@server1 ROOT]# cat  test.jsp 
server1-The time is:<%=new java.util.Date() %>
[root@server2 tomcat]# cd webapps/ROOT/
[root@server2 ROOT]# ls
asf-logo.png       bg-nav.png    RELEASE-NOTES.txt  tomcat.svg
asf-logo-wide.gif  bg-upper.png  tomcat.css         WEB-INF
bg-button.png      build.xml     tomcat.gif
bg-middle.png      favicon.ico   tomcat.png
bg-nav-item.png    index.jsp     tomcat-power.gif
[root@server2 ROOT]# vim test.jsp
[root@server2 ROOT]# cat test.jsp
server2-The time is:<%=new java.util.Date() %>
[root@server2 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT

tomcat前端访问测试:

http://172.25.46.1:8080/test.jsp
http://172.25.46.2:8080/test.jsp
在这里插入图片描述
在这里插入图片描述

2 编辑nginx配置文件,是其作为负载均衡器server1

[root@server1 ROOT]# vim /usr/local/lnmp/nginx/conf/nginx.conf
17 http {
 18     include       mime.types;
 19  default_type  application/octet-stream;
 20     upstream tomcat {
 21              server 172.25.46.1:8080; # 设定轮询后端服务器
 22              server 172.25.46.2:8080;
 23 }

 46         location / {
 47             root   /usr/local/tomcat/webapps/ROOT;# 设定访问的路径
 48             index  index.html index.htm index.php;
 49         }
62         location ~ \.jsp$ {
 63             proxy_pass   http://tomcat;  # 添加轮询
 64         }
 65 
[root@server1 ROOT]# cd  /usr/local/lnmp/nginx
[root@server1 nginx]# nginx -t  # 检测语法是否正确
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 nginx]# nginx  -s reload   重新加载

重新启动两台后端服务器的tomcat主机:

[root@server2 ROOT]# cd /usr/local/tomcat/
[root@server2 tomcat]# bin/shutdown.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@server2 tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

物理机测试:http://172.25.46.1:8080/test.jsp 会自动实现负载均衡
在这里插入图片描述
在这里插入图片描述

###linux运维—在tomcat中实现session交叉存储####

一、cookie:

在网站中,http请求是无状态的。也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户。cookie的出现就是为了解决这个问题,第一次登录后服务器返回一些数据(cookie)给浏览器,然后浏览器保存在本地,当该用户发送第二次请求的时候,就会自动的把上次请求存储的cookie数据自动的携带给服务器,服务器通过浏览器携带的数据就能判断当前用户是哪个了。cookie存储的数据量有限,不同的浏览器有不同的存储大小,但一般不超过4KB。因此使用cookie只能存储一些小量的数据。
二、session:

session和cookie的作用有点类似,都是为了存储用户相关的信息。不同的是,cookie是存储在本地浏览器,而session存储在服务器。存储在服务器的数据会更加的安全,不容易被窃取。但存储在服务器也有一定的弊端,就是会占用服务器的资源,但现在服务器已经发展至今,一些session信息还是绰绰有余的。
三、cookie和session结合使用:

web开发发展至今,cookie和session的使用已经出现了一些非常成熟的方案。在如今的市场或者企业里,一般有两种存储方式:

1、存储在服务端:通过cookie存储一个session_id,然后具体的数据则是保存在session中。如果用户已经登录,则服务器会在cookie中保存一个session_id,下次再次请求的时候,会把该session_id携带上来,服务器根据session_id在session库中获取用户的session数据。就能知道该用户到底是谁,以及之前保存的一些状态信息。这种专业术语叫做server side session。

2、将session数据加密,然后存储在cookie中。这种专业术语叫做client side session。flask采用的就是这种方式,但是也可以替换成其他形式
一、模拟无法记忆客户端上次访问的真实后端服务器

在上篇负载均衡基础上:tomcat实现负载均衡

1 修改两个后端服务器的测试文件:
vim /usr/local/tomcat/webapps/ROOT/test.jsp

ROOT/test.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

重新启动tomcat

/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
3 、使用物理主机进行访问:http://172.25.46.1/test.jsp

结论:连续访问负载均衡器,其根本会不会将用户定位到在之所访问的后端服务器,并且每一次访问的session ID 都不一致。
二、nginx设置添加sticky(粘滞)模块
.在nginx配置文件中增加tomcat模块,为了实现session共享,需要支持sticky(粘滞)模块,nginx-1.14不支持sticky,所以使用nginx-1.10版本,重新编译nginx。

[root@server1 ~]# cd /usr/local/lnmp/
[root@server1 lnmp]# ls
mysql  nginx  php
[root@server1 lnmp]# rm -fr nginx/
[root@server1 lnmp]# tar zxf nginx-sticky-module-ng.tar.gz -C /usr/local/
[root@server1 lnmp]# tar zxf nginx-1.10.1.tar.gz
[root@server1 lnmp]# cd nginx-1.10.1
[root@server1 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx   --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/usr/local/nginx-sticky-module-ng

2  user  nginx   nginx  
 20     upstream tomcat {
 21         sticky;
 22         server 172.25.46.1:8080;
 23         server 172.25.46.2:8080;

36     gzip  on;

 46    location / {
 47             root   /usr/local/tomcat/webapps/ROOT;
 48             index  index.html index.htm;
 49         }

62         location ~ \.jsp$ {
 63             proxy_pass   http://tomcat;
 64         }
[root@server1 conf]# nginx 
[root@server1 conf]# nginx 
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 

访问测试:http://172.25.46.1/test.jsp
再次刷新页面,访问的ID号不会变,实现了sticky

在这里插入图片描述
上面的设置存在一种情况:

目前物理主机一致访问的是这一台后端服务器:

Server Info: 172.25.46.1 : 8080

ID 525CC544350569139C6363DCBBCB94DF

将这台服务器的tamcat服务关闭,模拟服务其宕机再次使用物理机进行访问:其会转移到另外的后端服务器但再次开启之前关闭的服务,物理主机所连接的主机不会再调回。

Server Info: 172.25.46.12 : 8080

ID 3646896BDA1D306FF523797E9AC79D9F

弊端:
这样极为的不安全,且服务器发生故障时,会丢失数据,所以要设置session共享(交叉存储)的方式

###实现session共享###
进入/usr/local/tomcat/lib目录(server1,2都做)将以下jar包拷贝到该目录:

asm-3.2.jar
kryo-1.04.jar
kryo-serializers-0.10.jar
memcached-session-manager-1.6.3.jar
memcached-session-manager-tc7-1.6.3.jar(最重要)
minlog-1.2.jar
msm-kryo-serializer-1.6.3.jar
reflectasm-1.01.jar
spymemcached-2.7.3.jar
[root@server1 ~]# rm -fr memcached-session-manager-tc6-1.6.3.jar   # 删除掉重复的

在这里插入图片描述

2)编辑/usr/local/tomcat/conf/context.xml文件,加入session共享配置
context.xml 该文件的作用是配置各种数据库的连接池

XML概念
xml常用于数据存储和传输,文件后缀为 .xml
它是可扩展标记语言(Extensible Markup Language,简称XML),是一种标记语言
标记,指计算机所能理解的信息符号;
通过此种标记,计算机之间可以处理包含各种信息的文章等
1 标记,指计算机所能理解的信息符号;
2 通过此种标记,计算机之间可以处理包含各种信息的文章等。
XML设计用来传送及携带数据信息,不用来表现或展示数据,所以XML用途的焦点是它说明数据是什么,以及携带数据信息。而HTML语言则用来表现数据
172.25.46.1
在这里插入图片描述
172.25.46.2:
在这里插入图片描述

重启两台服务器的tomcat服务

[root@server2 lib]# /usr/local/tomcat/bin/shutdown.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
^[[A[root@server2 lib]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

在两台服务器安装memcached并开启

[root@server1 tomcat]# yum install memcached.x86_64 -y
[root@server1 tomcat]# systemctl start memcached

测试:
在这里插入图片描述关闭172.25.46.1的tomcat,跳到172.25.46.2
在这里插入图片描述

重启172.25.46.1的tomat,关闭172.25.46.2的tomcat
在这里插入图片描述
开启172.25.46.2的tomcat 关闭172.25.46.2memcached
ID就会便成n1
在这里插入图片描述
开启172.25.46.2的memcached,关闭172.25.46.1的memcached
在这里插入图片描述
以上可以看出,在设置的交叉存储之后,客户一旦成功连接,从开始到结束,再次连接的时候ID都没有改变。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值