我有一个 redis集群,包含主服务器,从服务器和3个标记服务器。主服务器和从服务器映射到dns名称为node1-redis-dev.com,node2-redis-dev.com。 redis服务器版本为2.8
我在application.properties文件中包含以下内容。
TestBed.createComponent(AppComponent);
但是当我检查StringRedisTemplate时,我看到了localhost而不是JedisConnectionFactory的hostName属性下的集群信息。
此外,我在JedisPool的creationStackTrace属性中看到了异常。
spring.redis.cluster.nodes=node1-redis-dev.com:6379,node2-redis-dev.com:6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=-1
spring.redis.pool.max-wait=-1
CasheRepository类如下所示,
java.lang.Exception
at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:139)
at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:107)
at redis.clients.util.Pool.initPool(Pool.java:43)
at redis.clients.util.Pool.<init>(Pool.java:31)
at redis.clients.jedis.JedisPool.<init>(JedisPool.java:80)
at redis.clients.jedis.JedisPool.<init>(JedisPool.java:74)
at redis.clients.jedis.JedisPool.<init>(JedisPool.java:55)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createRedisPool(JedisConnectionFactory.java:228)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.createPool(JedisConnectionFactory.java:204)
我使用spring-boot-starter-redis 1.2.7的spring boot 1.3.4导入jedis 2.7.3依赖。
使用Spring启动应用程序集成redis集群时我缺少什么?
答案 0 :(得分:1)
所需的只是在RedisClusterConfiguration中设置集群节点的初始集合,并将其提供给JedisConnectionFactory。
@Configuration
class Config {
List<String> clusterNodes = Arrays.asList("node1-redis-dev.com:6379", "node2-redis-dev.com:6379");
@Bean
RedisConnectionFactory connectionFactory() {
return new JedisConnectionFactory(new RedisClusterConfiguration(clusterNodes));
}
@Bean
RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
// just used StringRedisTemplate for simplicity here.
return new StringRedisTemplate(factory);
}
}
答案 1 :(得分:0)
以下应该可以工作
application.properties
spring.redis.cluster.nodes=127.0.0.1:6379
spring.redis.cluster.max-redirects=3
ClusterConfigurationProperties.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class ClusterConfigurationProperties {
/*
* spring.redis.cluster.nodes[0] = 127.0.0.1:7379 spring.redis.cluster.nodes[1]
* = 127.0.0.1:7380 ...
*/
private List<String> nodes;
/**
* spring.redis.cluster.max-redirects=3
*/
private int maxRedirects;
/**
* Get initial collection of known cluster nodes in format {@code host:port}.
*
* @return
*/
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
public int getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(int maxRedirects) {
this.maxRedirects = maxRedirects;
}
}
RedisConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import io.lettuce.core.ReadFrom;
@Configuration
public class RedisConfig {
@Autowired
private ClusterConfigurationProperties clusterProperties;
@Bean
LettuceConnectionFactory redisConnectionFactory(RedisClusterConfiguration redisConfiguration) {
LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
.readFrom(ReadFrom.REPLICA_PREFERRED).build();
return new LettuceConnectionFactory(redisConfiguration, clientConfig);
}
@Bean
RedisClusterConfiguration redisConfiguration() {
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(clusterProperties.getNodes());
redisClusterConfiguration.setMaxRedirects(clusterProperties.getMaxRedirects());
return redisClusterConfiguration;
}
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@Primary
RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(new StringRedisSerializer());
// other settings...
return template;
}
}