集群redis整合springboot
【一】启动集群redis
在集群redis整合springboot前需要启动redis集群,集群至少需要六台redis服务,三台主,三台从机,具体请查看另外一篇:
redis主从,集群:https://mp.csdn.net/mdeditor/99772712#
【二】引入maven
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除springboot默认的logback日志框架 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入log4j日志依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
</dependencies>
【三】配置yml
每台节点之间使用逗号隔开
spring:
redis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 10
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 10
# 连接池中的最小空闲连接
min-idle: 0
# 连接超时时间(毫秒)
timeout: 20000
cluster:
max-redirects: 3
nodes: 192.168.179.129:7001,192.168.179.129:7002,192.168.179.129:7003
server:
port: 8080
【四】配置redis
public class RedisConfiguration {
@Autowired
private RedisTemplate redisTemplate;
/**
* 解决中文乱码
*/
@Bean
public RedisTemplate redisTemplateInit() {
//设置序列化Key的实例化对象
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置序列化Value的实例化对象
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
return redisTemplate;
}
}
【五】引入RedisUtil
这个请查看上一篇中有记录:
https://mp.csdn.net/mdeditor/99772712#
【六】调用
@RestController
@ResponseBody
public class RedisTestController {
@Autowired
RedisUtil redisUtil;
@GetMapping(value = "save")
public void test(){
redisUtil.set("慕和南道","15栋3单元");
}
}
【七】链接
https://www.cnblogs.com/zwcry/p/9176250.html
https://blog.csdn.net/u010199866/article/details/80705797
https://blog.csdn.net/qq_33371766/article/details/82223683
https://blog.csdn.net/x631617479/article/details/82380530