针对JedisShardInfo中无法修改db的解决办法

Posted 傻瓜不傻108

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了针对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 -->

 

以上是关于针对JedisShardInfo中无法修改db的解决办法的主要内容,如果未能解决你的问题,请参考以下文章

Runstats,Reorgchk,Reorg,Rebind

Linux针对单多文件的解压缩操作

Ubuntu中针对问题 E: Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)的解

针对 Access 2010 DB 的 SQL 语句不适用于 ODBC

Oracle 中scott 用户的解锁以及修改密码

mongoDB-----针对某个或多个文档只需要部分更新可使用原子的更新修改器