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/02 Report--
1. Introduction of redis
Redis is a key-value storage system. Similar to Memcached, it supports relatively more value types of storage, including
String (string), list (linked list), set (collection), zset (sorted set-ordered set), and hash (hash type). And memcached
Similarly, in order to ensure efficiency, data is cached in memory. The difference is that redis periodically writes updated data to disk.
Or write the modification operation to the appended record file, and realize master-slave (master-slave) synchronization on this basis.
Redis is a high-performance key-value database. The emergence of redis compensates for key/value like memcached to a great extent.
The deficiency of storage can play a good complementary role to the relational database on some occasions. It provides Java, Cumberbatch, C#
PHP, JavaScript, Perl, Object-C, Python, Ruby and other clients are easy to use.
If you simply compare the difference between Redis and Memcached, there are basically the following three points:
1. Redis not only supports simple KBH data, but also provides storage of list, set, zset, hash and other data structures.
2. Redis supports data backup, that is, data backup in master-slave mode.
3. Redis supports data persistence. You can keep the data in memory on disk, and you can load it again when you restart.
Use.
In Redis, not all data is stored in memory all the time. This is the biggest difference compared with Memcached.
Redis only caches all key information. If Redis finds that memory usage exceeds a certain threshold, swap will be triggered.
According to "swappability = age*log (size_in_memory)", Redis calculates which value corresponding to key requires swap
To the disk. The value corresponding to these key is then persisted to disk and cleared in memory. This feature makes Redis
You can maintain data that exceeds the memory size of its machine itself. Of course, the memory of the machine itself must be able to hold all the key
Because there is no swap operation on this data.
When reading data from the Redis, if the value corresponding to the read key is not in memory, then the Redis needs to read from the swap
The appropriate data is loaded in the file and then returned to the requestor.
Comparison between memcached and redis
1. Network IO model
Memcached is a multithreaded, non-blocking IO multiplexing network model, which is divided into listening main thread and worker subthread, listening thread monitoring
Listen to the network connection, accept the request, pass the connection description word pipe to the worker thread, read and write IO, and the network layer uses
Libevent encapsulated event library, multi-threaded model can play a multi-core role.
Redis uses a single-threaded IO reuse model and encapsulates a simple AeEvent event handling framework, which mainly implements epoll,
Kqueue and select, for simple IO operations, single thread can maximize the speed advantage, but Redis also mentions
It provides some simple computing functions, such as sorting, aggregation, etc., for these operations, the single-threaded model will actually seriously affect the whole.
Throughput, in the process of CPU calculation, the whole IO scheduling is blocked.
2. Memory management
Memcached uses pre-allocated memory pools, using slab and different sizes of chunk to manage memory, and value according to large
Small choose the appropriate chunk storage. Redis uses on-site memory requests to store data.
3. Storage mode and other aspects
Memcached basically only supports simple key-value storage, and does not support functions such as persistence and replication. Redis except key/value
It also supports many data structures such as list,set,sortedset,hash.
Second, how to maintain a session session
At present, 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 for clustering is load.
Balancing, and to achieve load balancing, every request of a user may be assigned to a non-fixed server, so we have to solve the problem first.
Determine the unity of session to ensure the normal use of users no matter which server the user's request is forwarded to.
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, such as hash policy based on accessing ip, that is, all requests of current users are located centrally.
To a server, so that a single server stores the user's session login information. If it is down, it is equivalent to a single point of service.
Signed, it will be lost, and the session will not be copied.
2. Session replication sharing: sessionreplication. For example, tomcat comes with session sharing, which mainly refers to the cluster environment.
Synchronize session among multiple application servers to make session consistent and transparent. If one of the servers occurs
Failure, according to the principle of load balancing, the scheduler will traverse to find available nodes, distribute requests, because the session has been synchronized, so
Can guarantee 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 larger objects are saved in session and the objects change
When faster, 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. Perform under large concurrence and
not good
3. Session sharing based on cache DB cache
Session sharing based on memcache/redis caching
Even if cacheDB is used to access session information, the application server accepts a new request to save the session information in cacheDB, when
When the application server fails, the scheduler will traverse for available nodes, distribute the request, and when the application server finds that the session is not
When you are in local memory, look for it in cache DB and copy it to this machine if you find it, so as to achieve session sharing and high availability.
Third, nginx+tomcat+redis realizes load balancing and session sharing.
1. Experimental environment
Host operating system IP address
Nginx 192.168.1.10
Tomcat-1 192.168.1.20
Tomcat-2 Centos7.2 192.168.1.30
Mysql 192.168.1.40
Redis 192.168.1.80
2. Experimental topology
In this figure, nginx acts as a reverse proxy to achieve static and dynamic separation, and customer dynamic requests are randomly assigned to two tomcat according to weight.
Server, redis serves as a shared session data server for two tomcat, and mysql serves as a back-end database for two tomcat.
3. Nginx installation and configuration
Using Nginx as the load balancer of Tomcat, the session Session data of Tomcat is stored in Redis, which can achieve zero downtime.
7x24 effect. Because the session is stored in Redis, Nginx does not have to be configured to stick paste some Tomcat way, so that
In order to truly achieve the load balancing of multiple Tomcat in the background.
(1) install nginx
Install the nginx dependency package
Yum-y install openssl-devel pcre-devel zlib-devel
Compile and install nginx
Link nginx command directory
Ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/
(2) configure nginx reverse proxy: reverse proxy + load balancer + health detection, nginx.conf file content:
Vim / usr/local/nginx/conf/nginx.conf add 20 add upstream backend_tomcat {server 192.168.1.20 upstream backend_tomcat 192.168.1.30 weight=1 max_fails=2 fail_timeout=10s;server 192.168.1.30 upstream backend_tomcat 8080 weight=1 max_fails=2 fail_timeout=10s;} 24 add proxy_connect_timeout 75 48 add location / {# root html; # index index.jsp index.html index.htm; proxy_pass http://backend_tomcat;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;}
50 add
(2) Open nginx and check the port
4. Install and deploy tomcat application servers (both)
(1) install tomcat and open it to view the port
20 tar zxf apache-tomcat-8.5.35.tar.gz
Move the extracted folder to / usr/local/ and rename it to tomcat
21 mv apache-tomcat-8.5.35 / usr/local/tomcat 22 / usr/local/tomcat/bin/startup.sh 23 netstat-anpt | grep 8080
Browser testing
All right, you can see the success of the visit. Our tomcat installation is complete. Let's modify the configuration file.
(2) set the default virtual host
Vim / usr/local/tomcat/conf/server.xml 150 add
(3) create the required directories and web pages
30 mkdir-p / web/webapp1 31 vim / web/webapp1/index.jsp
Tomcat-1 Session serviced by tomcatSession IDCreated on
(4) restart tomcat
(5) browser testing
The way to verify the health check is to shut down a tomcat host and test the access with a client browser. From the above results, we can see that there are two visits. Nginx distributes the access requests to the backend tomcat-1 and tomcat-2 respectively, and the client access requests achieve load balancing, but the sessionid is different. So, at this point, we are all ready, and let's configure tomcat to implement session persistence through redis.
5. Install redis
(1) compile and install redis
23 rz 24 tar zxf redis-4.0.14.tar.gz 25 mv redis-4.0.14 / usr/local/redis 26 cd / usr/local/redis/ 27 make & & make install
(2) change to the utils directory and execute the redis initialization script install_server.sh
29 cd utils/ 30. / install_server.sh 31 netstat-anput | grep redis
Through the above installation process, we can see that the redis configuration file after redis initialization is
/ etc/redis/6379.conf, log file is / var/log/redis_6379.log, data file dump.rdb is saved
Put it in the / var/lib/redis/6379 directory, and the startup script is / etc/init.d/redis_6379.
Now we are going to use systemd, so create a unit file name under / etc/systems/system
For redis_6379.service.
(3) modify / etc/redis/6379.conf
Bind 0.0.0.0 70 modification
Requirepass 123.com 501 to annotate and set the password
(4) restart redis and look at the port
/ etc/init.d/redis_6379 restart
Netstat-anput | grep redis
(5) redis test
6. Configure tomcat session redis synchronization (both tomcat)
(1) download the corresponding jar package for tomcat-redis-session-manager, and copy it to $TOMCAT_HOME/lib after the download is completed.
(2) modify / add the penultimate line of usr/local/tomcat/conf/context.xml
To annotate
(3) restart tomcat
(4) restart redis
(5) browser testing
As you can see, different tomcat are visited, but the session is the same, indicating that the purpose of the cluster has been achieved.
(6) redis test
Tomcat connects to the database
Mv / usr/local/tomcat/webapps/ROOT/ / web/webapp1/
192.168.1.80 as a mysql database server
(1) mysql configuration
Mysql-u root-p 123
Grant all on. To javauser@'192.168.1.%' identified by 'javapasswd'
Create database javatest
Use javatest
Create table testdata (id int not null auto_increment primary key,foo varchar (25), bar int)
Insert into testdata (foo,bar) values ('hello','123456'), (' ok','654321'), ('xgp','1123645')
Select * from testdata
(2) tomcat configuration (both)
Go to the configuration tutorial
/ usr/local/tomcat/conf/context.xml add the penultimate line
Create the required directories and web pages
55 cd / web/webapp1/ 56 mkdir WEB-INF 57 vim WEB-INF/web.xml add MySQL Test App DB Connection jdbc/TestDB javax.sql.DataSource Container
Create a test.jsp web page
Vim / web/webapp1/test.jsp modification
MySQL-1connect MySQL
Restart tomcat
40 / usr/local/tomcat/bin/shutdown.sh
41 / usr/local/tomcat/bin/startup.sh
(3) browser testing
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.