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

Introduction of three ways to realize load balancing in apache+tomcat

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The following will give you an introduction to the three ways to achieve apache+tomcat load balancing, hoping to give you some help in practical application. Load balancing involves a lot of things, there are not many theories, and there are many books on the Internet. Today, we will use the accumulated experience in the industry to do an answer.

End

1. First, install apache. After compiling, you can access it through the IP: port. If "it workers" is returned, it proves that Apache starts successfully (note that the project path of apache must be correct)

two。 Download JK from http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.31/. If the http CVM is an Apache2.0.X version, you must select mod_jk-1.2.31-httpd-2.0.52.so. There is an English description at the bottom of the page, which you can take a look at The mod_jk-1.2.23-apache-2.2.x-linux-x86_64.so I advertised here matches my operating system version, then renames it to mod_jk.so, and assigns 777permissions.

3. Copy mod_jk.so to apache/modules

4. Add the following to httpd.conf

Include "/ usr/local/apache/conf/mod_jk.conf"

5. Create a new mod_jk.conf file under the conf path and add the following

# load mod_jk Module

LoadModule jk_module modules/mod_jk.so

# specify workers.properties file path

JkWorkersFile conf/workers.properties

# specify which requests are to be processed by tomcat. "loadbalancer" is the name of the load distribution controller specified in workers.propertise

JkLogFile logs/mod_jk.log

# JkLogLevel debug

JkMount / * .do loadbalancer

JkMount / * .jsp loadbalancer

6. Create a new workers.properties file under conf and add the following

Worker.list=loadbalancer

# this is consistent with mod_jk.conf

Worker.worker2.port=8009

# ajp13 port number in tomcat2, corresponding to Connector port= "8009" in tomcat configuration file server.xml. Default is 8009.

Worker.worker2.host=127.0.0.1

# worker2 address, native loaclhost or 127.0.0.1, write IP remotely

Worker.worker2.type=ajp13

# consistent with the protocol of server.xml in worker2

Worker.worker2.lbfactor=1

# the weight of the load. The higher the load, the greater the load.

Worker.worker3.port=9009

Worker.worker3.host=127.0.0.1

Worker.worker3.type=ajp13

Worker.worker3.lbfactor=1

Worker.loadbalancer.type=lb

Worker.loadbalancer.balance_workers=worker2,worker3

# worker list of specified loads, separated by commas

Worker.loadbalancer..sticky_session=false

# configure whether the session session is sticky

# in this way, the load balancer lb will try its best to maintain a session, that is, to enable users to interact with the same Tomcat in one session

Worker.loadbalancer..sticky_session_force=false

Worker.loadbalancer.sticky_session=1

# whether to enable session sharing

7. Download tomcat and decompress it, copy 2 copies of tomcat, and keep it as worker2,worker3

Modify the server.xml of 2 tomcat to ensure that the port numbers of the 2 tomcat are inconsistent. The specific configuration is as follows. You need to modify the bold and bold mark.

Worker2 is as follows:

[html] view plaincopy

Worker3 is as follows:

[html] view plaincopy

8. Create project test1 under 2 tomcat, and add andashu.jsp, as follows:

9. Access 2 tomcat, http://IP:8081/test1/andashu.jsp and http://10.14.133.204:8080/test1/andashu.jsp respectively. If the log catalina.out of both tomcat prints =, it proves that both tomcat accesses are ok.

10. Launch apache worker2,worker3 respectively, and use different browsers or different machines to access http://IP/test1/andashu.jsp respectively. If the = is printed in the log catalina.out of both tomcat, it proves that the load balancer has been successfully built (here, you need to use different browsers or different machines to access it)

11. If the trial node fails, randomly stop one of the tomcat and continue to access http://IP/test1/andashu.jsp, which can be accessed normally.

The other two ways can also achieve load balancing.

2 、 ajp_proxy

Uncomment the following in the httpd.conf file (delete the # sign) and open the configuration below

Include conf/extra/httpd-vhosts.conf

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

LoadModule proxy_connect_module modules/mod_proxy_connect.so

LoadModule proxy_ftp_module modules/mod_proxy_ftp.so

LoadModule proxy_http_module modules/mod_proxy_http.so

Note:

Except for mod_proxy.so,mod_proxy_balancer.so,mod_proxy_connect.so.

If you use ajp_proxy, you need to load the module mod_proxy_ajp.so.

If you use http_proxy, you need to load the module mod_proxy_http.so.

Httpd.conf, delete the configuration in jk mode and add the following configuration

ProxyRequests Off

BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=worker3

BalancerMember ajp://127.0.0.1:8009loadfactor=1 route=worker2

Conf/extra/httpd-vhosts.conf add configuration

ServerAdmin xxxx@xxx.com

ServerName localhost

ServerAlias localhost

ProxyPass / balancer://loadbalancer/ stickysession=jsessionid nofailover=On

ProxyPa***everse / balancer://loadbalancer/

ErrorLog "logs/loadbalancer-error.log"

CustomLog "logs/loadbalancer-access.log" common

The configuration completes the task, accesses http://IP:8081/test1/andashu.jsp, and checks whether it can be accessed properly.

3 、 http_proxy

The configuration of http_proxy is similar to that of ajp_proxy. The httpd.conf configuration of method 2 is modified as follows, and the rest remains the same.

ProxyRequests Off

BalancerMember http://127.0.0.1:9009 loadfactor=1 route=worker3

BalancerMember http://127.0.0.1:8009 loadfactor=1 route=worker2

Configure to complete the task, access http://IP:8081/test/test.jsp, and check whether it can be accessed properly.

After reading the above introduction of the three ways to achieve load balancing in apache+tomcat, if there is anything else you need to know, you can find out what you are interested in in the industry information or find our professional and technical engineers for answers. 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

Internet Technology

Wechat

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

12
Report