In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Tomcat to build clusters in Nginx. The content of the article is of high quality, so Xiaobian shares it with you as a reference. I hope you have a certain understanding of related knowledge after reading this article.
Create a Maven project, add dependencies
Create a Maven project using IntelliJ IDEA and add dependencies as follows:
redis.clients jedis 2.9.0 org.springframework.data spring-data-redis 1.8.4.RELEASE org.springframework.session spring-session 1.3.1.RELEASE org.apache.commons commons-pool2 2.4.2 org.springframework spring-webmvc ${spring-framework.version}
There are a few dependencies to add here, but they can be roughly divided into three categories:
1. Java client for Redis, which operates Redis through Java code
2. Spring Data Redis, which simplifies Redis operations
3. Spring Session, this is used for Session management
4.Spring
Build Spring+SpringMVC environment
First create a configuration file for spring+springmvc under the resources folder:
Then configure spring and springmvc in the web.xml file as follows:
contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener springmvc org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-servlet.xml 1 springmvc /
Then simply configure spring in the applicationContext.xml file, as follows:
Configure annotations to be scanned by Spring container.
Then configure the annotations to be scanned by the SpringMVC container in spring-servlet.xml as follows:
Spring-servlet.xml is relatively simple to configure.
OK, so far, Spring+SpringMVC environment has been successfully built.
Configuring Spring Session and Redis
Session has always been a headache when we do cluster, before there is an open source control tomcat-redis-session-manager on GitHub, but this thing is currently only supported to Tomcat7, not the best option, we can also use IP_tables provided by Nginx to locate the same request IP to the same server, but there is no way to make full use of the performance of the service cluster, Spring Session can be very good to help us solve these problems, it has the following characteristics:
1. Spring Session provides redis, jvm map, mongo, gemfire, hazelcast, jdbc and other ways to store session containers.
2. The same browser and the same website support multiple sessions.
3. Not dependent on cookies. sessionID can be passed via header
4. WebSocket and spring-session combination, synchronous lifecycle management.
5. using simple
OK, let's take a look at how to configure our Spring Session, first add filters to the web.xml file:
springSessionRepositoryFilter org.springframework.web.filter.DelegatingFilterProxy springSessionRepositoryFilter /*
This uses the proxy filter provided by Spring Web to pass all intercepted requests to a filter called springSessionRepositoryFilter for processing. OK, then configure Spring Session and Redis in applicationContext.xml as follows:
hostName is the address of redis and port is the service port of redis.
OK, write all this OK, let's write a simple jsp page test, remember to start your redis service before testing oh.
test
The index.jsp page is as follows:
Title
I try to make this page as simple as possible. There are two buttons. The first button stores data into the session, and the second button fetches data from the session. The corresponding Controller is as follows:
@Controllerpublic class HelloController { @RequestMapping("/setsession") @ResponseBody public void setSession(HttpSession session, String name) { session.setAttribute("name", name); } @RequestMapping(value = "/getsession",produces = "text/html;charset=utf-8") @ResponseBody public String getSession(HttpSession session, HttpServletRequest req) { return session.getAttribute("name").toString()+"-----"+req.getServletContext().getRealPath("/"); }}
When I write data to httpsession, it is automatically stored in redis cache/database. I added the project deployment path to the returned session data to distinguish which server handled the request after deployment to the cluster. Start the project with the following page:
Click Submit to save the data to redis, as follows:
Then go back to the start page and click the Get button to get the values in the Session, as follows:
OK, it's easy!
At this point, our Spring Session+Redis implementation Session sharing this function is completed. Let's look at how to build a cluster for this simple web project.
Preparation for cluster setup
1. Download Nginx (http://nginx.org/), the version of Nginx I use here is nginx-1.12.0
2. Download Tomcat, the Tomcat version I'm using here is apache-tomcat-8.5.12
The network topology diagram is as follows:
Copy the Tomcat download in two copies as follows:
Modify the Tomcat configuration file (conf/server.xml) and reset the port number. Each Tomcat server.xml needs to be modified in three places, as follows:
There are three places to modify. I will add 1 to the default port number, and then add 2 to the port number of the second Tomcat. This is convenient for me to run multiple Tomcats on the same machine later.
OK, after doing all this, you can start these two Tomcats first to see if they can be accessed normally.
deployment project
OK, after both Tomcats are configured, we copy the project just now to tomcat's webapps directory and copy it to both Tomcats. I won't repeat it here.
configure Nginx
Download Nginx, unzip, configure conf/nginx.conf file, as follows:
Once the configuration is complete, start nginx. Retest.
test
Save data to session:
Get data from session:
Take it a few more times and you'll see that the requests are handled by different servers, as follows:
About how to use Tomcat to build clusters in Nginx to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.
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.