SpringBoot integrates Redis multi-master and multi-slave clusters
Environmental preparation
First we need to prepare a redis cluster. You can refer to the article I wrote:https://blog.csdn.net/m0_51510236/article/details/132684529. This time I have prepared a cluster, as shown in the figure (a multi-master and multi-slave redis cluster):
The code warehouse address used in this article:https://gitcode.net/m0_51510236/redis-cluster-example
SpringBoot integrates Redis cluster
New Project
Let’s create a new SpringBoot project to connect to this cluster and go tospring initializr for initialization:
Then we import it into the development tool, as shown in the figure:
Modify SpringBoot configuration file
Edit application.yaml
file, the file content is:
# 设置端口
server:
port: 8080
spring:
redis:
cluster:
# 设置redis集群当中都有哪些节点,注意修改为你自己的节点地址
nodes:
- 192.168.1.171:6379
- 192.168.1.172:6379
- 192.168.1.173:6379
- 192.168.1.174:6379
- 192.168.1.175:6379
- 192.168.1.176:6379
Write code tests
We will write a controller to test this cluster
Write DTO
We first write a DTO class that is stored in redis RedisValueDTO.java
:
package city.yueyang.redis.entity;
import java.io.Serializable;
/**
* <p>
* 存储Redis值类型的数据传输类
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-09-06 星期三 11:21:28
* @file RedisValueDTO.java
*/
public class RedisValueDTO implements Serializable {
/**
* Redis的Key
*/
private String key;
/**
* 设置到Redis当中的值
*/
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Write Controller
Write a Controller class that can set and get redis values RedisController.java
:
package city.yueyang.redis.controller;
import city.yueyang.redis.entity.RedisValueDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
/**
* <p>
* 操作Redis的Controller
* </p>
*
* @author XiaoHH
* @version 1.0.0
* @date 2023-09-06 星期三 11:49:01
* @file RedisController.java
*/
@RestController
@RequestMapping("/redis")
public class RedisController {
/**
* 注入Redis的操作对象
*/
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 设置一个值到Redis当中
*
* @param value 设置的内容对象
* @return 返回Success
*/
@PostMapping
public String set(@RequestBody RedisValueDTO value) {
this.redisTemplate.opsForValue().set(value.getKey(), value.getValue());
return "Success";
}
/**
* 根据key在Redis当中获取一个值
*
* @param key redis的key
* @return 缓存当中的值
*/
@GetMapping("/{key}")
public String get(@PathVariable("key") String key) {
return this.redisTemplate.opsForValue().get(key);
}
}
The project structure at this time is:
Code warehouse address:https://gitcode.net/m0_51510236/redis-cluster-example
Test the code written
Let’s directly use the http tool that comes with the development tool idea to test. The test code is as follows:
### 设置的值的内容
POST http://localhost:8080/redis
Content-Type: application/json
{
"key": "k1",
"value": "v1"
}
### 获取值的内容
GET http://localhost:8080/redis/k1
We test the first setting value and see that Success is returned:
We test the first content to get the value, and we can see that the value we set is successfully obtained v1
:
This concludes SpringBoot's integration of Redis.