In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
Foreword:
In order to enable web to adapt to large-scale access, it is necessary to implement the cluster deployment of applications. The most effective solution of the cluster is load balancing, and every request of load balancing users may be assigned to a non-fixed server, so we must first solve the unity of session to ensure the normal use of users no matter which server their requests are forwarded to, that is, we need to implement the sharing mechanism of session.
There are several solutions to achieve session unification under the cluster system:
1. Precise location of requests: sessionsticky, for example, the hash policy based on accessing ip, that is, all current user requests are located in one server, so that a single server stores the user's session login information. If it goes down, it is equivalent to a single point of deployment, which will be lost and the session will not be replicated.
2. Session replication sharing: sessionreplication, for example, tomcat comes with session sharing, which mainly refers to synchronizing session among multiple application servers in a cluster environment to make session consistent and transparent. If one of the servers fails, according to the principle of load balancing, the scheduler will traverse to find available nodes and distribute requests. Because the sessio has been synchronized, it can ensure that the user's session information will not be lost, session replication.
Inadequacies of this scheme:
Must be done between the same kind of middleware (e.g. between tomcat-tomcat).
The performance loss caused by session replication will increase rapidly. Especially when large objects are saved in session, and the objects change rapidly, the performance degradation is more significant, which will consume the system performance. This feature limits the horizontal expansion of web applications. Session content is synchronized to members through broadcasting, which will cause network traffic bottlenecks, even intranet bottlenecks. The performance is not good under the big concurrency.
3, session sharing based on cacheDB cache: even if cacheDB is used to access session information, the application server accepts new requests to save session information in cacheDB. When the application server fails, the scheduler will traverse to find available nodes and distribute the request. When the application server finds that session is not in local memory, it looks for it in cacheDB, and if found, it copies it to the local machine, so as to achieve session sharing and high availability.
Here we will use the third scheme, which implements session session sharing, and will use Redis to do cache DB.
Blog outline:
I. Environmental preparation
Configure Nginx reverse proxy server
Configure the Tomcat server
Configure the Redis server
5. Configure Tomcat connection Redis
VI. Install and deploy MySQL database
7. Configure Tomcat to connect to MySQL database
I. Environmental preparation
Before making the following configuration, download the source code package I provided and upload it to the corresponding server.
Configure Nginx reverse proxy server
Here the configuration of Nginx reverse proxy, only to achieve a simple proxy function, if you want to optimize this reverse proxy server, then it is best to refer to the blog article: Nginx installation, reverse proxy and depth optimization configuration.
The following operations are done on the Nginx server of 192.168.20.2
[root@nginx ~] # yum-y erase httpd # Uninstall the included web service [root@nginx ~] # yum-y install openssl-devel pcre-devel # installation depends on [root@nginx ~] # tar zxf nginx-1.14.0.tar.gz-C / usr/src # unpack [root@nginx ~] # cd / usr/src/nginx-1.14.0/ # change to the unzipped directory [root@ Nginx conf] #. / configure-- user=www-- group=www-- prefix=/usr/local/nginx & & make & & make install# compilation and installation [root@nginx nginx-1.14.0] # cd / usr/local/nginx/conf/ # switch to the Nginx configuration file home directory [root@nginx conf] # vim nginx.conf # Edit Nginx configuration file Write the following content http {# add the following content to the http field upstream backend {server 192.168.20.3 upstream backend 8080 weight=1 max_fails=2 fail_timeout=10s Server 192.168.20.4 weight=1 max_fails=2 fail_timeout=10s; 8080 weight=1 max_fails=2 fail_timeout=10s;} server {# navigate to the server field location / {# add the following below the location field and comment out the first two lines # root html; # index index.html index.htm; proxy_pass http://backend; # mainly writes this line, the following is optimized, optional write proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 After the modification is completed Save and exit [root@nginx conf] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ # soft connect to Nginx commands [root@nginx conf] # useradd-M-s / sbin/nologin www # create a running user [root@nginx conf] # nginx-t # check the configuration file [root@nginx conf] # nginx # start the Nginx service
At this point, the Nginx can provide basic reverse proxy functionality, and then configure the Tomcat server.
Configure the Tomcat server
The following actions need to be configured on both Tomcat servers
The host Tomcat1 operates as follows:
[root@tomcat1 ~] # ls | grep tomcat # upload the following source code package apache-tomcat-8.5.35.tar.gz [root@tomcat1 ~] # tar zxf apache-tomcat-8.5.35.tar.gz-C / usr/src [root@tomcat1 ~] # mv / usr/src/apache-tomcat-8.5.35 / usr/local/tomcat [root@tomcat1 ~] # cd / usr/local/tomcat/conf/ [root@tomcat1 conf] # vim server.xml # add the following configuration below # above is to configure the virtual host Specify that the root of the web page is / web/webapp1. After adding, save and exit [root@tomcat1 conf] # mkdir-p / web/webapp1 # create the root directory of the web page [root@tomcat1 conf] # vim / web/webapp1/index.jsp # write the first page file as follows: tomcat-1 # later on the tomcat2 host, you need to change the number in the title field to 2 to view the load balancing effect Session serviced by tomcatSession IDCreated on [root@tomcat1 conf] # / usr/local/tomcat/bin/startup.sh # start tomcat service
Configure all the operations of the above Tomcat1 host on the Tomcat2 host once.
When the Tomcat2 host is also configured, you can use the client to access the Nginx proxy server (access: 192.168.20.2). Refresh the page several times and you will see the following page in turn (note that the Session ID is not the same):
Next, configure the Redis cache server. The purpose of the Redis cache server is to save the session session information of the client and use it for the Tomcat server to share this session session information, so that it does not have to update the session session every time the client requests.
IV. Configure Redis server [root@redis ~] # ls | grep redis # upload the following source code package redis-4.0.14.tar.gz [root@redis ~] # tar zxf redis-4.0.14.tar.gz-C / usr/src # unpack [root@redis ~] # cd / usr/src # switch to the decompressed path [root@redis src] # mv redis-4.0.14/ / usr/local/redis # Directly move to the specified path and rename [root@redis src] # cd / usr/local/redis/ # switch to the redis directory [root@redis redis] # make & & make install # without configuration Directly compile and install [root@redis redis] # cd utils/ # enter the subdirectory [root@redis utils] #. / install_server.sh # initialize Redis # all options for initialization remain default Enter all the way to confirm that # is to confirm the listening port, configuration file, log file, pid storage path and other information. # omit part of the content Successfully added to chkconfigSuccessful added to runlevels 345 starting Redis server...Installation fulfilling Indicates that the initialization is successful [root@redis utils] # vim / etc/redis/6379.conf # Editing the Redis configuration file bind 0.0.0.0. find the line that has not been commented, modify it to 0.0.0.The requirepass foobared # navigate to the line change, and modify it as follows: after the password of requirepass 123.com# Redis is modified as 123.com#, save and exit. [root@redis utils] # / etc/init.d/redis_6379 restart # restart the Redis service [root@redis utils] # redis-cli-h 192.168.20.5 # login database 192.168.20.5 redis-cli 6379 > set a b # insert data Test (error) NOAUTH Authentication required. # insert failed because there was no password authentication 192.168.20.5123.com 6379 > AUTH 123.com # authenticated with AUTH. The password is "123.com" OK192.168.20.5:6379 > set a b # defined in the configuration file. Insert data OK # again successfully
At this point, the Redis server is configured successfully. Next, configure Tomcat so that it can write Session information to Redis.
Configure Tomcat connection Redis1, The host Tomcat1 configuration is as follows: [root@tomcat1 ~] # cd / usr/local/tomcat/lib/ [root@tomcat1 lib] # rz # upload the following four package files from my network disk to the current directory commons-pool2-2.4.2.jar mysql-connector-java-5.1.22.jarjedis-2.9.0.jar tomcat85-session-redis-1.0.jar [root@tomcat1 lib] # vim.. / conf/ Context.xml # Edit this file # navigate to this line Remove the comment symbol and write the following grant all on *. * to javauser@'192.168.20.%' identified by 'javapasswd' under the line # create test user mysql > create database javatest; # create test library mysql > use javatest; # switch to the created library mysql > create table testdata (id int not null auto_increment primary key,foo varchar (25), bar int); # create table mysql > insert into testdata (foo,bar) values ('hello','123456'), (' ok','654321'), ('lvtest','123123'); # insert data mysql > select * from testdata into the table # query the inserted data as follows: +-+ | id | foo | bar | +-+ | 1 | hello | 123456 | 2 | ok | 654321 | | 3 | lvtest | 123123 | +-+ 3 rows in set (sec) 3, Configure the host Tomcat1 [root@tomcat1 ~] # vim / usr/local/tomcat/conf/context.xml # Edit the context.xml configuration file # add the following above at the end of the file # add the above above to the last line # add and exit [root@tomcat1 ~] # mkdir / web/webapp1/WEB-INF # create the directory [root@tomcat1 ~] # vim / web/webapp1/WEB-INF/web. Xml # write the following content MySQL Test App DB Connection jdbc/TestDB javax.sql.DataSource Container # after writing Save and exit [root@tomcat1 ~] # cd / web/webapp1/ # switch to the root directory of the web page [root@tomcat1 webapp1] # vim test.jsp # write the test file MySQL-1 # when the second Tomcat operates, modify 1 to 2 in the title to test the load balancing effect connect MySQL
# after writing, save and exit, and then restart the Tomcat service [root@tomcat1 lib] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat1 lib] # / usr/local/tomcat/bin/startup.sh
At this point of configuration, the client accesses the test.jsp file of the host Tomcat1, and you can see the following page, indicating that there is no problem with the host Tomcat1 connecting to the database. The resulting page is as follows:
You can see the above page, and then do the following in Tomcat2 (the same as the host Tomcat1, so no comments)
4. Configure host Tomcat2: [root@tomcat2 ~] # vim / usr/local/tomcat/conf/context.xml [root@tomcat2 ~] # mkdir / web/webapp1/WEB-INF [root@tomcat2 ~] # vim / web/webapp1/WEB-INF/web.xml MySQL Test App DB Connection jdbc/TestDB javax.sql.DataSource Container [root@tomcat2 ~] # cd / web/webapp1/ [root@tomcat2 webapp1] # vim test.jspMySQL-2 connect MySQL
[root@tomcat2 lib] # / usr/local/tomcat/bin/shutdown.sh [root@tomcat2 lib] # / usr/local/tomcat/bin/startup.sh
At this point, the client accesses the Nginx proxy server (192.168.20.2/test.jsp) and refreshes it several times. You can see the switch between Tomcat1 and Tomcat2:
Note that in the above environment, Redis only stores Session information and does not cache data from the back-end server like Memcached does.
-this is the end of this article. Thank you for reading-
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.