zoukankan      html  css  js  c++  java
  • 针对JedisShardInfo中无法修改db的解决办法

    package com.ldr.bean;
    
    import java.lang.reflect.Field;
    
    import redis.clients.jedis.JedisShardInfo;
    
    public class MyJedisInfo {
        
        String host;
        int port;
        int db;
    
        public JedisShardInfo newInstance() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {  
            JedisShardInfo jedisShardInfo=new JedisShardInfo(host,port) ;
            Class<? extends JedisShardInfo> clz = jedisShardInfo.getClass();
            Field declaredField = clz.getDeclaredField("db");
            declaredField.setAccessible(true);
            declaredField.set(jedisShardInfo, db);
            return jedisShardInfo;
        }
    
        public String getHost() {
            return host;
        }
    
        public void setHost(String host) {
            this.host = host;
        }
    
        public int getDb() {
            return db;
        }
    
        public void setDb(int db) {
            this.db = db;
        }
        public int getPort() {
            return port;
        }
    
        public void setPort(int port) {
            this.port = port;
        }  
    }

    spring中的application.xml中配置如下

        
        <!-- spring集成redis -->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxTotal">
            <value>${redis.maxTotal}</value>
            </property>
            <property name="maxIdle">
             <value>${redis.maxIdle}</value>
            </property>
            <property name="testOnBorrow" value="true"/>
            <property name="testOnReturn" value="true"/>
        </bean>
       
         <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"  scope="singleton">
            <constructor-arg index="0" ref="jedisPoolConfig" />
            <constructor-arg index="1">
                <list>
                    <!-- <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg name="host" value="${redis.host}" />
                        <constructor-arg name="port" value="${redis.port}" />
                    </bean> -->
              
                    <ref bean="jedisShardInfo"/><!-- 生产环境请换成上述 -->
                </list>
            </constructor-arg>
        </bean>
        
        <!-- 以下配置上生产请注释掉  begin-->
        <bean id="jedisFactory" class="com.ldr.bean.MyJedisInfo">
            <property name="host" value="${redis.host}"></property>
            <property name="port" value="${redis.port}"></property>
            <property name="db" value="${redis.db}"></property>
        </bean>  
         
        <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo"  
            factory-bean="jedisFactory" factory-method="newInstance" >  
        </bean> 
         <!-- 以上配置上生产请注释掉 end -->
  • 相关阅读:
    从SAPI 5.1中提取中文发音引擎
    多图:你没见过的古董级PC(zz)
    难搞的证书
    原来VS.Net 2005正式版真的发布了
    Google要改进OpenOffice 并公布其搜索计算数据中心细节(zz)
    AMD CPU市占率突破20%!(zz)
    重定向页面会Alert()不了?
    忍无可忍,希望大家不要来苏州园区工作
    MSN登陆不了怎么办
    网易126免费域名去广告
  • 原文地址:https://www.cnblogs.com/wangyang108/p/8962925.html
Copyright © 2011-2022 走看看