集群模式下可以保证单节点存储受限问题
1. Redis Cluster master-slave model 官方简介
In order to remain available when a subset of master nodes are failing or are not able to communicate with the majority of nodes, Redis Cluster uses a master-slave model where every hash slot has from 1 (the master itself) to N replicas (N-1 additional slaves nodes). In our example cluster with nodes A, B, C, if node B fails the cluster is not able to continue, since we no longer have a way to serve hash slots in the range 5501-11000. However when the cluster is created (or at a later time) we add a slave node to every master, so that the final cluster is composed of A, B, C that are masters nodes, and A1, B1, C1 that are slaves nodes, the system is able to continue if node B fails. Node B1 replicates B, and B fails, the cluster will promote node B1 as the new master and will continue to operate correctly. However note that if nodes B and B1 fail at the same time Redis Cluster is not able to continue to operate.
2. 工程pom.xml配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</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-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--jedis-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.demo</groupId>
<artifactId>dubbo-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
3. application.yml配置
4. Jediscluster bean获取配置
package com.example.dubboserver.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @date 2019/7/5 14:25
*/
@Configuration
public class RedisClusterConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisCluster redisClusterClient(){
Set<HostAndPort> jedisClusterNodes = new HashSet<>();
List<String> nodes = redisProperties.getCluster().getNodes();
for (String node : nodes) {
jedisClusterNodes.add(new HostAndPort(node.split(":")[0], Integer.valueOf(node.split(":")[1])));
}
//Jedis Cluster will attempt to discover cluster nodes automatically
JedisCluster jc = new JedisCluster(jedisClusterNodes);
return jc;
}
}
5. spring-boot and jedis调用
package com.example.dubboserver.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
import service.IDemoService;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @date 2019/6/12 15:01
*/
//@Service
@org.springframework.stereotype.Service
public class DemoServiceImpl implements IDemoService {
@Autowired
private RedisTemplate stringRedisTemplate;
@Autowired
private JedisCluster jedisCluster;
private AtomicInteger atomicInteger = new AtomicInteger(0);
@Override
public String check() {
Set<String> keys = new HashSet<>();
try {
Random random = new Random();
SetParams setParams = new SetParams();
setParams.ex(10);
String set = jedisCluster.set(String.valueOf(random.nextInt()), "key" + random.nextLong());
System.out.println("server ...." + set);
keys = getKeys("*");
int i = atomicInteger.incrementAndGet();
System.out.println(i);
} catch (Exception e) {
e.printStackTrace();
}
return keys.toString();
}
public Set<String> getKeys(String partern) {
Set<String> sets = new HashSet<>();
Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
for (Map.Entry<String, JedisPool> jedisPoolEntry : clusterNodes.entrySet()) {
String key = jedisPoolEntry.getKey();
JedisPool jedisPool = jedisPoolEntry.getValue();
try (Jedis resource = jedisPool.getResource()){
Set<String> keys = resource.keys(partern);
sets.addAll(keys);
} catch (Exception e) {
e.printStackTrace();
}
}
return sets;
}
}