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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - Spring-data-redis操作redis cluster的示例代碼

Spring-data-redis操作redis cluster的示例代碼

2021-06-03 11:46moonandstar08 Java教程

這篇文章主要介紹了Spring-data-redis操作redis cluster的示例代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

redis 3.x版本引入了集群的新特性,為了保證所開發系統的高可用性項目組決定引用redis的集群特性。對于redis數據訪問的支持,目前主要有二種方式:一、以直接調用jedis來實現;二、使用spring-data-redis,通過spring的封裝來調用。下面分別對這二種方式如何操作redis進行說明。

一、利用jedis來實現

通過jedis操作redis cluster的模型可以參考redis官網,具體如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
set<hostandport> jedisclusternodes = new hashset<hostandport>();
 
 //jedis cluster will attempt to discover cluster nodes automatically
 
 jedisclusternodes.add(new hostandport("10.96.5.183",9001));
 
 jedisclusternodes.add(new hostandport("10.96.5.183",9002));
 
 jedisclusternodes.add(new hostandport("10.96.5.183",9003));
 
jediscluster jc = new jediscluster(jedisclusternodes);
 
jc.set("foo","bar");
 
jc.get("foo");

二、利用spring-data-redis來實現

目前spring-data-redis已發布的主干版本都不能很好的支持redis cluster的新特性。為了解決此問題spring-data-redis開源項目組單獨拉了一個315分支,但截止到目前尚未發布。下面在分析spring-data-redis源碼的基礎上配置spring實現操作redis cluster.下面分別針對xml和注入的方式進行說明。

315分支gitHub下載路徑如下:https://github.com/spring-projects/spring-data-redis

315分支源碼下載路徑:http://maven.springframework.org/snapshot/org/springframework/data/spring-data-redis/1.7.0.DATAREDIS-315-SNAPSHOT/

(1)采用setclusternodes屬性方式構造redisclusterconfiguration

代碼目錄結構如下所示:

?
1
2
3
4
5
src
  com.example.bean
  com.example.repo
  com.example.repo.impl
  resources

a.在resources目錄下增加spring-config.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
<!--通過構造方法注入redisnode-->
 
 <bean id="clusterredisnodes1"  class="org.springframework.data.redis.connection.redisnode">
 
    <constructor-arg value="10.96.5.183" />
 
    <constructor-arg value="9002" type="int" />
 
 </bean>
 
 ....
 
<!--setter方式注入-->
 
<bean id="redisclusterconfiguration"  class="org.springframework.data.redis.connection.redisclusterconfiguration">
 
  <property name="clusternodes">
 
     <set>
 
          <ref bean="clusterredisnodes1"/>
 
          <ref bean="clusterredisnodes2"/>
 
          <ref bean="clusterredisnodes3"/>
 
     </set>
 
  </property>
 
 <!--紅色所示部分在從github上獲取的jar包中無對應setter方法,因此需要修改其對應的源碼。

另外,如果不設置clustertimeout值,源碼中默認為2s。當集群服務器與客戶端不在同一服務器上時,容易報:could not get a resource from the cluster;

如果不設置maxredirects值,源碼中默認為5。一般當此值設置過大時,容易報:too many cluster redirections -->

?
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
34
35
    <property name="clustertimeout" value="10000" />
 
    <property name="maxredirects"  value="5" />
 
  </bean>
 
 <!--setter方式注入,對應的屬性需存在setterxxx方法-->
 
  <bean id="jedispoolconfig"  class="redis.clients.jedis.jedispoolconfig">
 
      <property name="maxtoal" value="1000" />
 
      <property name="maxidle" value="1000" />
 
      <property name="maxwaitmillis" value="1000" />
 
  </bean>
 
 <bean id="jedisconnfactory"  class="org.springframework.data.redis.connection.jedis.jedisconnectionfactory" p:use-pool="true">
 
      <constructor-arg ref="redisclusterconfiguration" />
 
      <constructor-arg ref="jedispoolconfig" />
 
 </bean>
 
 <bean id="redistemplate"  class="org.springframework.data.redis.core.redistemplate" p:connection-factory-ref="jedisconnfactory" />
 
<!--setter方式注入personrepoimpl-->
 
<bean id="personrepo" class="com.example.repo.impl.personrepoimpl">
 
    <property name="redistemplate" ref="redistemplate" />
 
</bean>

注:加載lua文件

?
1
2
3
4
5
6
7
<bean id ="xxx" class="org.springframework.data.redis.core.script.defaultredisscript">
 
  <property name="location" value="./redis/xxx.lua" />
 
 <property name="resulttype" value="java.lang.void" />
 
</bean>

在com.example.repo.impl下增加personrepoimpl,主要包括屬性private redistemplate<string,bean>  redistemplate(該屬性存在setterxxx方法,對應property屬性);

利用redistemplate.opsforhash().put()即可完成對redis cluster的操作。

?
1
2
3
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(new classpathresource("resources/spring-config.xml").getpath());
 
repo repo =(repo)context.getbean("personrepo");

(2)采用redisclusterconfiguration(propertysource<?> propertysource)方式構造redisclusterconfiguration

  代碼目錄結構如下所示:

?
1
2
3
4
5
6
src
  com.redis.cluster.support.config
       monitorconfig
  resources
      spring-config.xml
       redis.properties

a.在resources目錄下增加spring-config.xml配置,配置如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!--配置文件加載-->
 
 <context:property-placeholder location="resources/redis.properties"/>
 
<context:property-placeholder location="resources/xxx.properties"/>
 
 <bean class="com.redis.cluster.support.config.monitorconfig" />
 
<!--對靜態資源文件的訪問-->
 
 <mvc:default-servlet-handler/>
 
 <mvc:annotation-driven />
 
 <context:component-scan base-package="com.redis.cluster"/>

b.添加redis.properties文件

?
1
2
3
spring.redis.cluster.nodes=10.48.193.201:7389,10.48.193.201:7388
spring.redis.cluster.timeout=2000
spring.redis.cluster.max-redirects=8

c.編寫初始化jedisconnectionfactory連接工廠的java類

?
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@configuration
 
public class monitorconfig {
 
  @value("${spring.redis.cluster.nodes}")
 
  private string clusternodes;
 
  @value("${spring.redis.cluster.timeout}")
 
  private long timeout;
 
 @value("${spring.redis.cluster.max-redirects}")
 
  private int redirects;
 
  @bean
 
  public redisclusterconfiguration getclusterconfiguration() {
 
    map<string, object> source = new hashmap<string, object>();
 
    source.put("spring.redis.cluster.nodes", clusternodes);
 
    source.put("spring.redis.cluster.timeout", timeout);
 
    source.put("spring.redis.cluster.max-redirects", redirects);
 
    return new redisclusterconfiguration(new mappropertysource("redisclusterconfiguration", source));
 
   }
 
  @bean
 
  public jedisconnectionfactory getconnectionfactory() {
 
    return new jedisconnectionfactory(getclusterconfiguration());
 
   }
 
 @bean
 
  public jedisclusterconnection getjedisclusterconnection() {
 
    return (jedisclusterconnection) getconnectionfactory().getconnection();
 
   }
 
  @bean
 
  public redistemplate getredistemplate() {
 
    redistemplate clustertemplate = new redistemplate();
 
    clustertemplate.setconnectionfactory(getconnectionfactory());
 
    clustertemplate.setkeyserializer(new defaultkeyserializer());
 
    clustertemplate.setdefaultserializer(new genericjackson2jsonredisserializer());
 
    return clustertemplate;
 
   }
 
  }

d.通過注解方式使用jedisclusterconnection和redistemplate

?
1
2
3
4
5
6
7
@autowired
 
 jedisclusterconnection clusterconnection;
 
@autowired
 
redistemplate redistemplate;

三、簡單集成spring

自己編寫jediscluster的工廠類jedisclusterfactory,然后通過spring注入的方式獲取jediscluster,實現客戶端使用redis3.0版本的集群特性。

請參考:http://www.ythuaji.com.cn/article/140100.html

使用時,直接通過注解或者xml注入即可,如下所示:

?
1
2
@autowired
jediscluster jediscluster;

  或者

?
1
2
3
4
5
<bean id="testredis" class="com.test.testredis">
 
  <property name="jediscluster" ref="jedisclusterfactory" />
 
</bean>
?
1
2
3
classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(new classpathresource("resources/spring-config.xml").getpath());
 
testredis testredis=(testredis)context.getbean("testredis");

四、redis cluster調試中常見錯誤

(1)當客戶端與集群服務器不在同一臺服務器上時,有如下錯誤could not get a resource from the cluster

一般當客戶端與集群服務器在同一臺服務器上時,操作redis cluster正常; 當二者不在同一臺服務器上時報如上錯誤,可能是clustertimeout時間設置過小;

(2)操作redis時報too many cluster redirections

初始化jediscluster時,設定jediscluster的maxredirections.

?
1
2
jediscluster(set<hostandport> jedisclusternode, int timeout, int maxredirections) ;
jediscluster jc = new jediscluster(jedisclusternodes,5000,1000);

請參考:https://github.com/xetorthio/jedis/issues/659

(3)redis cluster數據寫入慢

檢查在通過./redis-trib命令建立集群時,如果是通過127.0.0.1的方式建立的集群,那么在往redis cluster中寫入數據時寫入速度比較慢??梢酝ㄟ^配置真實的ip來規避此問題。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/moonandstar08/p/5149585.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色综合久久日韩国产 | 婷婷丁香视频 | 国产精品一区二区三区免费 | 97福利社 | 久久久久嫩草影院精品 | 成人国产午夜在线视频 | 欧美日韩亚毛片免费观看 | 日本韩国无矿砖码 | 被夫上司侵犯了中文字幕 | 2020年精品国产午夜福利在线 | japonensis日本护士18 | 国产精品边做边接电话在线观看 | 小寡妇水真多好紧 | 喷奶水榨乳ova动漫无修 | 男女男精品网站免费观看 | 亚洲国产婷婷俺也色综合 | 182免费在线观看 | 亚洲欧美一区二区三区不卡 | 国产福利在线观看第二区 | 金牛网155755水心论坛黄大父母 | 999导航| 和岳m的小说 | h黑寡妇一级毛片 | 好姑娘在线视频观看免费 | 高清国产在线观看 | 国产区最新 | 精品一区二区三区视频 | 精品亚洲综合久久中文字幕 | 成年男人永久免费看片 | 国产精品xxxav免费视频 | 男女性潮高片无遮挡禁18 | 女同久久另类99精品国产 | 亚洲色图欧美色 | 日本一区二区三区在线 观看网站 | 好紧好爽的午夜寂寞视频 | 欧美在线视频免费播放 | 亚洲国产精品综合久久一线 | 国产在线观看福利片 | 动漫jk美女被爆羞羞漫画 | 日本一级不卡一二三区免费 | 欧美精品一区二区三区久久 |