首页 > 学院 > 开发设计 > 正文

spring-boot-redis-cluster-demo

2019-11-06 06:43:58
字体:
来源:转载
供稿:网友

摘要

Redis在项目中使用频率是很高的,使用的时候经常都是以Redis集群的形式。现整理一下SPRing-Boot整合redis cluster最基础配置,方便以后查阅。

依赖包

下面2个依赖是spring-boot集成Redis的必备依赖。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>${spring-boot.version}</version></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>1.4.4.RELEASE</version></dependency>

如果启用spring-boot单元测试,还需要加入下面的依赖。

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${spring-boot.version}</version> <scope>test</scope></dependency>

配置

application.yml配置Redis集群节点信息

spring: redis: cluster: nodes: - redis1.itclj.com:7000 - redis1.itclj.com:7001 - redis2.itclj.com:7002 - redis2.itclj.com:7003 - redis3.itclj.com:7004 - redis3.itclj.com:7005

初始化

spring-boot默认都采用注解方式初始化bean。 首先建一个redis集群配置bean,从配置文件中读取配置到配置bean里面。 其次建一个redis Cluster初始化配置bean,用于初始化Redis Cluster。

集群配置Bean

由于复杂配置项,如数组不能通过@Value注解直接读取配置项,所有只能采用新建配置Bean通过@ConfigurationProperties注解读取。

@Configuration@ConfigurationProperties(prefix = "spring.redis.cluster")public class RedisClusterProperties { //集群节点 private List<String> nodes=new ArrayList<>(); public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; }}

初始化RedisCluster

@Configuration@ConditionalOnClass(RedisClusterConfig.class)@EnableConfigurationProperties(RedisClusterProperties.class)public class RedisClusterConfig { @Resource private RedisClusterProperties redisClusterProperties; @Bean public JedisCluster redisCluster(){ Set<HostAndPort> nodes = new HashSet<>(); for (String node:redisClusterProperties.getNodes()){ String[] parts= StringUtils.split(node,":"); Assert.state(parts.length==2, "redis node shoule be defined as 'host:port', not '" + Arrays.toString(parts) + "'"); nodes.add(new HostAndPort(parts[0], Integer.valueOf(parts[1]))); } return new JedisCluster(nodes); }}

测试

@RunWith(SpringRunner.class)@SpringBootTestpublic class RedisTest { @Autowired private JedisCluster jedisCluster; @Test public void get(){ System.out.println("=============="+jedisCluster.get("youqian-spread-sync-to-MySQL-date")); }}

原文地址:http://www.itclj.com/blog/58bcf3f947508f786718d4f3 项目地址:https://github.com/clj198606061111/spring-boot-redis-cluster-demo


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表