1. What is Redis

Redis is a noSQL database, its data is stored in memory, and Redis can periodically synchronize the memory data to disk, that is, data can be persistent. And it supports more data structures than memcached (string,list list [queue and stack],set[collection],sorted set[ordered collection] hash)

2. Why redis

When we do a project, for example personal blog, we put this project to your own server, in general, we do this project without commercial, traffic will be small, this time, we can also do not use the redis, can to traffic after mysql directly, as we all know, mysql is on disk, It takes a little bit of time to load, which we can ignore when traffic is low. However, when we do a large commercial project, such as Taobao, the number of visits will increase exponentially, especially during the Double Eleven, the concurrency will reach its peak. At this time, if we still use mysql, the large number of visits will make the server processing speed very slow and even break down. So at this time we need to use Redis, redis is in memory, we know that memory reads data very fast, when a large number of visits yu Yao to access a certain data, we store the data in Redis, so that our server will not break down.

3. Install Redis in CentOs7

3.1 install the WGET download tool

yum -y install wget
Copy the code

3.2 Download the corresponding version of Redis using the wGET tool:download.redis.io/releases/

Wget HTTP: / / http://download.redis.io/releases/redis-5.0.5.tar.gzCopy the code

3.3, here can also use the good Redis compression package

  • Check if there is a build environment (updated or installed)

    yum install gcc-c++

Unzip the downloaded Redis software and move it to /use/local

Tar -zxvf redis-5.0.5.tar.gz mv redis-5.0.5 /usr/local/Copy the code

GCC GCC GCC GCC GCC GCC GCC GCC GCC

CD/usr/local/redis - 5.0.5 / makeCopy the code
  • Note that the. Redis. Conf configuration file is in the root directory of redis by default

  • Start Redis and go to the redis SRC directory java. /redis-server.. /redis.conf will involve background startup or foreground startup, that is, whether the classic logo of Redis will appear, just need to change in the redis.conf configuration file

    You can change the boot mode by modifying the configuration file

    daemonize no\yes

View Redis process information

ps -ef | grep  redis
Copy the code
  • If yes, the following information is displayed:
  • [root@localhost src# ps -ef |grep redis
  • root 58043 1 0 14:47 ? 00:00:00 ./redis-server *:6379
  • Root 58080 9737 0 14:47 PTS /0 00:00:00 grep color=auto redis

4. Enter Redis

/redis-cli --raw = /redis-cli --raw = /redis-cliCopy the code

5. Introduction to Jedis

Redis not only uses commands to operate, now basically mainstream languages have client support, such as Java,C.C#, C++.php.node.js.go, etc. In the web of listed some Java client, has Jedis, Redisson, Jredis, JDBC – Redis, such as the official recommended Jedis and Redisson. The most commonly used in enterprises is Jedis. Let’s focus on studying Jedis. Jedis is also hosted on Github at github.com/xetorthio/j…

== Open the port number of CentOS system! Key, do not open port 6379 ping is not through the background want to go through Jedis redis link is not ==

6. CentOS open ports

  • 1、运行命令:firewall-cmd –get-active-zones
  • 2. Run the firewall-cmd –zone=public –add-port=6379/ TCP –permanent command
  • 3. Restart the firewall and run the firewall-cmd –reload command
  • 4, check whether the port number is enabled, run the following command: firewall-cmd –query-port=6379/ TCP to enable the CentOS firewall port 6379

7. Java connects to Redis

Import the jar package

  • Jedis – 2.9.0. Jar
  • Commons-pool2-2.6.2. jar Commons pool2-2.6.2.jar

Java Connection testing

Public class Testredis {public static void main(String[] args) {Jedis Jedis = new Jedis(" IP address ",6379); System.out.println(jedis.ping()); // Close the resource jedis.close(); }}Copy the code

Single instance connection

Jar public class TestRedis {public static void main(String[] args) {jedis jedis = new Jedis (" IP address ", 6379); String test = jedis.get("test"); System.out.println("test = " + test); Jedis.set ("test","hi, this is the first key set "); Test = jedis.get("test"); System.out.println("test = " + test); // Close the resource jedis.close(); }}Copy the code

Connection pool connection

Public class TestRedis01 {public static void main(String[] args) { JedisPoolConfig config = new JedisPoolConfig(); Config.setmaxtotal (30); //1.2 Maximum number of idle connections config.setMaxIdle(10); JedisPool JedisPool = new JedisPool(config," IP address ",6379); Jedis jedis=null; //3. Get the core object jedis = jedispool.getResource (); Jedis.set ("name"," this is the connection pool key "); String name = jedis.get("name"); System.out.println("name = " + name); //6. Close resource jedis.close(); }}Copy the code

Test the connection

Installation may encounter potholes

If the startup fails, check the Redis conf configuration file

Since Redis binds 127.0.0.1 by default, you might be tempted to comment out “bind127.0.0.1” in the redis configuration file redis.conf. We still get an exception, and the exception message suggests a number of ways to do this, one of which is to turn off protected mode. Redis has protected mode enabled by default, ensuring that only the host can access it.

The correct solution to jedis conneciton refused is as follows:

First, shut down redis-server, open the redis configuration file redis.conf, and comment out bind 127.0.0.1. Bind 127.0.0.1 to 127.0.0.1. Bind 127.0.0.1 to 127.0.0.1. The default protected mode is yes. You need to change it to protected mode no before restarting Redis. If you run the Java file, you will find that you can get the key of Redis.

System.out.println(jedis.ping()); The reason for writing this is not that there will be a problem with installing Redis, but that there will be a problem with configuring Redis, which will cause Java to fail to connect to Redis. Many tutorials on the Internet are not using the default firewall, and HERE I use the default firewall to add port 6379.

end~~

Remember to like and follow!