通过<util:property-path /> 动态配置JedisCluster 节点
配置文件(统一命名):
# redis cluster redis.node1.host=192.168.1.61 redis.node1.port=7000 redis.node2.host=192.168.1.62 redis.node2.port=7000 redis.node3.host=192.168.1.63 redis.node3.port=7000 redis.node4.host=192.168.1.64 redis.node4.port=7000 redis.node5.host=192.168.1.65 redis.node5.port=7000 redis.node6.host=192.168.1.66 redis.node6.port=7000
RedisConfig.java
import java.io.IOException; import java.util.LinkedHashSet; import java.util.Properties; import java.util.Set; import org.springframework.core.io.ClassPathResource; import redis.clients.jedis.HostAndPort; class RedisConfig { private Set<HostAndPort> nodeSet = new LinkedHashSet<HostAndPort>(); public RedisConfig() { Properties properties = getProperties(); int loop = properties.keySet().size() / 2; String hostKeyFormat = "redis.node%s.host"; String portKeyFormat = "redis.node%s.port"; for (int i = 1; i <= loop; i++) { String hostKey = String.format(hostKeyFormat, i); String portKey = String.format(portKeyFormat, i); if (properties.containsKey(hostKey) && properties.containsKey(portKey)) { String hostValue = properties.getProperty(hostKey); Integer portValue = Integer.valueOf(properties.getProperty(portKey)); HostAndPort hostAndPort = new HostAndPort(hostValue, portValue); nodeSet.add(hostAndPort); } } } public Properties getProperties() { Properties properties = new Properties(); try { properties.load(new ClassPathResource("env/redis.properties").getInputStream()); } catch (IOException e) { throw new RuntimeException("读取属性文件", e); } return properties; } public Set<HostAndPort> getNodeSet() { return nodeSet; } public void setNodeSet(Set<HostAndPort> nodeSet) { this.nodeSet = nodeSet; } }
spring bean configuration:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <bean id="redisConfig" class="com.lzkj.csp.mgr.cache.redis.RedisConfig"></bean> <!-- redis --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <util:property-path path="redisConfig.nodeSet"/> </constructor-arg> <constructor-arg><value>2000</value></constructor-arg> <constructor-arg><value>100</value></constructor-arg> <constructor-arg> <ref bean="jedisPoolConfig"/> </constructor-arg> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100"/> <property name="maxIdle" value="20"/> <property name="minIdle" value="10"/> <property name="blockWhenExhausted" value="true"></property> <property name="maxWaitMillis" value="3000" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="testWhileIdle" value="true" /> </bean> </beans>
实现动态配置的关键点是:
util:property-path
该命名空间通过Java Bean 动态初始化属性后,引入到spring bean configuration 中