- A+
所属分类:redis
springboot整合redisCluster集群:(前提redis集群已经搭建好。windows上搭建redis集群参考:https://www.cnblogs.com/super-chao/p/9329018.html。linux上搭建redis集群步骤和windows上搭建redis集群一致)
1.引入springboot和redis的相关jar包:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <artifactId>spring-aop</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <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> <exclusions> <exclusion> <artifactId>spring-aop</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> </dependencies>
2.创建application.yml配置文件并配置redis集群:
#server:
# port: 8762
spring:
application:
name: redis
#redis配置
redis:
# host: 192.168.1.100
# port: 6385
pool:
max-idle: 100
min-idle: 1
max-active: 1000
max-wait: -1
database: 0
timeout: 100000
cluster:
nodes:
#地址要和redis配置中bind地址一致
- 127.0.0.1:6379
- 127.0.0.1:6380
- 127.0.0.1:6381
- 127.0.0.1:6382
- 127.0.0.1:6383
- 127.0.0.1:6384
3.redis工具类,RedisClusterService:
package com.springbootrediscluster.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit; @Servicepublic class RedisClusterService { @Autowired private StringRedisTemplate stringRedisTemplate; public void setStr(String key, String value) { setStr(key, value, null); } public void setStr(String key, String value, Long time) { /*stringRedisTemplate.opsForValue().set(key, value); if (time != null) stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);*/ stringRedisTemplate.opsForValue().set(key,value); if(time != null){ stringRedisTemplate.expire(key, time, TimeUnit.SECONDS); } /*if(time != null){ jedisCluster.expire(key,(int) (time/1000)); }*/ } public Object getKey(String key) { /*return redisTemplate.opsForValue().get(key);*/ return stringRedisTemplate.opsForValue().get(key); } public void delKey(String key) { /*stringRedisTemplate.delete(key);*/ stringRedisTemplate.delete(key); } }
4.controller类,IndexController:
package com.springbootrediscluster.controller;import com.springbootrediscluster.redis.RedisClusterService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * Created by Administrator on 2021/8/15. */@Controllerpublic class IndexController { @Autowired private RedisClusterService redisClusterService; @RequestMapping("/setRedis") @ResponseBody public String setRedis(String key, String value) { redisClusterService.setStr(key, value); return "success"; } @RequestMapping("/getKey") @ResponseBody public Object getKey(String key){ Object result = redisClusterService.getKey(key); return result == null ? "缓存中没有该数据" : result; } @RequestMapping("/delStringKey") @ResponseBody public String reStrRedis(String key){ redisClusterService.delKey(key); return "success"; } }
5.springboot的app启动类App:
package com.itmayiedu.app;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.itmayiedu.**") @EnableAutoConfigurationpublic class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
6.测试:
也可在dos命令窗口查看值,或者通过RedisClient可视化工具查看redis的值。
至此,springboot整合redisCluster集群完成。项目中可直接使用工具类RedisClusterService(可自行重构)对redisCluster集群操作。