SpringBoot2.0对Redis-Cluster的使用提供了支持,这也是我比较喜欢使用SpringBoot的原因之一,减少配置,加快开发速度。
org.springframework.boot
spring-boot-starter-data-redis
复制代码
一、yml配置
spring:
redis:
jedis:
pool:
max-wait:5000
max-Idle:50
min-Idle:5
cluster:
nodes:127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006
timeout:500复制代码
二、使用推荐的RedisTemplate使用Redis-Cluster
RedisClusterConfiguration.java
@Configuration
public class RedisClusterConfiguration{
@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionfactory) {
RedisTemplate redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}复制代码
单元测试
RedisClusterTest.java
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisClusterTest{
@Autowire
private RedisTemplate redisTemplate;
@Test
public void getValue(){
ValueOperations operations=redisTemplate.opsForValue();
System.out.println(operations.get("key1"));
}
}复制代码
在前一篇当中我们在Redis-Cluster中存入了key1-value1这样的键值对,位置在Slot[2292]
输出:"value1"
这是springboot2.0的新的功能,如果使用springboot1.x的话,得使用jedisCluster这样的类,当然也可以在springboot2中使用jedisCluster,当然也可以在springMvc中使用,没有版本上的限制。
三、JedisCluster使用Redis-Cluster
redis.clients
jedis
复制代码
application.yml配置
springmvc下支持.properties文件,请自改
#redis集群配置
redis:
# Redis服务器连接密码(默认为空)
password: *****
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 5000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1
# 连接池中的最大空闲连接
max-idle: 30
# 连接池中的最小空闲连接
min-idle: 5
# 连接超时时间(毫秒)
timeout: 50000
commandTimeout: 50000
#集群
cluster:
nodes: 192.168.0.159:7001,192.168.0.159:7002,192.168.0.159:7003,192.168.0.159:7004,192.168.0.159:7005,192.168.0.159:7006
复制代码
RedisConfig.class-java Config配置
import java.net.UnknownHostException;
import java.util.HashSet;
import java.util.Set;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
@ConditionalOnClass({JedisCluster.class})
public class RedisConfig{
@Value("${spring.redis.cluster.nodes}")
private String clusterNodes;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.commandTimeout}")
private int commandTimeout;
@Value("${spring.redis.password}")
private String password;
@Bean
public JedisCluster getJedisCluster(){
String[] cNodes=ClusterNodes.split(",");
Set node=new HashSet();
//分割出集群点
for(String node:cNodes){
String[] hp=node.split(":");
nodes.add(new HostAndPort(hp[0],Integer.parseInt(hp[1])));
}
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
JedisCluster jedisCluster=new JedisCluster(nodes,commandTimeout,timeout,maxIdle,password,jedisPoolConfig);
return jedisCluster;
}
}复制代码
编写使用工具类
JedisUtils.java
mport org.springframework.beans.factory.annotation.Autowired;import redis.clients.jedis.JedisCluster;public class JedisClientCluster implements JedisClient { @Autowired private JedisCluster jedisCluster;
@Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String get(String key) { return jedisCluster.get(key); } @Override public Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); } }
复制代码
springmvc版-applicationContext-redis.xml配置
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7001">constructor-arg> bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7002">constructor-arg> bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7003">constructor-arg> bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7004">constructor-arg> bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7005">constructor-arg> bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.153">constructor-arg> <constructor-arg name="port" value="7006">constructor-arg> bean> set> constructor-arg> bean>复制代码
测试
如果是springboot的话与上一致
Spring mvc测试
@Test public void testJedisClient() throws Exception { //初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml"); //从容器中获得JedisClient对象 JedisClient jedisClient = applicationContext.getBean(JedisClient.class); jedisClient.set("first", "100"); String result = jedisClient.get("first");
}复制代码
注:这只是简单的使用redis集群,如果项目的处理逻辑并不复杂只是考虑数据量和并发度,可以使用这样的架构。但使用实战的话还是有所欠缺,还需要好好学习技术和架构。
勉励~