最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring集成jedis的配置與使用簡單實例

 更新時間:2019年03月12日 14:16:21   作者:8blues  
今天小編就為大家分享一篇關(guān)于Spring集成jedis的配置與使用簡單實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

jedis是redis的java客戶端,spring將redis連接池作為一個bean配置。

redis連接池分為兩種,一種是“redis.clients.jedis.ShardedJedisPool”,這是基于hash算法的一種分布式集群redis客戶端連接池。

另一種是“redis.clients.jedis.JedisPool”,這是單機環(huán)境適用的redis連接池。

maven導(dǎo)入相關(guān)包:

  <!-- redis依賴包 -->
  <dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
  </dependency>

ShardedJedisPool是redis集群客戶端的對象池,可以通過他來操作ShardedJedis,下面是ShardedJedisPool的xml配置,spring-jedis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--shardedJedisPool的相關(guān)配置-->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!--新版是maxTotal,舊版是maxActive-->
    <property name="maxTotal">
      <value>${redis.pool.maxActive}</value>
    </property>
    <property name="maxIdle">
      <value>${redis.pool.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.uri}" />
        </bean>
      </list>
    </constructor-arg>
  </bean>
</beans>

下面是單機環(huán)境下redis連接池的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <!-- 引入jedis的properties配置文件 -->
  <!--如果你有多個數(shù)據(jù)源需要通過<context:property-placeholder管理,且不愿意放在一個配置文件里,那么一定要加上ignore-unresolvable=“true"-->
  <context:property-placeholder location="classpath:properties/redis.properties" ignore-unresolvable="true" />
  <!--Jedis連接池的相關(guān)配置-->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!--新版是maxTotal,舊版是maxActive-->
    <property name="maxTotal">
      <value>${redis.pool.maxActive}</value>
    </property>
    <property name="maxIdle">
      <value>${redis.pool.maxIdle}</value>
    </property>
    <property name="testOnBorrow" value="true"/>
    <property name="testOnReturn" value="true"/>
  </bean>
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
    <constructor-arg name="host" value="${redis.host}" />
    <constructor-arg name="port" value="${redis.port}" type="int" />
    <constructor-arg name="timeout" value="${redis.timeout}" type="int" />
    <constructor-arg name="password" value="${redis.password}" />
    <constructor-arg name="database" value="${redis.database}" type="int" />
  </bean>
</beans>

對應(yīng)的classpath:properties/redis.properties.xml為:

#最大分配的對象數(shù)
redis.pool.maxActive=200
#最大能夠保持idel狀態(tài)的對象數(shù)
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.maxWaitMillis=20000
#當(dāng)池內(nèi)沒有返回對象時,最大等待時間
redis.pool.maxWait=300
#格式:redis://:[密碼]@[服務(wù)器地址]:[端口]/[db index]
redis.uri = redis://:12345@127.0.0.1:6379/0
redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0

二者操作代碼類似,都是先注入連接池,然后通過連接池獲得jedis實例,通過實例對象操作redis。

ShardedJedis操作:

  @Autowired
  private ShardedJedisPool shardedJedisPool;//注入ShardedJedisPool
  @RequestMapping(value = "/demo_set",method = RequestMethod.GET)
  @ResponseBody
  public String demo_set(){
    //獲取ShardedJedis對象
    ShardedJedis shardJedis = shardedJedisPool.getResource();
    //存入鍵值對
    shardJedis.set("key1","hello jedis");
    //回收ShardedJedis實例
    shardJedis.close();
    return "set";
  }
  @RequestMapping(value = "/demo_get",method = RequestMethod.GET)
  @ResponseBody
  public String demo_get(){
    ShardedJedis shardedJedis = shardedJedisPool.getResource();
    //根據(jù)鍵值獲得數(shù)據(jù)
    String result = shardedJedis.get("key1");
    shardedJedis.close();
    return result;
  }

Jedis操作:

  @Autowired
  private JedisPool jedisPool;//注入JedisPool
  @RequestMapping(value = "/demo_set",method = RequestMethod.GET)
  @ResponseBody
  public String demo_set(){
    //獲取ShardedJedis對象
    Jedis jedis = jedisPool.getResource();
    //存入鍵值對
    jedis.set("key2","hello jedis one");
    //回收ShardedJedis實例
    jedis.close();
    return "set";
  }
  @RequestMapping(value = "/demo_get",method = RequestMethod.GET)
  @ResponseBody
  public String demo_get(){
    Jedis jedis = jedisPool.getResource();
    //根據(jù)鍵值獲得數(shù)據(jù)
    String result = jedis.get("key2");
    jedis.close();
    return result;
  }

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • 一文搞懂Java橋接方法

    一文搞懂Java橋接方法

    這篇文章主要介紹了Java中的橋接方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • SpringCloud OpenFeign Post請求400錯誤解決方案

    SpringCloud OpenFeign Post請求400錯誤解決方案

    這篇文章主要介紹了SpringCloud OpenFeign Post請求400錯誤解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • MyBatis各種類型查詢數(shù)據(jù)參數(shù)綁定的實現(xiàn)

    MyBatis各種類型查詢數(shù)據(jù)參數(shù)綁定的實現(xiàn)

    本文主要介紹了MyBatis各種類型查詢數(shù)據(jù)參數(shù)綁定的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • SpringCloud OpenFeign超時控制示例詳解

    SpringCloud OpenFeign超時控制示例詳解

    在Spring Cloud中使用OpenFeign時,可以通過配置來控制請求的超時時間,這篇文章主要介紹了SpringCloud OpenFeign超時控制,需要的朋友可以參考下
    2024-05-05
  • JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作

    JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作

    這篇文章主要介紹了JAVA設(shè)置手動提交事務(wù),回滾事務(wù),提交事務(wù)的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Java int與integer的對比區(qū)別

    Java int與integer的對比區(qū)別

    這篇文章主要介紹了Java int與integer的對比區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • JAVA 枚舉單例模式及源碼分析的實例詳解

    JAVA 枚舉單例模式及源碼分析的實例詳解

    這篇文章主要介紹了 JAVA 枚舉單例模式及源碼分析的實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • Java解析JSON數(shù)據(jù)時報錯問題解決方案

    Java解析JSON數(shù)據(jù)時報錯問題解決方案

    這篇文章主要介紹了Java解析JSON數(shù)據(jù)時報錯問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Spring Boot 啟動流程解析

    Spring Boot 啟動流程解析

    Spring Boot 是一個簡化的 Spring 應(yīng)用開發(fā)框架,它以 “約定優(yōu)于配置” 的理念,為開發(fā)者提供了開箱即用的功能,本文將詳細剖析其內(nèi)部實現(xiàn),幫助你深入理解 Spring Boot 的啟動機制,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Spring boot 連接多數(shù)據(jù)源過程詳解

    Spring boot 連接多數(shù)據(jù)源過程詳解

    這篇文章主要介紹了Spring boot 連接多數(shù)據(jù)源過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08

最新評論

桃源县| 南陵县| 灵石县| 栾城县| 包头市| 十堰市| 利津县| 山东省| 惠东县| 松潘县| 曲靖市| 凤阳县| 白水县| 台山市| 洛扎县| 凤台县| 伊通| 姜堰市| 安吉县| 上蔡县| 会泽县| 宁陕县| 尖扎县| 江达县| 黑水县| 雅江县| 景洪市| 江油市| SHOW| 武定县| 榕江县| 北海市| 威信县| 津南区| 丘北县| 兰溪市| 奈曼旗| 济宁市| 饶河县| 钟祥市| 榆林市|