天天看點

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

        本文是緊接着上文安裝好單機版的redis和rediscluster的之後需要去驗證是否安裝成功,以及如何和spring內建到一起。

        接着這上文,我們先簡單的寫一個測試類,測試一下單機版的redis是否已經可用

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

@test  

    public void testjedissingle() {  

        // 建立一個jedis的對象。  

        jedisjedis= newjedis("192.168.21.225", 6379);  

        // 調用jedis對象的方法,方法名稱和redis的指令一緻。  

        jedis.set("key1", "jedis test");  

        stringstring= jedis.get("key1");  

        system.out.println(string);  

        // 關閉jedis。每次用完之後都應該關閉jedis  

        jedis.close();  

    }  

        測試結果入下:

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

        在實際的運用過程中,我們基本上不會這樣每連接配接一次redis就去建立一個jedis對象,而是會在程式加載的時候直接建立一個連接配接池。測試方法如下

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

/** 

   * 使用連接配接池 

   */  

  @test  

  public void testjedispool() {  

      // 建立jedis連接配接池  

      jedispoolpool= newjedispool("192.168.21.225", 6379);  

      // 從連接配接池中獲得jedis對象  

      jedisjedis= pool.getresource();  

      stringstring= jedis.get("key1");  

      system.out.println(string);  

      // 關閉jedis對象  

      jedis.close();  

      pool.close();  

  }  

        運作結果入下:

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

        測試完單機版之後,我們接下來看看如何使用jedis操作redis叢集(本文使用的是僞叢集)

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

    public void testjediscluster() {  

        hashset<hostandport>nodes= newhashset<>();  

        nodes.add(new hostandport("192.168.21.225",7001));  

        nodes.add(new hostandport("192.168.21.225",7002));  

        nodes.add(new hostandport("192.168.21.225",7003));  

        nodes.add(new hostandport("192.168.21.225",7004));  

        nodes.add(new hostandport("192.168.21.225",7005));  

        nodes.add(new hostandport("192.168.21.225",7006));  

        jedisclustercluster= newjediscluster(nodes);  

        cluster.set("key1", "1000");  

        stringstring= cluster.get("key1");  

        cluster.close();  

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

        通過上面的測試一方面我們知道了如何簡單的去操作redis,另一方面也驗證在之前介紹的redis的按照是沒有問題的。

        下面我們看看在spring的配置檔案中如何配置redis的參數和jedis相關的東西。

        首先我們看看redis連接配接池的配置

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

<!-- 連接配接池配置 -->  

    <bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">  

        <!-- 最大連接配接數 -->  

        <property name="maxtotal" value="30" />  

        <!-- 最大空閑連接配接數 -->  

        <property name="maxidle" value="10" />  

        <!-- 每次釋放連接配接的最大數目 -->  

        <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>  

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

<bean id="redisclient" class="redis.clients.jedis.jedispool">  

        <constructor-arg name="host" value="192.168.21.225"></constructor-arg>  

        <constructor-arg name="port" value="6379"></constructor-arg>  

        <constructor-arg name="poolconfig" ref="jedispoolconfig"></constructor-arg>  

        <bean id="jedisclient" class="com.taotao.rest.dao.impl.jedisclientsingle" />  

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

<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.21.225"></constructor-arg>  

                    <constructor-arg name="port" value="7001"></constructor-arg>  

                </bean>  

                    <constructor-arg name="port" value="7002"></constructor-arg>  

                    <constructor-arg name="port" value="7003"></constructor-arg>  

                    <constructor-arg name="port" value="7004"></constructor-arg>  

                    <constructor-arg name="port" value="7005"></constructor-arg>  

                    <constructor-arg name="port" value="7006"></constructor-arg>  

            </set>  

        </constructor-arg>  

    <bean id="jedisclientcluster"class="com.taotao.rest.dao.impl.jedisclientcluster"></bean>  

        為了友善筆者直接調用jedis去操作redis,是以筆者寫了一個jedisclient工具類,如下

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

public interfacejedisclient {  

    stringget(string key);  

    stringset(string key,string value);  

    stringhget(string hkey,string key);  

    long hset(string hkey, string key, string value);  

    long incr(string key);  

    long expire(string key, int second);  

    long ttl(string key);  

    long del(string key);  

    long hdel(string hkey, string key);  

}  

        由于對單機和叢集的redis操作時不一樣的,是以實作工具類的時候單機和叢集的實作方式是不一樣的。

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

public classjedisclientsingle implements jedisclient{  

    @autowired  

    private jedispool jedispool;  

    @override  

    public string get(string key) {  

        jedisjedis= jedispool.getresource();  

        stringstring= jedis.get(key);  

        return string;  

    public string set(string key, string value) {  

        stringstring= jedis.set(key, value);  

    public string hget(string hkey, string key) {  

        stringstring= jedis.hget(hkey, key);  

    public long hset(string hkey, string key, string value) {  

        longresult= jedis.hset(hkey, key, value);  

        return result;  

    public long incr(string key) {  

        longresult= jedis.incr(key);  

    public long expire(string key, int second) {  

        longresult= jedis.expire(key, second);  

    public long ttl(string key) {  

        longresult= jedis.ttl(key);  

    public long del(string key) {  

        jedis jedis = jedispool.getresource();  

        longresult= jedis.del(key);  

    public long hdel(string hkey, string key) {  

        longresult= jedis.hdel(hkey, key);  

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

public classjedisclientcluster implements jedisclient {  

    private jediscluster jediscluster;  

        return jediscluster.get(key);  

        return jediscluster.set(key, value);  

        return jediscluster.hget(hkey, key);  

        return jediscluster.hset(hkey, key, value);  

        return jediscluster.incr(key);  

        return jediscluster.expire(key, second);  

        return jediscluster.ttl(key);  

        return jediscluster.del(key);  

        return jediscluster.hdel(hkey, key);  

        下面是測試配置以及工具類的方法

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

    public void testspringjedissingle(){  

        applicationcontextapplicationcontext = newclasspathxmlapplicationcontext(  

                "classpath:spring/applicationcontext-*.xml");  

        jedispoolpool= (jedispool) applicationcontext.getbean("redisclient");  

        jedisjedis= pool.getresource();  

        pool.close();  

NoSQL之Redis(五)--- Redis測試以及其與spring的內建概述         Jedis         Redis安裝測試         與Spring的內建         工具類         測試         總結

    public voidtestspringjediscluster() {  

        jedisclusterjediscluster= (jediscluster) applicationcontext  

                .getbean("redisclient");  

        stringstring= jediscluster.get("key1");  

        jediscluster.close();  

        至此,本文的的寫作目的——測試叢集,以及redis配置與spring內建就達到了。其實在筆者在寫本文的時候,一方面是對之前項目中redis運用的優化,另一方面在實際的其實本文在實作jedisclient的時候,所有的方法中在筆者的項目中是添加有日志的。至于為什麼添加日志本文就不介紹了。