国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

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

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

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美综合在线观看 | 国产精品不卡一区二区三区 | 亚洲精品一区二区三区在线 | a级免费电影 | 日韩电影一区 | 欧美成视频 | 二区在线视频 | 日韩精品日韩激情日韩综合 | 亚洲视频精品 | 国产精品一码二码三码在线 | 国产a视频| 综合久久99 | 一级黄色片看看 | 7878www免费看片| 国产精品高清在线 | 亚洲免费精品 | 欧美成年网站 | 自拍视频在线 | 久久久国产一区二区三区 | 日韩欧美精品一区二区三区 | 亚洲一区二区三区四区五区中文 | 欧美国产一区二区 | 精品一区二区三区中文字幕 | 日韩在线观看视频一区二区三区 | 91久久精品一区二区二区 | 91视频免费看片 | 欧美一级二级视频 | 欧美电影网站 | 自拍在线| 欧美一区二区三区的 | 欧美一区二区 | 久久免费视频3 | 色猫猫国产区一区二在线视频 | 在线免费黄 | 亚洲欧美综合精品久久成人 | 欧美日韩不卡视频 | 国产精品美女久久久久久免费 | 亚洲国产精品久久人人爱 | 成人黄网视频在线观看 | 日韩精品一区二区三区在线 | 91成人在线 |