redis双写

redis双写,其实就是配置两个redis客户端而已。不多说,直接上代码

首先看,redis和spring结合的配置文件spring-redis.xml配置(M6和兆维代表两个机房)

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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">
    <!--兆维 redis配置-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${rediszw.maxIdle}" />
        <property name="maxActive" value="${rediszw.maxActive}" />
        <property name="maxWait" value="${rediszw.maxWait}" />
        <property name="testOnBorrow" value="${rediszw.testOnBorrow}" />
    </bean>

    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${rediszw.host}"  p:port="${rediszw.port}" p:password="${rediszw.pass}" p:pool-config-ref="poolConfig"/>
    <!--注意m6和兆维此处需要修改,m6只读m6缓存,兆维只读兆维缓存,故如果在m6上部署时,需要将redisTemplateM 和redisTemplate名称换下-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
        <!--         如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,
        那么会提示错误User can't cast to String!!! -->
                <property name="keySerializer">
                     <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
                 </property>
                 <property name="valueSerializer">
                     <bean
                         class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
                 </property>
    </bean>

    <!--M6 redis配置-->
    <bean id="poolConfigm" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redism.maxIdle}" />
        <property name="maxActive" value="${redism.maxActive}" />
        <property name="maxWait" value="${redism.maxWait}" />
        <property name="testOnBorrow" value="${redism.testOnBorrow}" />
    </bean>

    <bean id="connectionFactorym" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="${redism.host}"  p:port="${redism.port}" p:password="${redism.pass}" p:pool-config-ref="poolConfigm"/>
    <bean id="redisTemplateM" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactorym" />
        <!--         如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,
        那么会提示错误User can't cast to String!!! -->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean
                    class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
        </property>
    </bean>
</beans>

这样,spring中就管理了两个redis客户端。

 

接下来,看redis.properties配置

#兆维 redis
rediszw.host=codis.mall.bj.huansoo.com.cn
rediszw.port=1879
rediszw.pass=
#最大能够保持idel状态的对象数
rediszw.maxIdle=50

#最大分配的对象数
rediszw.maxActive=300

#当池内没有返回对象时,最大等待时间
rediszw.maxWait=1000

#当调用borrow Object方法时,是否进行有效性检查
rediszw.testOnBorrow=true

#M6 redis
redism.host=codis.m6.huansoo.com.cn
redism.port=1980
redism.pass=
#最大能够保持idel状态的对象数
redism.maxIdle=50
#最大分配的对象数
redism.maxActive=300
#当池内没有返回对象时,最大等待时间
redism.maxWait=1000
#当调用borrow Object方法时,是否进行有效性检查
redism.testOnBorrow=true

这样两个客户端的redis配置就完成了。

 

第三步,就是程序中怎么调用了,这个和单个redis调用其实是一样的

@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RedisTemplate redisTemplateM;

这样就或得到了两个redis客户端,写操作时,使用redisTemplate和redisTemplateM分别调用写方法,这样,数据就存到了兆维和m6两个机房对应的缓存中了

转载于:https://my.oschina.net/u/1762811/blog/731916

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值