1. 程式人生 > >java web項目中redis集群或單擊版配置詳解

java web項目中redis集群或單擊版配置詳解

redis集群 redis單機 jedis jedis客戶端集群 jedis客戶端單機

單機版配置

 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大連接數 -->
        <property name="maxTotal" value="100" />
        <!-- 最大空閑連接數 -->
        <property name="maxIdle" value="300" />
        <!-- 每次釋放連接的最大數目 -->
        <property name="numTestsPerEvictionRun" value="1024" />
        <!-- 釋放連接的掃描間隔(毫秒) -->
        <property name="timeBetweenEvictionRunsMillis" value="30000" />
        <!-- 連接最小空閑時間 -->
        <property name="minEvictableIdleTimeMillis" value="1800000" />
        <!-- 連接空閑多久後釋放, 當空閑時間>該值 且 空閑連接>最大空閑連接數 時直接釋放 -->
        <property name="softMinEvictableIdleTimeMillis" value="10000" />
        <!-- 獲取連接時的最大等待毫秒數,小於零:阻塞不確定的時間,默認-1 -->
        <property name="maxWaitMillis" value="1500" />
        <!-- 在獲取連接的時候檢查有效性, 默認false -->
        <property name="testOnBorrow" value="true" />
        <!-- 在空閑時檢查有效性, 默認false -->
        <property name="testWhileIdle" value="true" />
        <!-- 連接耗盡時是否阻塞, false報異常,ture阻塞直到超時, 默認true -->
        <property name="blockWhenExhausted" value="false" />
    </bean> 
     <bean id="redisClient" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
        <constructor-arg name="host" value="${redis.host}"></constructor-arg>
        <constructor-arg name="port" value="${redis.port}"></constructor-arg>

        <!-- <constructor-arg name="timeout" value="${redis.timeOut}"></constructor-arg>
        <constructor-arg name="password" value="${redis.password}"></constructor-arg>  -->
    </bean> 
    <bean id="jedisClient" class="com.yagoo.wificontrolsys.redis.impl.JedisClientSingle"/>

集群版配置,其中constructor-arg name="host" value="192.168.199.203"指定ip更換即可

<bean id="redisClient" class="redis.clients.jedis.JedisCluster">
        <constructor-arg name="nodes">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7001"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7002"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7003"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7004"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7005"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="192.168.199.203"></constructor-arg>
                    <constructor-arg name="port" value="7006"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
    </bean>
    <bean id="jedisClient" class="com.wenwuyi.study.rest.dao.impl.JedisClientCluster"/> 

兩個客戶端代碼
客戶端接口

/**
 * Copyright (c) 2017. yagoosafe.com All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with yagoosafe.com.
 */
package com.yagoo.wificontrolsys.redis;

/** 
 * 類名:JedisClient.java
 * 描述:jedis client
 * 時間:2018年3月8日 下午4:59:30 
 * @author yangchangjiang  
 * @version 1.0 
 */
public interface JedisClient {
    /**
     * 
     * 根據key獲取信息
     * @param key
     * @return String
     */
    String get(String key);
    /**
     * 
     * 設置信息
     * @param key
     * @param value
     * @return String
     */
    String set(String key,String value);
    /**
     * 
     * 設置信息帶過期時間
     * @param key
     * @param value
     * @param expire
     * @return String
     */
    String set(String key, String value, int expire);
    /**
     * 
     * hset 帶多key值
     * @param hkey
     * @param key
     * @return String
     */
    String hget(String hkey,String key);
    /**
     * 
     * hset 帶多key值和value值
     * @param hkey
     * @param key
     * @param value
     * @return long
     */
    long hset(String hkey,String key,String value);
    /**
     * 
     * Incr鍵值+1
     * @param key
     * @return long
     */
    long incr(String key);
    /**
     * 
     * 設置過期時間
     * @param key
     * @param second
     * @return long
     */
    long expire(String key,int second);
    /**
     * 
     * 查看過期時間
     * @param key
     * @return long
     */
    long ttl(String key);
    /**
     * 
     * 刪除對應key值
     * @param key
     * @return long
     */
    long del(String key);
    /**
     * 
     * 刪除hkey和key
     * @param hkey
     * @param key
     * @return long
     */
    long hdel(String hkey,String key);
}

單機版實現

/*
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 */
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.yagoo.wificontrolsys.redis.JedisClient;

/** 
 * 類的名稱JedisClientSingle.java類
 * 類的作用:Redis單機版
 * @author YCJ  作者 E-mail: [email protected]
 * @date 創建時間:2017年12月24日 下午12:51:39 
 * @version 1.0 
 */
public class JedisClientSingle implements JedisClient{

    @Autowired
    private JedisPool jedisPool; 

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }

    @Override
    public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }

    @Override
    public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }

    @Override
    public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }

    @Override
    public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.del(key);
        jedis.close();
        return result;
    }

    @Override
    public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(hkey,key);
        jedis.close();
        return result;
    }

    @Override
    public String set(String key, String value, int expire) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.expire(key, expire);
        jedis.close();
        return string;
    }

}

集群版實現

/*
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 */
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;

import com.yagoo.wificontrolsys.redis.JedisClient;

/** 
 * 類的名稱JedisClientCluster.java類
 * 類的作用:redis集群版
 * @author YCJ  作者 E-mail: [email protected]
 * @date 創建時間:2017年12月24日 下午12:54:15 
 * @version 1.0 
 */
public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String hget(String hkey, String key) {
        return jedisCluster.hget(hkey, key);
    }

    @Override
    public long hset(String hkey, String key, String value) {
        return jedisCluster.hset(hkey, key, value);
    }

    @Override
    public long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public long expire(String key, int second) {
        return jedisCluster.expire(key, second);
    }

    @Override
    public long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public long del(String key) {
        return jedisCluster.del(key);
    }

    @Override
    public long hdel(String hkey, String key) {
        return jedisCluster.hdel(hkey,key);
    }

    @Override
    public String set(String key, String value, int expire) {
        String string = jedisCluster.set(key, value);
        jedisCluster.expire(key, expire);
        return string;
    }

}

java web項目中redis集群或單擊版配置詳解