quinta-feira, 22 de agosto de 2013

Tomcat Begins

- Tomcat
                http://www.ramkitech.com/
                http://tomcat.apache.org/tomcat-6.0-doc/realm-howto.html
                http://my.opera.com/shibutani/blog/show.dml/1779612
                http://www.scribd.com/doc/7571690/Java-Security-Como-Usar-JAAS-Com-Tomcat-6
                http://www.mhavila.com.br/topicos/java/tomcat.html#t10

                - With o Apache
                               http://www.ramkitech.com/2012/10/tomcat-clustering-series-simple-load.html
                               http://www.ntu.edu.sg/home/ehchua/programming/howto/ApachePlusTomcat_HowTo.html
                               http://www.ntu.edu.sg/home/ehchua/programming/howto/images/ApachePlusTomcat.png
                               http://www.ntu.edu.sg/home/ehchua/programming/howto/images/HTTP_ClientServerSystem.png
                               - Why integrate Tomcat with Apache?
                                               - Clustering and loadbalance: With Apache you can use multiple tomcat instances or divide Tomcat instances into various namespaces
                                               - Security:
                                               - Speed for static content
                               - Context Containers
                                               Context containers are web applications running inside the webapps.  A web site can have one or more contexts.
                                              
                               - To run
                                               - Apache needs to load a adpter module which uses  Apache JServ Protocol (AJP) to comunicate with Tomcat via another TCP port (8009)
                                               - When Apache receives an HTTP request:
                                                               - Apache checkes if the request belongs to Apache, Tomcat, PHP, etc
                                                               - If Tomcat, Adapter takes the request and forwards it to Tomcat.
                               - Steps
                                               - Install Apache HTTP Server
                                               - Install Tomcat
                                               - tomcat.apache.org (-> Download -> Tomcat Connectors -> JK 1.2 -> Binary Releases -> windows ->  tomcat-connectors-1.2.37-windows-i386-httpd-2.4.x.zip .
                                               - rename it to mod_jk and copy to %APACHE%/modules
                                               - create a file mod_jk.conf in %APACHE%/conf


1:                                  # Load mod_jk module  
2:                                  # Update this path to match your modules location  
3:                                  LoadModule jk_module modules/mod_jk.so  
4:                                  # Where to find workers.properties  
5:                                  # Update this path to match your conf directory location  
6:                                  JkWorkersFile C:/workspace/linguagens/java/servers/apache-tomcat-7.0.16/conf/workers.properties  
7:                                  # Where to put jk logs  
8:                                  # Update this path to match your logs directory location  
9:                                  JkLogFile C:/workspace/linguagens/java/servers/apache-tomcat-7.0.16/logs/mod_jk.log  
10:                                  # Set the jk log level [debug/error/info]  
11:                                  JkLogLevel info  
12:                                  # Select the log format  
13:                                  JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"  
14:                                  # JkOptions indicate to send SSL KEY SIZE,  
15:                                  JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  
16:                                  # JkRequestLogFormat set the request format  
17:                                  JkRequestLogFormat "%w %V %T"  
18:                                  # Send everything for context /ws to worker ajp13  
19:                                  JkMount /ws ajp13  
20:                                  JkMount /ws/* ajp13  
21:                                  # Send everything for context /examples to worker ajp13  
22:                                  JkMount /examples ajp13  
23:                                  JkMount /examples/* ajp13  


                                               - Include this file in httpd.conf
                                                               include C:/workspace/linguagens/java/servers/apache-tomcat-7.0.16/conf/mod_jk.conf
                                               - Create the C:/workspace/linguagens/java/servers/apache-tomcat-7.0.16/conf/workers.properties
                          

1:                     # Define 1 real worker named ajp13  
2:                                  worker.list=ajp13  
3:                                  # Set properties for worker named ajp13 to use ajp13 protocol,  
4:                                  # and run on port 8009  
5:                                  worker.ajp13.type=ajp13  
6:                                  worker.ajp13.host=localhost  
7:                                  worker.ajp13.port=8009  
8:                                  worker.ajp13.lbfactor=50  
9:                                  worker.ajp13.cachesize=10  
10:                                  worker.ajp13.cache_timeout=600  
11:                                  worker.ajp13.socket_keepalive=1  
12:                                  worker.ajp13.socket_timeout=300  


                                               - Configure Tomcat
                                                               conf/server.xml to start the AJP1.3 service on TCP port 8009
               
                /TOMCAT_HOME/conf/server.xml

                               ServletContext – O container da aplicação web.  Somente um por aplicação.



1:                          <Context path="/projeto" docBase="projeto" debug="0" reloadable="true" />  
2:                          ex: http://localhost:8080/onjava/login.jsp  
3:                          <global-results>  
4:                          <result name="naoLogado">/login.jsp</result>  
5:                          </global-results>  



                Caminhos
                               /projeto
                               /projeto/WEB-INF
                               web.xml – configurações do projeto web.
                               /projeto/WEB-INF/classes
                               /projeto/WEB-INF/lib
                               TagLib



1:                          <taglib>  
2:                                  <taglib-uri>/onjava</taglib-uri>  
3:                                  <taglib-location>/WEB-INF/lib/taglib.tld</taglib-location>  
4:                          </taglib>  
5:                          <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
6:                          <%@taglib tagdir="/WEB-INF/tags" prefix="romalopes" %>  
7:                          <romalopes:teste/>  



                DataSource JNDI
                               http://wiki.locaweb.com.br/pt-br/Criando_uma_datasource_JNDI

                Pool de Conexões
                               No diretório META-INF da aplicação, crie um arquivo “context.xml” com o seguinte conteúdo:
                          

1:     <?xml version=”1.0? encoding=”UTF-8??>  
2:                  <Context auth=”Container”>  
3:                          <Resource name=”jdbc/bd_teste” type=”javax.sql.DataSource”  
4:                          url=”jdbc:mckoi://localhost:59999/”       driverClassName=”com.mckoi.JDBCDriver”  
5:                          username=”admin”      password=”123456?     maxActive=”100?        maxIdle=”20?/>  
6:                  </Context>  



                               Registrando o pool no aplicativo…
                               No arquivo “web.xml” da aplicação, insira as linhas abaixo…


1:                          <resource-ref>  
2:                                  <res-ref-name>jdbc/bd_teste</res-ref-name>  
3:                                  <res-type>javax.sql.DataSource</res-type>  
4:                                  <res-auth>Container</res-auth>  
5:                          </resource-ref>  

                Chamada no Service Locator


1:                  import java.sql.Connection;  
2:                  import javax.naming.InitialContext;  
3:                  import javax.sql.DataSource;  
4:                  public class ServiceLocator {  
5:                          public static Connection getConexao() throws Exception {  
6:                                  Connection con = null;  
7:                                  // Obtém a raiz da hierarquia de nomes  
8:                                  InitialContext contexto = new InitialContext();  
9:                                  // Obtém a origem dos dados  
10:                                  DataSource ds = (DataSource)contexto.lookup(“java:comp/env/ jdbc/bd_teste”);  
11:                                  // Obtém uma conexão  
12:                                  con = ds.getConnection();  
13:                                  // Retorna a conexão  
14:                                  return con;  
15:                          }  
16:                  }  


                SSL
                               $CATALINA_HOME/conf/server.xml

1:                  <Connector protocol="org.apache.coyote.http11.Http11Protocol"  
2:                                   port="8443" .../>  
3:                  <Listener className="org.apache.catalina.core.AprLifecycleListener"  
4:                                   SSLEngine="on" SSLRandomSeed="builtin" />  
5:                  <Connector  
6:                                   port="8443" maxThreads="200"  
7:                                   scheme="https" secure="true" SSLEnabled="true"  
8:                                   keystoreFile="${user.home}/.keystore" keystorePass="changeit"  
9:                                   clientAuth="false" sslProtocol="TLS"/>  


                               Install a Certificate from a Certificate Authority like verisign.com,
                - Configuring connectors
                               Connectors are link to outside world to receive requests, pass them to the correct web app and send back the results.
                               Each connector element represents a port that Tomcat will listen or to link tomcat to other web technologies(Eg: Apache Web Service).
                               E.g:


1:                          <Server>  
2:                                  <Service name="Catalina">  
3:                                         <Connector port="8443"/>  
4:                                         <Engine>  
5:                                          <Host name="yourhostname">  
6:                                                 <Context path="/webapp1"/>  
7:                                          </Host>  
8:                                         </Engine>  
9:                                  </Service>  
10:                          </Server>  


                               Two Types
                                               Http:
                                                               Attributes can be modified to specify exaclty how it works
                                                               Can be used as parte of load balancing scheme
                                               AJP:
                                                               Works like HTTP connectors, but using a AJP(Apache JServer Protocol) Protocol(binary and optimized version of HTTP) mostly used for Tomcat-Apache comunication.
                                                               Used in mod_jk
                - Tuning (performance)
                               - Disable modules you don't need
                               - Tuning the JVM
                                               - Chose the right JVM
                                               - Effective Memory Management
                                                               -Configure memory reallocation Xms.  Set to Xms = Xmx eliminates the need of JVM to resize the heap memory.
                                                               Ex: -Xmx1024m(Maxsize) –Xms1024m(HeapSize) -XX:MaxNewSize=384m(NewSize) -XX:MaxPermSize=128m(Size to load class)
                                                                              - MaxNewSize is 1/3 of heap size
                                                                              - Consuption memory = 1G Heap + 128mb perm=1.128Gb + 500Mb(JVM consume)
                                               - Selecting garbage collector (eliminate excessive garbage collector)
                                                               GC can stress the server. Start jvm with a higher maximum heap memory  -Xmx
                               - Configuring connectors - http://tomcat.apache.org/tomcat-7.0-doc/config/http.html
                                               - Setting the best thread pool.
                          

1:                     <Connector port="8080"  
2:                                         address="localhost" --> which address for a specific port.  
3:                                         maxThreads="250" --> 50 to 500 is good. 100 to 200 is usually best  
4:                                         maxHttpHeaderSize="8192" --> Max Size of a Header of a Request of Response HTTP  
5:                                         emptySessionPath="true"  
6:                                         protocol="HTTP/1.1" -->User BIO. APR is more faster about 10% than BIO. But BIO is more stable.  
7:                                         enableLookups="false" -->Enable system return DNS name not a IP address. Degrade performance because Tomcat looks up DNS.  
8:                                         redirectPort="8443"  
9:                                         acceptCount="100" --> It specifies server request queue length. If overflow, client get a error. Prevent system overload  
10:                                         compression="off --> Used to REST API requests      
11:                                         connectionTimeout="20000" --> 20 seconds  
12:                                         maxConnections=”8192" --> this amount of sockets at same time.  
13:                                         maxKeepAliveRequests=”1" --> Used to REST API requests. Time that this Connector will wait for another HTTP request before closing the connection  
14:                                         disableUploadTimeout="true" />  




                                               Value too small: Tomcat's threads will not handle all requests and will not use all server hardware.
                                               Value too high: Increase tomcat startup time
                               - Compression
                                               Initially set to off. Use compressableMimeTypes.  Some application perform better
                               - Use HTTP instead HTTPS
                               - Web Servers For Static Content
                                               Put static content dedicated to web servers such as Apache.
                               -
                                                              
                                              
                              
                - Differences between 6 and 7

                               http://www.tekhnologia.com/2012/07/why-tomcat-7-upgarde-and-tomcat-7-vs.html

Nenhum comentário:

Postar um comentário