一丶TOMCAT

    1.TOMCAT是什么

        Tomcat 是由 Apache 开发的一个 Servlet 容器,实现了对 Servlet 和 JSP 的支持,并提供了作为Web服务器的一些特有功能,如Tomcat管理和控制平台、安全域管理和Tomcat阀等。

由于 Tomcat 本身也内含了一个 HTTP 服务器,它也可以被视作一个单独的 Web 服务器。但是,不能将 Tomcat 和 Apache HTTP 服务器混淆,Apache HTTP 服务器是一个用 C 语言实现的 HTTP Web 服务器;这两个 HTTP web server 不是捆绑在一起的。Tomcat 包含了一个配置管理工具,也可以通过编辑XML格式的配置文件来进行配置。

    2.tomcat的配置文件构成:

server.xml:主配置文件;

web.xml:每个webapp只有“部署”后才能被访问,它的部署方式通常由web.xml进行定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认部署相关的配置;

context.xml:每个webapp都可以专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认配置;

tomcat-users.xml:用户认证的账号和密码文件;

catalina.policy:当使用-security选项启动tomcat时,用于为tomcat设置安全策略; 

catalina.properties:Java属性的定义文件,用于设定类加载器路径,以及一些与JVM调优相关参数;

logging.properties:日志系统相关的配置;

    3.Tomcat的核心组件:/etc/tomcat/server.xml

<Server>

<Service>

<connector/>

<connector/>

...

<Engine>

<Host>

<Context/>

<Context/>

...

</Host>

<Host>

...

</Host>

...

</Engine>

</Service>

</Server>

每一个组件都由一个Java“类”实现,这些组件大体可分为以下几个类型:

顶级组件:Server

服务类组件:Service

连接器组件:http, https, ajp(apache jserv protocol)

容器类:Engine, Host, Context

被嵌套类:valve, logger, realm, loader, manager, ...

集群类组件:listener, cluster, ...

4.设置基于网页的manager app管理器和host-manager管理器配置

    vim /etc/tomcat/tomcat-users.xml

        <role rolename="manager-gui"/>

        <role rolename="admin-gui"/>

        <user username="tomcat" password="admin" roles="admin-gui,manager-gui"/>  #设置账号与密码

二丶TOMCAT的实现

1.建立并测试虚拟主机站点

    vim /etc/tomcat/server.xml  #编辑tomcat配置文件

        <Host name="node1.magedu.com"  appBase="/data/webapps"                 #主机名和根目录

        unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"

        prefix="node1_access_log." suffix=".log"

        pattern="%h %l %u %t &quot;%r&quot; %s %b" />

        <Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="172\.16\.100\.67"/>       #黑名单    

        </Host>

    mkdir -p /data/webapps/ROOT

    vim /data/webapps/ROOT/index.jsp    #编辑测试站点的内容JAVA语言

        <%@ page language="java" %>        
        <html>
        <head><title>TomcatA</title></head>
        <body>
        <h1><font color="red">TomcatA.magedu.com</font></h1>
        <table align="centre" border="1">
        <tr>
        <td>Session ID</td>
        <% session.setAttribute("magedu.com","magedu.com"); %>
        <td><%= session.getId() %></td>
        </tr>
        <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
        </tr>
        </table>
        </body>
        </html>

测试 curl http://node1.magedu.com:8080 或者直接登录浏览器访问

·························································································································

2.部署应用网站

vim /etc/tomcat/server.xml

<Context path="/test" docBase="/webapps/test" reloadable="true" />   #编辑访问根目录

mkidr  -pv  /webapps/test0.1/{classes,lib,WEB-INF}

vim /webapps/test0.1/index.jsp   #编辑测试站点的内容JAVA语言

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<% out.println("hello world");
%>
</body>
</html>

systemcat restart tomcat  #重启tomcat后登录访问

``````````````````````````````````````````````````````````````````````````````````````````````````````````

3.nginx反代tomcat

vim  /etc/nginx/conf.d/tomcat.conf    #编辑nginx配置文件

server {

listen 80;     #监听端口

server_name node1.magedu.com;     #主机名称

index index.jsp index.html;      #支持的文件格式

location / {    #普通文件类型访问点

root "/data/webapps/ROOT";    #必须到jsp目录下

}

location ~* \.*(jsp|do)$ {     #jps等文件类型访问站点

proxy_pass http://node1.magedu.com:8080;

}

}

····························································································

4.http通过ajp调度器负载均衡tomcat

vim /etc/httpd/conf.d/ajp_tomcat.conf  

<proxy balancer://tcsrvs>      #配置负载均衡服务端

BalancerMember ajp://172.18.100.67:8009     #ajp端口8009

BalancerMember ajp://172.18.100.68:8009

ProxySet lbmethod=byrequests   #负载均衡的一种

</Proxy>

<VirtualHost *:80>

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off    #启用正向代理请求

ProxyPreserveHost On   #反向代理转发所有主机

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://tcsrvs/

ProxyPa***everse / balancer://tcsrvs/  

<Location />

Require all granted

</Location>

</VirtualHost>

重新读取配制后访问

··············································································································

5.http调度器负载均衡tomcat

vim /etc/httpd/conf.d/http_tomcat.conf  

<proxy balancer://tcsrvs>

BalancerMember http://172.18.100.67:8080    #http8080端口

BalancerMember http://172.18.100.68:8080

ProxySet lbmethod=byrequests   #负载均衡的一种

</Proxy>

<VirtualHost *:80>

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://tcsrvs/

ProxyPa***everse / balancer://tcsrvs/

<Location />

Require all granted

</Location>

</VirtualHost>

重新读取配置并访问

·············································································································

会话粘性的实现方法:

后端两个tomcat添加:

1. <Engine name="Catalina" defaultHost="node1.magedu.com" jvmRoute="TomcatA">       #设置标记jvm路由

2. <Engine name="Catalina" defaultHost="node1.magedu.com" jvmRoute="TomcatB">

前端http添加:

 Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED   #添加认证信息,如果匹配到便发往对应标记路由

<proxy balancer://tcsrvs>

BalancerMember http://172.18.100.67:8080 route=TomcatA loadfactor=1

BalancerMember http://172.18.100.68:8080 route=TomcatB loadfactor=2

ProxySet lbmethod=byrequests

ProxySet stickysession=ROUTEID

</Proxy>

<VirtualHost *:80>

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy *>

Require all granted

</Proxy>

ProxyPass / balancer://tcsrvs/

ProxyPa***everse / balancer://tcsrvs/

<Location />

Require all granted

</Location>

</VirtualHost>


<Location /balancer-manager>    #启用管理接口

SetHandler balancer-manager

ProxyPass !

Require all granted

</Location>

重新读取配置后访问