disconf-基于xml分布式配置管理redis

x33g5p2x  于2021-12-20 转载在 其他  
字(4.1k)|赞(0)|评价(0)|浏览(370)

本文介绍disconf如何管理redis(单机和集群)的配置

  1. redis单机
    注意,pom中jedis的版本必须2.4.2及以下版本,否则报错
    新建redis-single.properties,内容为:
# 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

  1. redis集群
    由于我们redis搭建的是真实集群(基于3.0以上的版本),而目前spring data redis还不支持redis的集群,所以必须使用原生的jedis,做了个简单的封装如下
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

相关文章