加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:57:29 所属栏目:百科 来源:网络整理
导读:本文介绍disconf如何管理redis(单机和集群)的配置 redis单机 注意,pom中jedis的版本必须2.4.2及以下版本,否则报错 新建redis-single.properties,内容为: # redis jedis version 2.4.2 redis.host = localhost redis.port = 6379 redis.password = 新建
本文介绍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
  2. 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="upsmart.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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读