In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In the previous article, we introduced the load balancing and static separation implemented by nginx, which can be seen here.
We said at the end of the article that one of the problems faced by load balancing is the synchronization of in-memory data. For example, I have two servers, An and B, to do load balancing. When I log in on server An and store the login data in session, the session data only exists on server A, not on server B. if I need to use session data when processing the next request, unfortunately, this request happens to be handled by server B. At this point, there will be a situation where server B cannot get session data, resulting in an error.
This is an unavoidable problem. There are several solutions, all of which can be summed up to synchronize data such as session in various load balancing branches. The first solution that comes to mind is to put these data in databases such as mysql, that is to say, there is a disk, but we all know that session appears because it is in memory, and programs read data in memory much faster than data on disk. So we put some frequently used things in session.
Is there a database that is stored in memory? This is redis. Generally speaking, it is a database, but the database exists in memory, so it is much faster to access than to read the data on disk. And because it is a database, data synchronization can be realized.
We store the session data in redis, and then all the cluster branches can access the things in this database, which is the principle of global caching.
1. The first step is to install redis, my server is windows, download is the installation-free version, decompress it, its directory is as follows. At first, redis does not need a password by default. If you want to set a password, you can go to the redis.windows.conf file to find requirepass, delete the previous #, and then set the password after it.
two。 From cmd, enter the root directory of redis and type the following directive: redis-server.exeredis.windows.conf. In this way, you can start redis, and if it starts successfully, the following screen will appear. Of course, you can also modify the conf file, plus the password. Requirepass xxxxx
3. Then we can do some configuration work to implement the global caching of session data.
1) the first step is to add jar package. If you are a maven project, you need to add the following code to pom.xml
Org.springframework.session spring-session-data-redis 1.3.1.RELEASE pom
If it is not a maven project, you need to add the following jar packages.
2) write redis.properties with the following code
Redis_isopen:yes# host address redis_hostName=xxx.xxx.xxx.xxx# port redis_port=6379# password redis_password=xxxxxxxx# connection timeout redis_timeout=200000redis_maxIdle:300redis_maxActive:600redis_maxWait:100000redis_testOnBorrow:true
It is basically similar to the connection statement that we configure the database.
3) write a spring-redis.xml configuration file that configures some basic information about redis.
4) in application.xml (the main configuration file of spring), you need to join the scan of redis.properties configuration file, as follows.
/ WEB-INF/classes/redis.properties
5) introduce spring-redis.xml into the main configuration file, as follows.
6) in web.xml, add a filter about session so that session can be manipulated by redis.
SpringSessionRepositoryFilter org.springframework.web.filter.DelegatingFilterProxy springSessionRepositoryFilter / *
After this, we have realized the management of session by redis.
7) We can install a redis client to view the data in it, which is called Redis Desktop Manager. As shown in the following figure, it is easy to see the data in the redis database.
PS. When you quit again, you need to write this way so that you won't make a mistake. (ssh Project)
Public String yipinExit () {Iteratorkeys=session.keySet () .iterator (); while (keys.hasNext ()) {String key=keys.next (); session.remove (key);} return "yipinExit";}
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support 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.