Springboot redis (single-host/cluster), springbootredis
Preface
Redis has done so much to use in the project.
Here, let's take a look at the usage of the standalone version and the cluster version in springboot. In this example, I will also post the Jedis version for comparison.
Standalone Edition
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
Here we have set a password for redis, which can be set in the redis. conf file: requirepass Password
3. controller
@RestController@RequestMapping("simple")public class SimpleController { @Autowired private StringRedisTemplate stringRedisTemplate; @GetMapping("set") public void set(){ 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
Let's take a look at how Jedis works in single-host redis.
@ Test public void testJedisPool () throws Exception {// Step 1: Create a JedisPool object. You must specify the Server ip address and port. JedisPool jedisPool = new JedisPool ("127.0.0.1", 6379); // Step 2: Obtain the Jedis object from the JedisPool. Jedis jedis = jedisPool. getResource (); jedis. auth ("redis"); // Step 3: Use Jedis to operate the redis server. String result = jedis. get ("abc"); System. out. println (result); // Step 4: Close the jedis object after the operation is completed, and Recycle resources from the connection pool. Jedis. close (); // Step 5: close the JedisPool object. JedisPool. close ();}
The result is not displayed. The data stored in the controller can be read through the above steps.
Note the following: If Step 3 uses RedisTemplate instead of StringRedisTemplate, it cannot be read through step 4.
If you have installed redis desktop manager, you can use this to check whether it is readable.
Why is this happening?
Let's take a look at the source code of RedisTemplate:
We can see that JdkSerializationRedisSerializer is used to serialize key and value.
For more information, see:
So, here we can see that why can't we find the desired result directly with abc.
Cluster version
In the cluster, if you are using spring-boot-starter-data-redis, you will find that it is very convenient to change the configuration file, you can leave it unchanged.
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 password: 123456
Configure the cluster node in the application.
2. controller
The method in the controller remains unchanged. You can use that method to directly view the method using the Terminal operation:
Actually saved.
3. Jedis
@ Test public void testJedisCluster () throws Exception {// Step 1: Use the JedisCluster object. A Set <HostAndPort> parameter is required. List of Redis nodes. Set <HostAndPort> nodes = new HashSet <> (); nodes. add (new HostAndPort ("Fig", 7001); nodes. add (new HostAndPort ("Fig", 7002); nodes. add (new HostAndPort ("Fig", 7003); nodes. add (new HostAndPort ("Fig", 7004); nodes. add (new HostAndPort ("Fig", 7005); nodes. add (new HostAndPort ("127.0.0.1", 7006); JedisCluster jedisCluster = new JedisCluster (nodes, 2000, 5, 8, "123456", ne W GenericObjectPoolConfig (); // Step 2: Use the JedisCluster object to operate redis directly. Exists in a single instance in the system. String result = jedisCluster. get ("abc"); // Step 3: print the result System. out. println (result); // Step 4: Disable the JedisCluster object before the system is shut down. JedisCluster. close ();}
One thing that hurts is that if a password is set for the cluster, the password cannot be entered through jedisCluster. auth ().
At the beginning, I thought this was not recommended. Who knows, this tm cannot be used. It's too much.
Through Jedis code, we can find that the operating objects of the standalone and cluster versions are different. How can we unify the operating objects during development? (Redis clusters are not required during development. You can directly switch over when you go online)
To solve this problem, you can use the policy mode to define an operation interface and define a method in the interface. You need to implement this interface either on a single machine or on a cluster. in the operation process, the interface is unified. the rest is the assignment and switching.
The use of spring-boot-starter-data-redis does not need to be considered so much, which is indeed much more convenient.