spring boot JedisCluster
第一步:添加依赖
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency>
第二步:在配置文件中添加redis属性
spring.redis.nodes=xx.xx.xx.xx:xxxx,xx.xx.xx.xx:xx
spring.redis.password=
第三步:把配置文件映射成jave bean
@Component @ConfigurationProperties(prefix = "spring.redis") public class RedisSettingsProperties { private String nodes; private String password; public String getNodes() { return nodes; } public void setNodes(String nodes) { this.nodes = nodes; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
第四步:建立JedisCluster
@Configuration public class JedisClusterConfig { @Bean public JedisCluster getJedisCluster(RedisSettingsProperties propertie) { // 添加集群的服务节点Set集合 Set<HostAndPort> nodes = new HashSet<HostAndPort>(); String[] serverArray = propertie.getNodes().split(","); for (String ipPort : serverArray) { String[] ipPortPair = ipPort.split(":"); nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()))); } // Jedis连接池配置 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(100); jedisPoolConfig.setMaxTotal(500); jedisPoolConfig.setMinIdle(0); jedisPoolConfig.setMaxWaitMillis(2000); jedisPoolConfig.setTestOnBorrow(true); return new JedisCluster(nodes,jedisPoolConfig); } }
第五步:使用JedisCluster
@RestController public class TestController { @Autowired private JedisCluster jedisCluster; @RequestMapping("/testRedis") public Object testR() { return jedisCluster.hgetAll("F001.WF"); } }