













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of Apache Tomcat, its features that make Java stand out, and the use of Maven as a project management tool. It covers setting up a Java project with Maven, the Maven phases, and instructions for installing and configuring Apache Tomcat and Maven. It also explains the difference between Apache Tomcat and Apache Server.
Typology: Lab Reports
1 / 21
This page cannot be seen from the preview
Don't miss anything!














1 số đặc điểm khiến Java nổi trội hơn so với những ngôn ngữ khác
Độc lập phần cứng và hệ điều hành : Java được thiết kế để biên dịch code thành bytecode và bytecode sẽ chạy trên môi trường thực thi (jvm). Nên chương trình Java có thể chạy trên nhiều thiết bị, nhiều hệ điều hành khác nhau.
Mạnh mẽ: quá trình cấp phát, giải phóng bộ nhớ thực hiện tự động. Không sử dụng con trỏ hoặc phép toán con trỏ. Bảo mật: Do Java phải biên dịch qua máy ảo Java (JVM) nên sẽ được bảo mật cao hơn khi mọi đối tượng phải qua JVM mới đến hệ điều hành. Phân tán: Java hỗ trợ rất mạnh cho lập trình hệ thống phân tán như client-server, RMI… bằng Java Web, UDP, TCP
Apache maven là một công cụ quản lý dự án cho phép các developers có thể quản lý về version, các dependencies( các thư viện sử dụng trong dự án ), quản lý build (jar, war), tự động download javadoc & source, …
Taọ 1 project Java với maven:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactI d=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
Cấu trúc tiêu chuẩn của 1 maven project:
my-app |-- pom.xml -- src |-- main |-- java | -- com |-- mycompany | -- app |-- App.java -- test-- java -- com-- mycompany -- app-- AppTest.java
src/main/java : chứa source code của project src/test/java :chứ test source pom.xml :Project Object Model (POM).
Là nơi khai báo tất cả những gì liên quan đến dự án được cấu hình qua maven, như khai báo các dependency, version của dự án, tên dự án, repossitory …
4.0.0 com.mycompany.app my-app 1.0-SNAPSHOT
1.7 1.7
junit
test : test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed package : take the compiled code and package it in its distributable format, such as a JAR. integration-test : process and deploy the package if necessary into an environment where integration tests can be run verify : run any checks to verify the package is valid and meets quality criteria install : install the package into the local repository, for use as a dependency in other projects locally deploy : done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
Ngoài ra:
clean : cleans up artifacts created by prior builds site : generates site documentation for this project
1 điều thú vị là có thể thực hiện nhiều phase theo trình tự:
mvn clean dependency:copy-dependencies package
Thiết lập môi trường
Ubuntu/Debian:
sudo apt install openjdk-8-jdk sudo apt install openjdk-11-jdk java -version
CentOS 7:
sudo yum install java-1.8.0-openjdk-devel yum install java-11-openjdk-devel -y java -version
Switch giữa các phiên bản java (nếu cài nhiều phiên bản)
sudo update-java-alternatives --list sudo update-alternatives --config java sudo update-alternatives --config javac java -version javac -version
Tạo $JAVA_HOME global:
To set JAVA_HOME in Linux for all users, we can use /etc/profile or /etc/environment (preferred).
Open /etc/environment in any text editor and add the following:
JAVA_HOME=/path/to/java_installation
Please note that /etc/environment is not a script but a list of assignment expressions (that is why export is not used). This file is read at the time of login.
To set JAVA_HOME using /etc/profile, here's what we'll add to the file:
export JAVA_HOME="/path/to/java_installation" export PATH=$JAVA_HOME/bin:$PATH
logout and login again, reboot, or use source /etc/profile
Download:
wget https://archive.apache.org/dist/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.ta r.gz
Extract:
tar xzvf apache-maven-3.6.3-bin.tar.gz --directory=/opt
Add thư mục chứa file bin vào PATH:
export PATH=/opt/apache-maven-3.6.3/bin:$PATH
Confirm:
/webapps/PROJECT_DIR/META-INF: context.xml
CATALINA_HOME và CATALINA_BASE
CATALINA_HOME: Thư mục root của Tomcat. Ex: /home/tomcat/apache-tomcat-9.0. CATALINA_BASE : Thư mục root của 1 Tomcat instance (khi sử dụng nhiều phiên bản Tomcat)
Nếu đặt 2 variable trên khác nhau:
CATALINA_HOME : chứa static source như .jar, binary files, dùng cho mục đích nâng cấp Tomcat dễ dàng. CATALINA_BASE : chứa tệp cấu hình, logs, application, …
CATALINA_BASE:
/bin: setenv.sh , setenv.bat , tomcat-juli.jar. Order: CATALINA_BASE → CATALINA_HOME. /lib: CATALINA_BASE → CATALINA_HOME /log: /webapps: CATALINA_BASE only. /work: temporary working directories for the deployed web applications. /temp: used by the JVM for temporary files.
At minimum, CATALINA_BASE must contain:
conf/server.xml conf/web.xml
In CATALINA_BASE:
bin - Only the following files:
work - Temporary working directories for web applications temp - Directory used by the JVM for temporary files (java.io.tmpdir)
bin - Startup and shutdown scripts lib - Libraries and classes
Cài đặt Tomcat 9.0 (requires Java 8 or later)
Tạo non-root user phục vụ cho việc chạy tomcat:
sudo groupadd tomcat sudo mkdir /opt/tomcat sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Download & uncompress package:
cd /tmp wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.70/bin/apache-tomcat-9.0.70.tar.gz sudo tar xvf apache-tomcat-9.0.70.tar.gz -C /opt/tomcat --strip-components=
Update lại quyền:
sudo chown tomcat:tomcat -R /opt/tomcat
sudo chmod -R g+rw conf #sudo chmod g+r conf/*
Tạo tomcat service:
[Unit] Description=Tomcat 9 servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true" Environment="CATALINA_BASE=/opt/tomcat"
manager-gui - allows access to the HTML GUI and the status pages manager-script - allows access to the text interface and the status pages manager-jmx - allows access to the JMX proxy and the status pages manager-status - allows access to the status pages only
Mặc định manager page chỉ truy cập đc từ 127.0.0.1. Để loại bỏ hạn chế này, bạn sẽ cần phải chỉnh sửa tệp context.xml , nhận xét hoặc xóa các dòng (cũng có thể cấu hình IP cụ thể nào đó) sau:
Tomcat Deployer
Bố cục thư mục chuẩn
Trong thư mục "document root” :
**.html, .jsp, etc. /WEB-INF/web.xml - Đây là tệp mô tả servlets, components tạo nên web app cùng các tham số khởi tạo, bảo mật, … /WEB-INF/classes/ - Java class files được kết hợp thành JAR. /WEB-INF/lib/ - third party class libraries hoặc JDBC drivers.
Deploy sử dụng Tomcat Manager GUI
Tải sample war file:
wget https://tomcat.apache.org/tomcat-9.0-doc/appdev/sample/sample.war
Đi đến trang manager, tại “WAR files to deploy,” nhấp vào Browse đi đến location của file war đã tải
Cuối cùng, click deploy để deploy ứng dụng. Sau khi deploy, bạn sẽ thay ứng dụng trong danh sách ứng dụng kèm theo địa chỉ:
Multiple Tomcat Instances
Tương tự như cách cài với 1 server nhưng chúng ta sẽ cấu hình lại để tránh bị conflict.
Ex: /var/tomcat/serverA và /var/tomcat/serverB.
Như đã đề cập ở trên, thư mục này bắt buộc phải bao gồm: conf, logs, temp, webapps, và work.
Copy chúng từ thư mục cài đặt tomcat và tạo thêm 1 thư mục bin rỗng (nơi mà ta sẽ đặt file setenv.sh )
CATALINA_OPTS="$CATALINA_OPTS -Xms256m" CATALINA_OPTS="$CATALINA_OPTS -Xmx1024m" CATALINA_OPTS="$CATALINA_OPTS -XX:MaxPermSize=512m" JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd
Xóa toàn bộ thư mục đã copy trong trong Tomcat. Tạo thư mục /controller nơi sẽ chưa toàn bộ script control từng instance.
Tomcat listen tối thiểu 2 port (1 cho shutdown script, 1 cho các request). Tìm đến file server.xml trong conf/ và chắc chắn rằng mỗi instance có 1 giá trị khác nhau (> 1024 và không .xung đột với bất kỳ dịch vụ nào khác).
shutdown port
Các cổng khác được cấu hình trong : HTTP Connect, Redirect, AJP, … Tất nhiên nếu muốn sử dụng chung port thì chúng phải đc cấu hình với IP khác nhau.
#!/bin/bash app_instance=$1; BASE_TOMCAT=/location-to-tomcat-parent-directory/ CATALINA_HOME=$BASE_TOMCAT/tomcat CATALINA_BASE=$BASE_TOMCAT/$app_instance export JAVA_HOME CATALINA_HOME CATALINA_BASE $CATALINA_HOME/bin/startup.sh
#!/bin/bash app_instance=$1; BASE_TOMCAT=/location-to-tomcat-parent-directory/ CATALINA_HOME=$BASE_TOMCAT/tomcat CATALINA_BASE=$BASE_TOMCAT/$app_instance export JAVA_HOME CATALINA_HOME CATALINA_BASE $CATALINA_HOME/bin/shutdown.sh
Topology
Tiếp theo, thêm các dòng dưới đây:
port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/>
Multicast address: 228.0.0. Multicast port: 45564 (mutilcast address & port là nơi các instance xác nhận thành viên cluster) IP broadcaste: IP-Of-Node (khác 127.0.0.1) TCP port listening cho replication messages: range 4000-4100 (nếu trên cùng 1 máy thì các instance phải khác nhau) Listener: ClusterSessionListener Two interceptors: TcpFailureDetector & MessageDispatchInterceptor Deployer section:
watchDir : tạo thư mục apps tại /opt/tomcluster, được sử dụng để deploying và un-deploying trong cluster tempDir : used by the server while the deployment is underway. deployDir : where your application will be deployed (most probably webapps/ ) watchEnabled=true : chỉ đặt = true trên máy chủ đầu tiên (master node). The application will be deployed from the master to the other nodes.
Create user for tomcat cluster:
groupadd tomcat useradd -s /sbin/nologin -g tomcat -d /opt/tomcluster tomcat passwd tomcat chown -R tomcat.tomcat /opt/tomcluster
File war dùng để deploy:
SampleWebApp.wa r
https://drive.google.com/file/d/1m5dPDq-t9yak3jhd08Z5pf9J5a21p1az/view?usp=drives dk
Using mod_jk
Install
sudo apt-get install apache2 libapache2-mod-jk
Cấu hình workers.properties
workers chính là những Tomcat instance Virtual servers sẽ handle load balancing và monitoring cũng được mod_jk coi là worker
workers.tomcat_home=/home/quangtd/tomcat
workers.java_home=/usr/lib/jvm/java-1.8.0-openjdk-amd
worker.list=jkstatus, tomcat1,tomcat2, loadbalancer
worker.jkstatus.type=status worker.loadbalancer.type=lb
Đi tới URL /SampleWebApp/SessRepl trên các trình duyệt khác nhau:
You’ll need the following Apache modules for load balancing:
proxy proxy_http Proxy_balancer (for Balancer Manager) lbmethod_bytraffic (for round robin load balancing)
sudo a2enmod proxy sudo a2enmod proxy_http sudo a2enmod proxy_balancer sudo a2enmod lbmethod_byrequests
BalancerMember http://127.0.0.1: BalancerMember http://127.0.0.1:
ProxyPreserveHost On ProxyPass / balancer://mycluster/ ProxyPassReverse / balancer://mycluster/
Proxy : this additional Proxy block is used to define multiple servers. The block is named balancer:// mycluster (the name can be freely altered) and consists of one or more BalancerMember s, which specify the underlying backend server addresses. ProxyPass and ProxyPassReverse: these directives use the load balancer pool named mycluster` instead of a specific server.
upstream tomcat {
ip_hash;
server 10.100.100.11:8080; server 10.100.100.12:8080; } server { listen 80; server_name example.com;
location / { proxy_pass http://tomcat; } }
Ref:
https://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html https://www.oracle.com/webfolder/technetwork/tutorials/obe/cloud/apaas/java/tomcat- tcp-cluster/index.html https://www.virtuozzo.com/application-platform-docs/tomcat-server-clustering/ https://examples.javacodegeeks.com/enterprise-java/tomcat/tomcat-clustering- session-replication-tutorial/ https://www.xmodulo.com/configure-tomcat-cluster-ubuntu.html