spring boot下JedisCluster客户端的配置,连接Redis集群
来源:互联网 发布:centos 6 搜狗输入法 编辑:程序博客网 时间:2024/04/29 20:09
1,pom依赖添加:
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<type>jar</type>
<scope>compile</scope>
<version>2.8.0</version>
</dependency>
2,application.properties中配置:
#redis cluster
redis.cache.clusterNodes=120.125.122.103:5000,120.125.122.103:5001,120.125.122.103:5002,120.125.122.103:5003,120.125.122.103:5004,120.125.122.103:5005
redis.cache.commandTimeout=5000
#unit:second
redis.cache.expireSeconds=120
3,新增类RedisProperties JedisClusterConfig,核心代码如下:
@Component
@ConfigurationProperties(prefix = "redis.cache")
public class RedisProperties {
private int expireSeconds;
private String clusterNodes;
private int commandTimeout;
}
@Configuration
public class JedisClusterConfig {
@Autowired
private RedisProperties redisProperties;
/**
* 注意:
* 这里返回的JedisCluster是单例的,并且可以直接注入到其他类中去使用
* @return
*/
@Bean
public JedisCluster getJedisCluster() {
String[] serverArray = redisProperties.getClusterNodes().split(",");//获取服务器数组(这里要相信自己的输入,所以没有考虑空指针问题)
Set<HostAndPort> nodes = new HashSet<>();
for (String ipPort : serverArray) {
String[] ipPortPair = ipPort.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes, redisProperties.getCommandTimeout());
}
}
特别注意:@ConfigurationProperties(prefix = "redis.cache") 中redis.cache要和application.properties中的前缀对应。
4,使用:
@Autowired
private JedisCluster jc ;
- spring boot下JedisCluster客户端的配置,连接Redis集群
- spring boot下JedisCluster客户端的配置,连接Redis集群
- Spring Boot JedisCluster配置
- redis集群客户端JedisCluster优化
- spring集成 JedisCluster 连接 redis3.0 集群
- Springboot Redis分布式集群(4)- JedisCluster连接测试集群
- redis集群JedisCluster优化
- JedisCluster 操作集群Redis
- spring boot下redis的配置使用
- redis集群客户端JedisCluster优化 - 管道(pipeline)模式支持
- redis集群客户端JedisCluster优化 - 管道(pipeline)模式支持
- 阿里云redis集群用JedisCluster无法连接
- Redis JedisCluster Spring整合
- redis - JedisCluster Spring整合
- jedis(redis)整合spring,包括jedis客户端单机版,jedis集群版配置 ,连接池配置
- 使用jedisCluster操作Redis集群
- jedisCluster整合spring 的配置说明
- 搭建Spring MVC+JedisCluster集群
- 创建filter,及web.xml文件配置
- Java练习:Overriding Vs. Overloading经典笔试题
- Chapter 3. GRUB
- First Bad Version
- macOS 安装 python
- spring boot下JedisCluster客户端的配置,连接Redis集群
- 什么是敲诈者病毒
- 关于Java的path,classpath,java_home环境变量的配置与具体含义
- Syncthing免费开源的文件同步工具-带版本控制可用于构建个人云存储
- Java注释和注释模板
- 善用Eclipse的代码模板功能
- Apache与Tomcat有什么关系和区别
- Charpter 5. Linux Filesystem Tree Layout
- Go学习笔记-Go语言数组array和切片slice