Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Tomcat and load balancing configuration

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The following brings you how to deploy Tomcat and Load Balancer configuration, hoping to bring you some help in practical application. Load Balancer involves many things, but there are not many theories. There are many books on the Internet. Today, we will use the accumulated experience in the industry to make an answer.

Tomcat Cloud Virtual Machine, as a free open source web application server, belongs to lightweight application server, which is widely used in small and medium-sized systems and occasions where concurrent users are not many. It is the first choice for developing and testing JSP programs. Generally speaking, Tomcat, like apache or Nginx web servers, has the ability to process HTML pages. However, because its ability to process static pages is far less than apache or Nginx, Tomcat is generally used as a servlet and JSP container. It runs separately on the backend. Tomcat application scenarios are as follows:

The apache/Nginx server is always accessed by users, and then the apache/Nginx server is handed over to the Tomcat server for processing. All servers are connected to the shared storage server, so that the data accessed by users is the same every time. apache/Nginx is used for scheduling, which is known as Load Balancer. There is not much explanation about Load Balancer.

In general, a Tomcat site cannot be used in a production environment alone due to problems such as a single point of failure and an inability to cope with too many complex and diverse customer requests, so Load Balancer is needed to solve these problems.

Nginx is a very good http server software, it can support up to 50000 concurrent connections, has strong static resource processing capabilities, runs stably, and memory, CPU and other system resources consumption is very low. At present, many large websites apply Nginx server as a reverse proxy and Load Balancer for backend website programs to improve the load concurrency capability of the entire site.

The deployment environment is as follows:

I. Preparations:

Three Centos 7 and system mirrors

Tomcat and Nginx source package (can be downloaded from the official website)

My package https://pan.baidu.com/s/1mqrs2jW-6ADRA2uUf6ERBg

Extraction code: mu4j

Start to do Tomcat on 1.10 server (I am lazy, firewall configuration is free, I directly stop the service, if necessary, please configure the release)

[root@localhost /]# java -version #Check whether Java is installed, if not, install openjdk version "1.8.0_102"OpenJDK Runtime Environment (build 1.8.0_102-b14)OpenJDK 64-Bit Server VM (build 25.102-b14, mixed mode)

II. Installation Configuration Tomcat:

[root@localhost /]# tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src/[root@localhost /]# cd /usr/src/[root@localhost src]# mv apache-tomcat-8.5.16/ /usr/local/tomcat8 #Move generated files to/usr/src [root@localhost /]# mkdir -p /web/webapp1 #Create a root directory for Java web sites [root@localhost /]#vim/web/webapp1/index.jsp #Create test files JSP test1 page [root@localhost /]# vim /usr/local/tomcat8/conf/server.xml ............................... #Add context to Host // docBase: Document reference directory for web applications // reloadable: Set whether the monitoring "class" changes // path="": Set default "class"

Start Tomcat:

.............. Close Tomcat Change startup.sh to shutdown.sh [root@localhost /]# /usr/local/tomcat8/bin/startup.sh #Start TomcatUsing CATALINA_BASE: /usr/local/tomcat8Using CATALINA_HOME: /usr/local/tomcat8Using CATALINA_TMPDIR: /usr/local/tomcat8/tempUsing JRE_HOME: /usrUsing CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jarTomcat started. [root@localhost /]# netstat -anpt | grep 8080tcp6 0 0 :::8080 :::* LISTEN 4280/java tcp6 0 0 192.168.1.10:8080 192.168.1.10:41946 TIME_WAIT -

Test access 192.168.1.10:8080 as follows:

.

So far, Tomcat 192.168.1.10 has been configured. The configuration of another Tomcat server 192.168.1.20 is exactly the same as that of 192.168.1.10. You can configure the above configuration on the 192.168.1.20 server once. However, in order to test, you can see the effect of Load Balancer, so that we can see that the server visited each time is not the same one. The Tomcat server test page for 192.168.1.20 needs to be different from the page for 192.168.1.10.

.

However, in a real production environment, two Tomcats must use the same shared storage server to access, no matter which server provides services to users, users must receive the same page.

Third, deploy Nginx:

Required dependencies to install:

[root@localhost /]# yum -y install pcre-devel zlib-devel openssl-devel [root@localhost /]# tar zxf nginx-1.12.0.tar.gz -C /usr/src/[root@localhost /]# groupadd www #create rungroup [root@localhost /]# useradd -g www -s /bin/false #Create a running user and add it to the group,/bin/false is equivalent to/sbin/nologin [root@localhost nginx-1.12.0]# cd /usr/src/nginx-1.12.0/[root@localhost nginx-1.12.0]# ./ configure --prefix=/usr/local/nginx --user=www --group=www --with-file-aio --with-http_stub_status_module--with-http_gzip_static_module --with-http_flv_module && make && make install// --user=,--group= Specify user and group to run//--with-file-aio Enable file modification support//--with-http_stub_status_module Enable status statistics//--with-http_gzip_static_module Enable gzip static compression//--with-http_flv_module Enable flv module to provide time-based offset file for memory usage//--with-http_ssl_module Enable ssl module [root@localhost /]# vim /usr/local/nginx/conf/nginx. conf.............................................................................. #gzip on; Navigate to this row and add down upstream tomcat_server { server 192.168.1.10:8080 weight=1; server 192.168.1.20:8080 weight=1; } #Write it here The #weight parameter indicates the weight. The higher the weight, the higher the probability of being assigned. #In order to test the effect is obvious, set the weight to the same server { listen 80; server_name localhost;............................................................. location / { root html; index index.html index.htm; proxy_pass http://tomcat_server; #Add this line, the name after http needs to be the same as the upstream name set before }

Optimize Nginx:

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #Create link file for main program [root@localhost ~]# vim /etc/init.d/nginx #Edit Service Script #!/ bin/bash# chkconfig: - 99 20PROG="/usr/local/nginx/sbin/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" in start) $PROG ;; stop) kill -s QUIT $(cat $PIDF) ;; restart) $0 stop $0 start ;; reload) kill -s HUP $(cat $PIDF) ;; *) echo "USAGE:$0 {start | stop | restart | reload}" exit 1esacexit 0[root@localhost ~]# chmod +x /etc/init.d/nginx #add execute permissions [root@localhost ~]# chkconfig --add nginx #Add as System Service [root@localhost nginx-1.12.0]# nginx -t #Check the master configuration file for errors nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost ~]# systemctl start nginx #Start the Nginx service to confirm the script is working properly [root@localhost ~]# netstat -anpt| grep nginx #Check whether port 80 is listening tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 90475/nginx: master

IV. Interview test:

Visit 192.168.1.30 to get the following:

Changes after refresh

After reading the above information on how to deploy Tomcat and Load Balancer configuration, if you still have anything to know, you can find out what you are interested in the industry information or find our professional technical engineers to answer. Technical engineers have more than ten years of experience in the industry.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report