一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Spring集成jedis的配置與使用簡單實例

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

2021-07-21 11:458blues Java教程

今天小編就為大家分享一篇關(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)包:

?
1
2
3
4
5
6
<!-- redis依賴包 -->
<dependency>
 <groupid>redis.clients</groupid>
 <artifactid>jedis</artifactid>
 <version>2.9.0</version>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?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連接池的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?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為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#最大分配的對象數(shù)
redis.pool.maxactive=200
#最大能夠保持idel狀態(tài)的對象數(shù)
redis.pool.maxidle=50
redis.pool.minidle=10
redis.pool.maxwaitmillis=20000
#當池內(nèi)沒有返回對象時,最大等待時間
redis.pool.maxwait=300
#格式:redis://:[密碼]@[服務(wù)器地址]:[端口]/[db index]
redis.uri = redis://:[email protected]:6379/0
redis.host = 127.0.0.1
redis.port = 6379
redis.timeout=30000
redis.password = 12345
redis.database = 0

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

shardedjedis操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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操作:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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í)價值,謝謝大家對服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

原文鏈接:https://blog.csdn.net/u014320421/article/details/80576232

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久se精品一区二区国产 | 奇米影视久久777中文字幕 | 日本高清动作片www欧美 | 亚洲高清无在码在线电影 | 免费国产白棉袜踩踏区域 | 久久天堂成人影院 | 91次元成年破解版 | 男男视频18免费网站 | 成人免费观看在线视频 | 好男人在线观看免费高清2019韩剧 | 国产一卡二卡3卡4卡四卡在线 | 国产伦精品一区二区 | 99人中文字幕亚洲区 | 成人免费福利网站在线看 | 九色PORNY真实丨国产免费 | 国产伦精品一区二区三区免费迷 | 四虎永久在线精品国产 | 亚洲H成年动漫在线观看不卡 | 俄罗斯13一14处出血视频在线 | 俄罗斯15一16处交 | 色综合久久天天综合观看 | 成人伊人亚洲人综合网站222 | 双性np玩烂了np欲之国的太子 | 日本玖玖视频 | 女人肮脏的交易中文字幕未删减版 | 亚洲va天堂va国产va久久 | 高清在线免费 | 毛片一级免费 | 国产精品久久久久一区二区三区 | 国亚洲欧美日韩精品 | 精品视频免费在线 | 国产这里有精品 | 欧美日韩精品亚洲精品v18 | 热99精品只有里视频最新 | 国产成年人在线观看 | 久久亚洲成a人片 | 亚洲AV久久无码精品蜜桃 | 国产探花在线视频 | 99ri国产精品 | 国产精品一区二区不卡的视频 | 日韩欧美国产一区 |