Springboot Redis (standalone/cluster)

Source: Internet
Author: User
Tags redis desktop manager redis cluster redis server

Objective

There are so many redis in front of you to use in the project.

Here, to see separately, stand-alone version and the cluster version in the use of springboot. Inside, I will also post the Jedis version, as a comparison.

  

Stand-alone version

1. Pom.xml

<!--Https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -<Dependency>    <groupId>Org.springframework.boot</groupId>    <Artifactid>Spring-boot-starter-data-redis</Artifactid>    <version>1.5.9.RELEASE</version></Dependency>

2. Application.yml

Spring:  redis:    port:6379    host:127.0.0.1    Password:redis

A password is set for Redis, which can be set in the redis.conf file: requirepass password

3. Controller

@RestController @requestmapping ("Simple") Public classSimplecontroller {@AutowiredPrivatestringredistemplate stringredistemplate;@GetMapping ("Set")     Public voidset () {Valueoperations<string, string> operations =Stringredistemplate.opsforvalue ();Operations.set ("1", "1a"); Operations.set ("2", "2b"); Operations.set ("3", "3c"); Operations.set ("4", "4d"); Operations.set ("5", "5e"); Operations.set ("Elvin", "Elvin"); Operations.set ("ABC", "ABC"); Operations.set ("Xingming", "xingming"); }}

4. Jedis

Take a look at how Jedis is playing under a single-machine version of Redis.

@Test Public voidTestjedispool ()throwsException {//First step: Create a Jedispool object. You need to specify the IP and port on the server side. Jedispool Jedispool =NewJedispool ("127.0.0.1", 6379); //Step Two: Get the Jedis object from the Jedispool. Jedis Jedis =Jedispool.getresource (); Jedis.auth ("Redis"); //Step Three: use Jedis to operate the Redis server. String result = Jedis.get ("abc");        SYSTEM.OUT.PRINTLN (result); //Fourth step: Close the Jedis object after the operation is complete, and the connection pool recycles the resource. Jedis.close (); //Fifth step: Close the Jedispool object. Jedispool.close (); }

I will not show the result, through the above steps, the controller can be stored in the data, read out.

Here is a point to note that if step 3 is not stringredistemplate, but redistemplate, then through step 4 is not read out.

If you have a Redis desktop manager, you can use this to look at it and know why you can't read it.

Why does it happen?

Can look at the source code of Redistemplate:

As you can see, Jdkserializationredisserializer is used here to serialize key and value.

Straight point of view, you can see:

So, here can be seen, why use ABC directly to check, is not found the desired results.

Cluster edition

In the cluster, if you are using Spring-boot-starter-data-redis words, you will find that super convenient, as long as the configuration files can be changed, the other can not be changed.

1. Application.yml

Spring:  redis:    cluster:      nodes:       -127.0.0.1:7001       -127.0.0.1:7002       - 127.0.0.1:7003       -127.0.0.1:7004       -127.0.0.1:7005       -127.0.0.1:7006    123456

Configure the cluster node inside the application.

2. Controller

The method in the controller does not change, or use that. View directly with Terminal operation:

It's actually in there.

3. Jedis

@Test Public voidTestjediscluster ()throwsException {//First step: Use the Jediscluster object. A setsetNewHashset<>(); Nodes.Add (NewHostandport ("127.0.0.1", 7001)); Nodes.Add (NewHostandport ("127.0.0.1", 7002)); Nodes.Add (NewHostandport ("127.0.0.1", 7003)); Nodes.Add (NewHostandport ("127.0.0.1", 7004)); Nodes.Add (NewHostandport ("127.0.0.1", 7005)); Nodes.Add (NewHostandport ("127.0.0.1", 7006)); Jediscluster Jediscluster=NewJediscluster (Nodes, 2000, 5, 8, "123456",Newgenericobjectpoolconfig ()); //Step two: Operate Redis directly using the Jediscluster object. A singleton exists in the system. String result = Jediscluster.get ("abc"); //Step Three: Print the resultsSystem.out.println (Result); //Fourth step: Close the Jediscluster object before the system shuts down. Jediscluster.close (); }

Here is a comparison of the egg pain is that if the cluster set a password, and can not be Jediscluster.auth () mode to enter the password

At first, I thought it was not recommended, who knows, this TM is not used AH. Too much, almost.

Through the Jedis code, you can find that the standalone version and the cluster version, the operation of the object is not the same, then in the process of development, how to unify it? (When developing, do not need to use Redis cluster, when on-line, directly switch the past is possible)

So want to solve this problem, can be solved by the policy mode, define an operation interface, define the method in the interface, I tube you single-machine or cluster, all want to implement this interface. So in the process of operation, it is unified to the interface. The rest is assignment and switching.

And the use of Spring-boot-starter-data-redis does not need to consider so much, it is indeed convenient many.

Springboot Redis (standalone/cluster)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.