redis哨兵模式客户端(非企业版-我自己耍的)

时间:2022-09-12 16:56:28

分为4个类和一个配置文件redis哨兵模式客户端(非企业版-我自己耍的)还有一个redis.xml

下面是redis.xml的配置(关于jedisPoolConfig的配置需要按照自己公司的条件进行修改):

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
         <property name="maxTotal" value="10"/>
         <property name="maxIdle" value="10"/>
         <property name="minIdle" value="1"/>
         <property name="maxWaitMillis" value="3000"/>
         <property name="blockWhenExhausted" value="true"/>
         <property name="testOnBorrow" value="true"/>
         <property name="testOnReturn" value="true"/>
         <property name="testWhileIdle" value="true"/>
    </bean>
    
    <bean id="myJedisPool1" class="redisMasterAndSentinel.MyJedisPool1" init-method="init">
        <property name="masterName" value="mymaster"></property>
        <property name="jedisPoolConfig" ref="jedisPoolConfig"></property>
        <property name="redisAddress">
            <set>
                <value>192.168.200.136:26379</value>
                <value>192.168.200.135:26379</value>
                <value>192.168.200.135:26380</value>
            </set>
        </property>
    </bean>
    
    <bean name="redisClient" class="redisMasterAndSentinel.RedisClient">
        <property name="myJedisPool" ref="myJedisPool1"></property>
    </bean>

 抽象类RedisAbstractTemplate:

public abstract class RedisAbstractTemplate {
    protected MyJedisPool1 myJedisPool;

    public MyJedisPool1 getMyJedisPool() {
        return myJedisPool;
    }

    public void setMyJedisPool(MyJedisPool1 myJedisPool) {
        this.myJedisPool = myJedisPool;
    }
    
    

}

操作客户端RedisClient(这里仅仅实现了一个set方法,其实感觉在这里可以实现一些常用的方法,然后如果后期有需求的话可以自己注入myJedisPool,自己获取连接 进行操作数据):

public class RedisClient extends RedisAbstractTemplate {
    
    public boolean set(String key,String value){
        Jedis jedis = null;
        try {
             jedis= myJedisPool.getJedis();
             if(jedis!=null){
                 jedis.set(key, value);
                 return true;
             }else{
                 return false;
             }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            if (jedis!=null) {
                jedis.close();
            }
        }
    }
    
}

MyJedisPool1连接池:

public class MyJedisPool1 {
    private JedisSentinelPool jedisSentinelPool;
    private String masterName;
    private JedisPoolConfig jedisPoolConfig;
    private Set<String> redisAddress;
    private ThreadLocal<Jedis> jedisThreadLocal=new ThreadLocal<Jedis>();


    public void init(){
        jedisSentinelPool = new JedisSentinelPool(masterName, redisAddress,jedisPoolConfig);
    }
    
    public Jedis getJedis(){
        Jedis resource = jedisSentinelPool.getResource();
        jedisThreadLocal.set(resource);
        return resource;
    }
    public void backToJedisPool(){
        Jedis jedis = jedisThreadLocal.get();
        jedis.close();
    }



    public JedisPoolConfig getJedisPoolConfig() {
        return jedisPoolConfig;
    }

    public void setJedisPoolConfig(JedisPoolConfig jedisPoolConfig) {
        this.jedisPoolConfig = jedisPoolConfig;
    }

    public String getMasterName() {
        return masterName;
    }

    public void setMasterName(String masterName) {
        this.masterName = masterName;
    }

    public Set<String> getRedisAddress() {
        return redisAddress;
    }

    public void setRedisAddress(Set<String> redisAddress) {
        this.redisAddress = redisAddress;
    }
    

}

测试类TestRedisClient:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:redis.xml" })
public class TestRedisClient {
    @Autowired
    RedisClient redisClient;
    @Test
    public void testMethod1() throws Exception {
        for(int i=0;i<60;i++){
            boolean set = redisClient.set("key"+i,"value"+i);
            if (!set) {
                System.out.println("往redis存数据出现错误:key"+i);
            }
            Thread.sleep(1000L);
            System.out.println("for循环第"+i+"次");
        }

        
    }

}

有不妥的或者错误的望指出,不要误人子弟呀

2018-09-06

先搞明白-后熟悉-最后精湛