一丶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

...

...

...

...

每一个组件都由一个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

        

        

          #设置账号与密码

二丶TOMCAT的实现

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

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

        #主机名和根目录

        unpackWARs="true" autoDeploy="true">

        

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

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

               #黑名单    

        

    mkdir -p /data/webapps/ROOT

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

        <%@ page language="java" %>        
        
        TomcatA
        
        

TomcatA.magedu.com

                          Session ID         <% session.setAttribute("magedu.com","magedu.com"); %>         <%= session.getId() %>                           Created on         <%= session.getCreationTime() %>                                    

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

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

2.部署应用网站

vim /etc/tomcat/server.xml

   #编辑访问根目录

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

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

<%@ page language="java" %>
<%@ page import="java.util.*" %>


Test Page


<% out.println("hello world");
%>

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  

      #配置负载均衡服务端

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

BalancerMember ajp://172.18.100.68:8009

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

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off    #启用正向代理请求

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

Require all granted

ProxyPass / balancer://tcsrvs/

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

Require all granted

重新读取配制后访问

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

5.http调度器负载均衡tomcat

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

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

BalancerMember http://172.18.100.68:8080

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

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

Require all granted

ProxyPass / balancer://tcsrvs/

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

Require all granted

重新读取配置并访问

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

会话粘性的实现方法:

后端两个tomcat添加:

1.        #设置标记jvm路由

2.

前端http添加:

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

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

ServerName lb.magedu.com

ProxyVia On

ProxyRequests Off

ProxyPreserveHost On

Require all granted

ProxyPass / balancer://tcsrvs/

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

Require all granted


    #启用管理接口

SetHandler balancer-manager

ProxyPass !

Require all granted

重新读取配置后访问