本文介绍disconf如何管理redis(单机和集群)的配置
# redis jedis version 2.4.2
redis.host=localhost
redis.port=6379
redis.password=
新建redis-single.xml,内容为:
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
p:use-pool="true" />
<bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:defaultSerializer-ref="stringRedisSerializer" />
</beans>
这里主要是用的spring data redis,注入redisTemplate就可以使用spring data封装的api
public class JedisClusterFactory implements FactoryBean<JedisCluster>, InitializingBean {
private JedisCluster jedisCluster;
private Integer timeout;
private String cluster;
private Set<HostAndPort> parseHostAndPort() {
String[] clusters = this.cluster.split(",");
Set<HostAndPort> haps = new HashSet<HostAndPort>();
for (String c : clusters) {
String[] ipAndPort = c.split(":");
HostAndPort hap = new HostAndPort(ipAndPort[0], Integer.parseInt(ipAndPort[1]));
haps.add(hap);
}
return haps;
}
@Override
public JedisCluster getObject() throws Exception {
return jedisCluster;
}
@Override
public Class<?> getObjectType() {
return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
Set<HostAndPort> haps = this.parseHostAndPort();
jedisCluster = new JedisCluster(haps, timeout);
}
public Integer getTimeout() {
return timeout;
}
public void setTimeout(Integer timeout) {
this.timeout = timeout;
}
public String getCluster() {
return cluster;
}
public void setCluster(String cluster) {
this.cluster = cluster;
}
}
从代码中可以看出我们并没有提供连接池这样的概念,实际上JedisCluster自己维护了一个连接池,可以看GenericObjectPoolConfig源码
注意,pom中的jedis版本必须在2.7.2及以上版本,这个版本以上才支持JedisCluster
新建redis-cluster.properties,内容如下:
# redis jedis version 2.7.2
redis.cluster=192.168.88.140:6378,192.168.88.140:6379,192.168.88.143:6378,192.168.88.143:6379,192.168.88.145:6378,192.168.88.145:6379
redis.timeout=300000
新建redis-cluster.xml,内容为:
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="jedisClusterFactory" class="xxx.disconf.demo.service.JedisClusterFactory">
<property name="cluster" value="${redis.cluster}" />
<property name="timeout" value="${redis.timeout}" />
</bean>
</beans>
那么我们使用的时候注入jedisClusterFactory就可以使用了,这个bean比较轻,不需要依赖别的bean,所以之前提到的disconf中修改了redis的配置后相应的jedisClusterFactory也会重新flush成新的bean
参考:https://github.com/knightliao/disconf/wiki
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://jthink.blog.csdn.net/article/details/50588289
内容来源于网络,如有侵权,请联系作者删除!