Redis server installation and configuration (mall 5)

Insert image description here

Redis is an open source NoSQL in-memory database for high-performance data storage and access. Redis supports multiple data types, including strings, hashes, lists, sets and ordered sets, and supports distributed storage and operations. Redis features include fast speed, high availability, and easy scalability, making it suitable for various application scenarios.

1. Redis server usage scenarios

1. Cache

The first application scenario of Redis is that Redis is used as a cache object to accelerate access to web applications.
Insert image description here

In this scenario, some data stored in the database will be accessed frequently. If the database is accessed frequently, the database load will increase. At the same time, because the database IO is slow, the response of the application will be poor. At this time, if Redis is introduced to store these frequently accessed data, it can effectively reduce the load on the database and improve the application's request response.

2. Session storage (Session)

Using Redis to store session data allows you to share user-related state data between stateless servers.
Insert image description here

When the user logs in to the web application, the session data is stored in Redis and the unique session ID (Session ID) is returned to the client's cookie. When the user sends a request to the application, this session ID will be included in the request. The stateless web server searches for relevant session data from Redis based on this session ID for further request processing.
What needs to be noted here is that Redis is an in-memory database, if single-instance deployment is used. Then when the Redis server fails and restarts, all Session sessions will disappear and the user has to log in again to obtain a new Session. Therefore, when using Redis to store Sessions, it is recommended to deploy it in a master-slave cluster mode. In this way, even if the main server hangs up, the slave database will immediately take over the traffic, which will not affect the user's use.

3. Distributed Lock

When we deploy multiple nodes in an application, there will be competition when these nodes need to operate the same resource. At this time, we can use Redis as a distributed lock to coordinate the operations of multiple nodes on shared resources.
In the figure below, only one of multiple servers can get the lock and perform shared resource operations. The server that gets lock fail needs to wait for a period of time before acquiring the lock.
Insert image description here

Although the simple implementation above can satisfy many use cases, it does not have a good fault tolerance mechanism. If it is to be used in production, it is recommended to use some higher quality distributed lock implementations. For example, for the Java platform, you can choose: Redisson.

4. Rate Limiter

Since Redis provides a counter function, we can use this capability and the timeout to implement a rate limiter. The most common scenario is that the server uses request current limiting.
A basic rate limiting implementation is as follows:
use the user ID or IP as the key, and use the INCR command to record the number of user requests. Then the number of requests is compared with the upper limit of allowed requests. Only when it is lower than the limit, request processing will be performed. If the limit is exceeded, the request is rejected.
At the same time, the counter for the number of requests needs to be set with a time window, such as 1 minute. That is to say, within one minute, the counter will be cleared and counted again. Therefore, when the current is limited in a time window, the request can be resumed in the next time window. To achieve rate limiting effect.
Insert image description here

In addition to the time window algorithm, the leaky bucket algorithm can also be implemented through Redis.

5. Ranking/Leaderboard

Since Redis provides the function of Sorted Sets, many game applications use Redis to implement various ranking functions.
Insert image description here

A sorted collection is a collection of unique elements (for example: user ID). Each element is sorted by score, so that elements can be quickly retrieved by score.

2. Install redis under CentOS8

1. Download redis from windows
https://download.redis.io/releases/redis-7.2.2.tar.gz
2. Unzip and upload to CentOS8
(1) Use winscp to log in to CentOS8
Insert image description here

(2) Upload the decompressed redis to the C entOS8 path /usr/local
Insert image description here

3. Enter CentOS8 and execute the following commands
(1) cd /usr/local to modify the current directory to /usr/local
(2) ll command to view files and permissions
(3) chmod –R +7 redis-7.2.2 configuration Access permissions under redis-7.2.2 and its subdirectories are readable and writable
Insert image description here

4. Execute the make command to install. If it can be installed normally, wait until it is completed (the conditions for this installation are to be completed on the premise that nacos, seata, rabiitmq, and elasticsearch have been installed. First, execute yum –y update. Updated, so it can work normally when executing make)
Insert image description here
Insert image description here

This completes the make installation
. 5. If an error is reported when executing the make command, redis is developed in C language. Therefore, you must ensure that gcc has been installed on the server before installation. Then execute the following command to install the C language compiler:
Insert image description here

yum install -y cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
make MALLOC=libc

6. Continue to execute the command installation
. Note that prefix=/data/redis specifies the installation directory.
Insert image description here

7. Verify whether the installation is successful:
(1) You can see the redis-server file under src in the installation directory
Insert image description here

(2) Execute the command ./redis-server &
to start the redis service, as shown below:
Insert image description here

(3) The redis-server
command to start according to the redis.conf configuration file is not in the same path as the configuration file. The execution command is as follows:
Cd /usr/local/redis-server-7.2.2/src
./redis-server …/redis .conf
Insert image description here

(4) After startup, it is as follows:
Insert image description here

(5) Execute the redis-cli command under src to start the command mode of redis-server, as shown below:

Insert image description here

(6) Specify the IP address and password to log in to redis-server.
The command format is: redis-cli –a password –h ip.
In this example, execute: redis-cli –a yuanfeng021 –h 192.168.0.132
Insert image description here

(7) Stop redis-server
Cd /usr/local/redis-7.2.2/src
Redis-cli shutdown

8. Configure environment variables
Vi /etc/profile
export REDIS_HOME=/usr/local/redis-7.2.2/src
export PATH= PATH : PATH:PATH:REDIS_HOME
Insert image description here

9. Remote connection to the client. The redis configuration file only allows access on the local machine (the redis service can only be accessed on the machine on which it is started). Remote connection is not allowed. If remote access is required, execute the following command
cd /usr/local/redis-7.2.2
vim redis.conf

10. Modify the line
#bind 127.0.0.1 -::1
0.0.0.0 to allow any IP to access
bind 0.0.0.0
Insert image description here

11. Open port 6379 of redis and reload the firewall
firewall-cmd -add-port=6379/tcp --permanent
firewall-cmd -reload
Insert image description here

12. Use the command to test port 6379, telnet 192.168.0.132 6379, the effect is as follows:

Insert image description here

13. Test the connection to redis-server

Insert image description here

Insert image description here

14. Use the redis Desktop Manager tool to connect to the redis server
Insert image description here
. 15. After the connection is successful, the connected db database can be displayed normally
Insert image description here
. This is the end of this article. Please criticize and correct any deficiencies.

Guess you like

Origin blog.csdn.net/weixin_43075093/article/details/134812428