session会话、tomcat会话保持、mysql存储引擎等

2019-03-15  本文已影响0人  佐岸的咖啡

1、简述session会话保持的几种方式并举例实现

1).session sticky

 nginx: ip_hash
 haproxy: source
 lvs: sh
vim /etc/nginx/nginx.conf
http  {
..........
upstream tcsrvs {
#一致性哈希算法绑定后端主机,保持会话
       hash $request_uri consistent; 
#使用cookie绑定后端主机,保持会话
       hash $cookie_name consistent; 
      server 192.168.10.100:8080;
      server 192.168.10.101:8080;
}

vim /etc/nginx/conf.d/ilinux.conf
    server {
                listen 80;
                server_name www.ilinux.io;
      location / {
                 proxy_pass http://tcsrvs;
}
}
..........
}

2).session cluster

<proxy balancer://tcsrvs>
BalancerMember http://192.168.10.100:8080 route=TomcatA loadfactor=1  #会话组成员
BalancerMember http://192.168.10.101:8080 route=TomcatB loadfactor=2
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>

<VirtualHost *:80>
ServerName lb.ilinux.io
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/  #反向代理,调用tcsrvs会话组
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
<proxy balancer://tcsrvs>
BalancerMember ajp://192.168.1.15:8009
BalancerMember ajp://192.168.1.16:8009
ProxySet lbmethod=byrequests
</Proxy>

<VirtualHost *:80>
ServerName lb.ilinux.io
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPassReverse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
</VirtualHost>
<Location /balancer-manager> 
  SetHandler balancer-manager 
  ProxyPass ! 
  Require all granted   
</Location>

3).session server

~ #]   vim /etc/tomcat/server.xml
......
<Host name= localhost" appBase="webapps"
    unpackWARS="true" autoDeploy="true"
#添加以下语句
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">

<Manager className="org.apache.catalina.ha.session.DeltaManager"  #使用多播同步管理器

expireSessionsOnShutdown="false" #不允许关机丢失会话
notifyListenersOnReplication="true"/> #通知其他服务器

<Channel className="org.apache.catalina.tribes.group.GroupChannel">#定义信道
<Membership className="org.apache.catalina.tribes.membership.McastService"#组件一
address="228.0.0.4" #多播地址
port="45564" #端口
frequency="500" #每隔0.5秒发送状态探测
dropTime="3000"/> #离线时长
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"#接收器
address="auto" #自动探测地址
port="4000" #接受端口
autoBind="100" #每隔多长时间绑定
selectorTimeout="5000" #选择器的超时时间
maxThreads="6"/> #最大并发连接数

<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">#发送器
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>#轮询的并发送器
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>#过滤器
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> #过滤器

<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"#部署器自动部署
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>

<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

编辑WEB-INF/web.xml, 添加<distributable/>元素

~ #] cp /etc/tomcat/web.xml  /var/lib/tomcat/webapps/test/WEB-INF/
~ #] vim /var/lib/tomcat/webapps/test/WEB-INF/web.xml
..................
<distributable/> #t添加元素
.................

注意: CentOS 7上的tomcat自带的文档中的配置示例有语法错误,绑定的地址为auto时,会自动解析本地主机名,并解析得出的IP地址做为使用的地址

[root@tomcat-77 ~]# vim /etc/hosts  #查看主机名和ip 是否正确

2、实现tomcat会话保持与会话保持器msm配置示例

image.png

1).架构配置

名称 服务器ip 类型 (CentOS 7)
Server 192.168.8.104 httpd
tomcatA 192.168.8.105
tomcatB 192.168.8.103
memcachedA 192.168.8.106
memcachedB 192.168.8.107

2).安装JDK和TOMCAT

[root@tomcat-A ~]# yum install ntpdate -y
[root@tomcat-A ~]# ntpdate ntp1.aliyun.com #同步时间
[root@tomcat-A ~]# yum -y install java-1.8.0-openjdk
[root@tomcat-A ~]# java -version   #版本查询
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)  #混合模式
注意:安装完成后,要配置JAVA_HOME环境变量,指向java的安装路径;
设定java环境变量
[root@tomcat-A ~]# echo  $JAVA_HOME   #查询是否有路径
[root@tomcat-A ~]# vim /etc/profile.d/java.sh  #编写脚本
export JAVA_HOME=/usr
[root@tomcat-A ~]# . /etc/profile.d/java.sh
[root@tomcat-A ~]# echo $JAVA_HOME
/usr

#安装tomcat
[root@tomcat-15 ~]# yum install -y tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp

2).下载对应的jar包,存储到对应的位置

url: https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

memcached-session-manager-${version}.jar
memcached-session-manager-tc${6,7,8}-${version}.jar
spymemcached-${version}.jar
msm-javolution-serializer-${version}.jar
javolution-${version}.jar
rpm -ql tomcat-lib                  #   
/usr/share/java/tomcat/             # yum安装默认在此目录
/usr/local/tomcat/lib               # 在官网rpm安装的默认在此目录了

3).配置tomcat的主文件

host字段

[root@tomcat-15 ~]# vim /etc/tomcat/server.xml 
......
<Host name= "localhost" appBase="webapps"
    unpackWARS="true" autoDeploy="true"
#添加以下语句

           <Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">
              <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                memcachedNodes="n1:192.168.8.105:11211,n2:192.168.8.103:11211"  #缓存服务器地址端口
                failoverNodes="n2"       
                requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
              />
             </Context>

4).提供测试的页面

[root@tomcat-A ~]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-A ~]# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="green">TomcatA.ilinux.io</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("ilinux.io","ilinux.io"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
[root@tomcat-A ~]#systemctl start tomcat.servicen #启动tomcat

tomcatB服务器:

[root@tomcat-B ~]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-B ~]# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="red">TomcatB.ilinux.io</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("ilinux.io","ilinux.io"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
[root@tomcat-B ~]#systemctl start tomcat.servicen #启动tomcat

5).下载并启动memcached

[root@memcached-A ~]# yum install ntpdate -y
[root@memcached-A ~]# ntpdate time1.aliyun.com #同步时间
[root@memcached-A ~]# yum install -y memcached
[root@memcached-A ~]# systemctl start memcached.service
[root@memcached-A ~]# ss -tnl
State      Recv-Q Send-Q                                   Local Address:Port                                                  Peer Address:Port              
LISTEN     0      128                                                  *:11211                                                            *:*                  
LISTEN     0      128                                                 :::11211                                                           :::*                  
.....

6).配置httpd反代设置

[root@httpd ~]#  yum install -y httpd

[root@httpd ~]# vim /etc/httpd/conf.d/ilinux.conf

#proxy_http_module反向代理配置:
<VirtualHost *:80>
ServerName      www.ilinux.io
ProxyRequests Off
ProxyVia        On
ProxyPreserveHost On
<Proxy balancer://tomcat>
    BalancerMember  http://192.168.8.105:8080 loadfactor=1
    BalancerMember  http://192.168.8.103:8080 loadfactor=1
    ProxySet  lbmethod=byrequests
</Proxy>

ProxyVia Off
ProxyRequests Off
ProxyPass / balancer://tomcat/
ProxyPassReverse / balancer://tomcat/
<Proxy *>
    Order Allow,Deny
    Allow From all
</Proxy>

<Location />
    Order Allow,Deny
    Allow From all
</Location>
</VirtualHost>
>
[root@httpd ~]# httpd -t
[root@httpd ~]#  systemctl start httpd.service

7).测试页面

image.png image.png

3、简述mysql存储引擎及读写锁实现并发访问控制

存储引擎

MySQL --> MariaDB --> Percona-server
InnoDB --> XtraDB

MyISAM: (非事务型)
并发控制:
上一篇下一篇

猜你喜欢

热点阅读